A toggle switch is turning two lights on / off. All is configured within the OH3-UI. It works stable. But the rule that triggers the script looks “awful”. Do you have a more elegant solution?
Thanks for any hints.
A Toggle Switch is connected to a fibaro ZWAVE dimmer (S2). This THING is only used to deliver the position of the Toggle Switch. The fibaro ZWAVE dimmer has the CHANNEL Scene Number. It provides these numbers based on the interactions with the Toggle Switch: 20 (position 1), 21 (position 2), 24 (fast double push), 25 (fast triple push) and null.
The rule needs to track when the Scene Number changes from one number to another.
All simpler approaches lead into uncontrolled switching of the light (e.g. switching the light whenever openhab restarts.):
A simple “update” of the state of the Scene Number, or
The “from state” and “to state” sections of the trigger dialog are optional. You just want just one “changed” without any specifier. Then your rule will trigger not on every update from the device (that’s “item updated”) but only when an update from the device causes the item state to change. Since you explicitly test for the all specific states in the rule script that you care about, it shouldn’t really matter what the state changes to or changes from.
There is no reason why the rule won’t run without the from state and to state sections. In fact, it should run more often (and just not produce any results based on your script); the presence of those parts of the rule definition pare down the possible triggers. Any change from, for example, 21 to 24 is also just a change.
If you are not seeing the results you expect when you just use the bare item changed trigger, then there is something else going on:
Do you see any errors in the logs when this rule runs?
Add some logging to the top of your rule so you know for sure it is being called or not. Perhaps the rule is running and just not executing the statements you expect.
When you test it with the item changed trigger, check your log file. Make sure the log says Item Bedroom_Scene changed from X to Y If there’s no such log line then there’s no event to trigger the rule.
Your recommended trigger works in principle (I don’t know what I messed up in the first attempt). Unfortunately the initial problem persists, which triggers this rule whenever openhab restarts. This is because this scene changes from “NULL” to whichever state the item has. You find this also in the “event.logs”:
2022-04-27 13:34:03.375 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Bedroom_Scene' changed from NULL to 20
I found a simpler way to build the trigger, since I’m aware that the “state” and “previousState” is optional. Even more elegant would be a solution that excludes / filters “previousState” that are NULL. Is this possible after all? Could you help me with the syntax?
This problem can be dealt with independent of the rule if you want. This is exactly what the the persistence feature of Restore On Startup is for. You can check out persistence setup and restoreOnStartup in the persistence docs (I personally use mapDB for this sort of thing).
Yes, handling the NULL state within the rule is the other perfectly viable option and there are two easy ways to do this.