Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to declare a switch? #293

Closed
andig opened this issue Oct 10, 2014 · 28 comments

Comments

Projects
None yet
9 participants
@andig
Copy link
Contributor

commented Oct 10, 2014

Running 0.8.16 I'm having touble creating a switch:

{
  "id": "button2",
  "name": "Überschussverwendung",
  "class": "SwitchActuator",
  "state": "on"
}

[pimatic] no plugin found for device "button2" of class "SwitchActuator"!

Any ideas? Classname is copied from devices.coffee?

@wutu

This comment has been minimized.

Copy link

commented Oct 10, 2014

I use ShellSwitch:

{
  "onCommand": "",
  "offCommand": "",
  "getStateCommand": "echo off",
  "id": "alarm_office",
  "name": "Alarm",
  "class": "ShellSwitch"
}
@sweetpi

This comment has been minimized.

Copy link
Contributor

commented Oct 10, 2014

Any ideas? Classname is copied from devices.coffee?

SwitchActuator is just a abstract class used in plugins as super class. You can't instantiate it directly. What do you want to do?

@leader21

This comment has been minimized.

Copy link
Contributor

commented Oct 10, 2014

what purpose is your switch for?
a virtual switch without hardwaredevice can be created for eg with the following code :
systembuttons1

this creates an array of two buttons as shown here :
systembuttons

these buttons can be switched on the gui and can be executed with a shellscript or shell command.
i created a simple rule to shutdown or reboot :
IF button-shutdown is pressed THEN turn LCD backlight on and execute "sudo shutdown -h now" and display "System shutdown" on lcd line 1 for 15 seconds and display "Bye bye" on lcd line 2 for 15 seconds and turn LCD backlight off after 16 seconds
this works very well and could be used for any other switchingprocess as well.

instead of a buttonsdevice you can use the class shellswitch to create on / off instead of a pushbutton.
have a look here : http://www.pimatic.org/docs/pimatic-shell-execute/

@andig

This comment has been minimized.

Copy link
Contributor Author

commented Oct 10, 2014

SwitchActuator is just a abstract class used in plugins as super class. You can't instantiate it directly. What do you want to do?

Right now I'm just prototyping and putting pieces together. What I wanted to have was one of the demo's on/off switches that indicates its switching state.

these buttons can be switched on the gui and can be executed with a shellscript or shell command.
i created a simple rule to shutdown or reboot :

I understand the ButtonsDevice but it doesn't give me the visual feedback I'd like.

From devices.coffee it wasn't clear to me that SwitchActuator or PowerSwitch are abstract and can't be used.

Can I achieve something like the demo's on/off button the way I'm approaching it?

@sweetpi

This comment has been minimized.

Copy link
Contributor

commented Oct 10, 2014

Use @wutu s example.

@andig

This comment has been minimized.

Copy link
Contributor Author

commented Oct 11, 2014

Use @wutu s example.

Does not work on Windows as echo on/off doesn't return output. Returning something else again is not recognized as state. arrgh...

@sweetpi

This comment has been minimized.

Copy link
Contributor

commented Oct 11, 2014

I added support for Dummy devices:

{
  "class": "DummySwitch",
  "id": "dummy-switch",
  "name": "Dummy Switch"
},
{
  "class": "DummyDimmer",
  "id": "dummy-dimmer",
  "name": "Dummy Dimmer"
},
{
  "class": "DummyShutter",
  "id": "dummy-shutter",
  "name": "Dummy Shutter"
}
@koffienl

This comment has been minimized.

Copy link

commented Oct 11, 2014

Verry nice, wgoing to test it right away :)

@Icesory

This comment has been minimized.

Copy link
Contributor

commented Oct 11, 2014

I used the code sweetpi poste but it dont work for me. Must i add a special plugin or a state to the Dummys? On wich pimatic version it should work?

@Yves911

This comment has been minimized.

Copy link
Contributor

commented Oct 11, 2014

Tried to replace my shellswitch by a dummy one, but some rules fail now:

23:42:20.725 [pimatic] error: Error on evaluation of rule condition of rule manualnotautomatic: Cannot call method 'then' of undefined
23:42:20.766 [pimatic] debug: [TypeError: Cannot call method 'then' of undefined]
23:42:20.805 [pimatic] error: rule manualnotautomatic error executing an action: Cannot call method 'then' of undefined
23:42:20.884 [pimatic] debug: TypeError: Cannot call method 'then' of undefined
23:42:20.884 [pimatic] debug:>  at SwitchActionHandler._doExectuteAction (/root/pimatic-git/node_modules/pimatic/lib/actions.coffee:262:5)
23:42:20.884 [pimatic] debug:>  at SwitchActionHandler._doExectuteAction (/root/pimatic-git/node_modules/pimatic/lib/actions.coffee:1:1)
23:42:20.884 [pimatic] debug:>  at SwitchActionHandler.executeAction (/root/pimatic-git/node_modules/pimatic/lib/actions.coffee:271:51)
23:42:20.884 [pimatic] debug:>  at SwitchActionHandler.executeAction (/root/pimatic-git/node_modules/pimatic/lib/actions.coffee:1:1)
23:42:20.884 [pimatic] debug:>  at /root/pimatic-git/node_modules/pimatic/lib/rules.coffee:925:18
23:42:20.884 [pimatic] debug:>  at tryCatch1 (/root/pimatic-git/node_modules/pimatic/node_modules/bluebird/js/main/util.js:43:21)
23:42:20.884 [pimatic] debug:>  at Function.Promise$_Try (/root/pimatic-git/node_modules/pimatic/node_modules/bluebird/js/main/promise.js:233:11)
23:42:20.884 [pimatic] debug:>  at RuleManager._executeAction (/root/pimatic-git/node_modules/pimatic/lib/rules.coffee:924:24)
23:42:20.884 [pimatic] debug:>  at RuleManager._executeAction (/root/pimatic-git/node_modules/pimatic/lib/rules.coffee:1:1)
23:42:20.884 [pimatic] debug:>  at _fn (/root/pimatic-git/node_modules/pimatic/lib/rules.coffee:901:14)
23:42:20.884 [pimatic] debug:>  at RuleManager._executeRuleActions (/root/pimatic-git/node_modules/pimatic/lib/rules.coffee:907:11)
23:42:20.884 [pimatic] debug:>  at RuleManager._executeRuleActionsAndLogResult (/root/pimatic-git/node_modules/pimatic/lib/rules.coffee:851:13)
23:42:20.884 [pimatic] debug:>  at /root/pimatic-git/node_modules/pimatic/lib/rules.coffee:550:7
23:42:20.884 [pimatic] debug:>  at tryCatch1 (/root/pimatic-git/node_modules/pimatic/node_modules/bluebird/js/main/util.js:43:21)
23:42:20.884 [pimatic] debug:>  at Promise$_callHandler [as _callHandler] (/root/pimatic-git/node_modules/pimatic/node_modules/bluebird/js/main/promise.js:627:13)
23:42:20.884 [pimatic] debug:>  at Promise$_settlePromiseFromHandler [as _settlePromiseFromHandler] (/root/pimatic-git/node_modules/pimatic/node_modules/bluebird/js/main/promise.js:641:18)
23:42:20.884 [pimatic] debug:>  at Promise$_settlePromiseAt [as _settlePromiseAt] (/root/pimatic-git/node_modules/pimatic/node_modules/bluebird/js/main/promise.js:800:14)
23:42:20.884 [pimatic] debug:>  at Promise$_settlePromises [as _settlePromises] (/root/pimatic-git/node_modules/pimatic/node_modules/bluebird/js/main/promise.js:934:14)
23:42:20.884 [pimatic] debug:>  at Async$_consumeFunctionBuffer [as _consumeFunctionBuffer] (/root/pimatic-git/node_modules/pimatic/node_modules/bluebird/js/main/async.js:75:12)
23:42:20.884 [pimatic] debug:>  at Async$consumeFunctionBuffer (/root/pimatic-git/node_modules/pimatic/node_modules/bluebird/js/main/async.js:38:14)
23:42:20.884 [pimatic] debug:>  at process._tickCallback (node.js:415:13)
23:42:20.884 [pimatic] debug:>

and the rule is

"if increase button is pressed or decrease button is pressed or std19 is pressed or std17 is pressed then switch thermostat-auto off"

The rule was working fine before replacing shellswitch by dummyswitch

@sweetpi

This comment has been minimized.

Copy link
Contributor

commented Oct 12, 2014

Tried to replace my shellswitch by a dummy one, but some rules fail now:

Should now (pimatic v0.8.19) be fixed.

Keep in mind the dummy devices do currently not remember state on restart...I'm working on it. I really want to ban lastState from config and add save it into the database.

I used the code sweetpi poste but it dont work for me. Must i add a special plugin or a state to the Dummys? On wich pimatic version it should work?

pimatic v0.8.18 or later

@sweetpi

This comment has been minimized.

Copy link
Contributor

commented Oct 12, 2014

Update: In pimatic v0.8.20 the state of all dummy device gets restored to the last state on startup.

@Yves911

This comment has been minimized.

Copy link
Contributor

commented Oct 12, 2014

Tried to replace my shellswitch by a dummy one, but some rules fail now:

Should now (pimatic v0.8.19) be fixed.

Yes it is
thanks

@andig

This comment has been minimized.

Copy link
Contributor Author

commented Oct 12, 2014

@sweetpi you rock!

@sweetpi sweetpi closed this Oct 12, 2014

@georg90

This comment has been minimized.

Copy link
Contributor

commented Oct 15, 2014

When I add this to my config:

{                                                                                                                                                                                                          
    "class": "DummySwitch",                                                                                                                                                                                  
    "id": "tvonoff-switch",                                                                                                                                                                                  
    "name": "TV Remote Toggle"                                                                                                                                                                               
}, 

I get this error:

09:04:04.111 [pimatic] error: Error loading device tvonoff-switch: Cannot read property 'state' of undefined                                                                                                                                                                                                                                                            
09:04:05.984 [pimatic] error: typeof variable.value === 'number' || variable.value.length > 0

I have pimatic v0.8.20 installed :-/

@sweetpi

This comment has been minimized.

Copy link
Contributor

commented Oct 15, 2014

Do you have another device with the same id in your config?

@georg90

This comment has been minimized.

Copy link
Contributor

commented Oct 15, 2014

no, I checked that already

@sweetpi

This comment has been minimized.

Copy link
Contributor

commented Oct 15, 2014

Could you please activate debug logging (#137) and post the stack trace?

@georg90

This comment has been minimized.

Copy link
Contributor

commented Oct 15, 2014

should have done that already..

09:17:46.509 [pimatic] error: Error loading device tvonoff-switch: Cannot read property 'state' of undefined                                                                                                   
09:17:47.284 [pimatic] debug: TypeError: Cannot read property 'state' of undefined                                                                                                                             
09:17:47.284 [pimatic] debug:>  at new DummySwitch (/home/xbmc/pimatic-8/node_modules/pimatic/lib/devices.coffee:497:5)                                                                                        
09:17:47.284 [pimatic] debug:>  at Object._results.push._this.registerDeviceClass.createCallback (/home/xbmc/pimatic-8/node_modules/pimatic/lib/devices.coffee:795:7)                                          
09:17:47.284 [pimatic] debug:>  at DeviceManager._loadDevice (/home/xbmc/pimatic-8/node_modules/pimatic/lib/devices.coffee:707:18)                                                                             
09:17:47.284 [pimatic] debug:>  at /home/xbmc/pimatic-8/node_modules/pimatic/lib/devices.coffee:721:11                                                                                                         
09:17:47.284 [pimatic] debug:>  at tryCatch1 (/home/xbmc/pimatic-8/node_modules/pimatic/node_modules/bluebird/js/main/util.js:43:21)                                                                           
09:17:47.284 [pimatic] debug:>  at Promise$_callHandler [as _callHandler] (/home/xbmc/pimatic-8/node_modules/pimatic/node_modules/bluebird/js/main/promise.js:627:13)                                          
09:17:47.284 [pimatic] debug:>  at Promise$_settlePromiseFromHandler [as _settlePromiseFromHandler] (/home/xbmc/pimatic-8/node_modules/pimatic/node_modules/bluebird/js/main/promise.js:641:18)                
09:17:47.284 [pimatic] debug:>  at Promise$_settlePromiseAt [as _settlePromiseAt] (/home/xbmc/pimatic-8/node_modules/pimatic/node_modules/bluebird/js/main/promise.js:800:14)                                  
09:17:47.284 [pimatic] debug:>  at Promise$_settlePromises [as _settlePromises] (/home/xbmc/pimatic-8/node_modules/pimatic/node_modules/bluebird/js/main/promise.js:934:14)                                    
09:17:47.284 [pimatic] debug:>  at Async$_consumeFunctionBuffer [as _consumeFunctionBuffer] (/home/xbmc/pimatic-8/node_modules/pimatic/node_modules/bluebird/js/main/async.js:75:12)                           
09:17:47.284 [pimatic] debug:>  at Async$consumeFunctionBuffer (/home/xbmc/pimatic-8/node_modules/pimatic/node_modules/bluebird/js/main/async.js:38:14)                                                        
09:17:47.284 [pimatic] debug:>  at process._tickCallback (node.js:415:13)                                                                                                                                      
09:17:47.284 [pimatic] debug:>                                        

09:17:48.396 [pimatic] error: typeof variable.value === 'number' || variable.value.length > 0                                                                                                                  
09:17:49.549 [pimatic] debug: AssertionError: typeof variable.value === 'number' || variable.value.length > 0                                                                                                  
09:17:49.549 [pimatic] debug:>  at _fn (/home/xbmc/pimatic-8/node_modules/pimatic/lib/variables.coffee:175:7)                                                                                                  
09:17:49.549 [pimatic] debug:>  at VariableManager.init (/home/xbmc/pimatic-8/node_modules/pimatic/lib/variables.coffee:353:11)                                                                                
09:17:49.549 [pimatic] debug:>  at initVariables (/home/xbmc/pimatic-8/node_modules/pimatic/lib/framework.coffee:26:5)                                                                                         
09:17:49.549 [pimatic] debug:>  at tryCatch1 (/home/xbmc/pimatic-8/node_modules/pimatic/node_modules/bluebird/js/main/util.js:43:21)                                                                           
09:17:49.549 [pimatic] debug:>  at Promise$_callHandler [as _callHandler] (/home/xbmc/pimatic-8/node_modules/pimatic/node_modules/bluebird/js/main/promise.js:627:13)                                          
09:17:49.549 [pimatic] debug:>  at Promise$_settlePromiseFromHandler [as _settlePromiseFromHandler] (/home/xbmc/pimatic-8/node_modules/pimatic/node_modules/bluebird/js/main/promise.js:641:18)                
09:17:49.549 [pimatic] debug:>  at Promise$_settlePromiseAt [as _settlePromiseAt] (/home/xbmc/pimatic-8/node_modules/pimatic/node_modules/bluebird/js/main/promise.js:800:14)                                  
09:17:49.549 [pimatic] debug:>  at Promise$_settlePromises [as _settlePromises] (/home/xbmc/pimatic-8/node_modules/pimatic/node_modules/bluebird/js/main/promise.js:934:14)                                    
09:17:49.549 [pimatic] debug:>  at Async$_consumeFunctionBuffer [as _consumeFunctionBuffer] (/home/xbmc/pimatic-8/node_modules/pimatic/node_modules/bluebird/js/main/async.js:75:12)                           
09:17:49.549 [pimatic] debug:>  at Async$consumeFunctionBuffer (/home/xbmc/pimatic-8/node_modules/pimatic/node_modules/bluebird/js/main/async.js:38:14)                                                        
09:17:49.549 [pimatic] debug:>  at process._tickCallback (node.js:415:13)                                                                                                                                      
09:17:49.549 [pimatic] debug:>    

@georg90

This comment has been minimized.

Copy link
Contributor

commented Oct 15, 2014

okay, seems like the error variable.value === 'number' || variable.value.length > 0 was caused by a rule in my config..

How can I make a rule like this or how can I escape the quotes?

if it's 5pm then execute "echo "MyText" | mailx -s "something" etc..." and set $var = 1

@sweetpi sweetpi added the bug label Oct 15, 2014

@sweetpi sweetpi reopened this Oct 15, 2014

@sweetpi

This comment has been minimized.

Copy link
Contributor

commented Oct 15, 2014

How can I make a rule like this or how can I escape the quotes?

Unfortunately it's not possible, yet. Could you make a new request issue for that, so it does not get lost :)
I will look into the other error.

@sweetpi

This comment has been minimized.

Copy link
Contributor

commented Oct 15, 2014

Should be fixed in pimatic v0.8.21. Could you please test?

@georg90

This comment has been minimized.

Copy link
Contributor

commented Oct 15, 2014

working thanks! :) Will create a new issue ;-)

@andig

This comment has been minimized.

Copy link
Contributor Author

commented Oct 16, 2014

Out of curiosity for the benefit of learning: why are the promises of the dummy devices not resolved with a value return Promise.resolve()? Coffeescript feature again?

@sweetpi

This comment has been minimized.

Copy link
Contributor

commented Oct 17, 2014

Dou you want to know, why the getState: -> Promise.resolve(@_state) is missing?

@andig

This comment has been minimized.

Copy link
Contributor Author

commented Oct 17, 2014

Dou you want to know, why the getState: -> Promise.resolve(@_state) is missing?

I was confused why DummySwitch

changeStateTo: (state) ->
  @_setState(state)
  return Promise.resolve()

returns an empty promise.

On second thought this is of course the setter, not the getter. Wondering if it would be good practice to return Promise.resolve(@state) or Promise.resolve(@getState) to make the setters chainable?

@sweetpi

This comment has been minimized.

Copy link
Contributor

commented Oct 17, 2014

On second thought this is of course the setter, not the getter. Wondering if it would be good practice to return Promise.resolve(@State) or Promise.resolve(@getState) to make the setters chainable?

I had actions (/setter) return the new state in the beginning, but then decided against it, because I didn't find a scenario where it could be helpful and so it is just another thing plugin authors would have to think about.

Chainable JavaScript interfaces (like JQuery is doing it) are very cool. But I think there is no scenario where it would be so much helpful, that it makes sense that every device must concern about it.

I had a cool idea for scripting rules in JS/CS for advanced people recently. Would be really cool to have the ability to build rules like this in JS/CS:

if().device('lamp1').isTurnedOn().and().device('my-phone').isPresent().for('10 seconds')
    .then().device('lamp2').turnOn()

But I think for this we need to wrap devices into another object anyway...

btw: Very nice presentation about DSLs in JS: http://www.youtube.com/watch?v=lm4jEcnWeKI

@sebastienvg

This comment has been minimized.

Copy link
Contributor

commented Nov 23, 2014

Playing around with DummyDevices I can almost achieve what I have in mind. I am just wondering if there's a way to declare a "Dummy Contact" open/close

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.