In fact, there is no Status TOGGLE in openHAB at all, switch-States are only ON|OFF|uninizialized, so if you want to toggle a switch, you have to use a rule for it:
rule "toggle"
when
Item toggler received command
then
if (myitem.state!=ON) // item OFF or uninitialized
myitem.sendCommand(ON)
else
myitem.sendCommand(OFF)
end
rules are not good option for swiches, because work with big delay some times more than 1 minute.
In github write “TOGGLE changes the current status of a switch item from ON to OFF or from OFF to ON.”
As @Udo_Hartmann mentioned, there is no command or state for toggle. If you don’t want to use a rule, you could implement two switches on the UI, one with a mapping for ON and one with a mapping for OFF and have the visibility of the switches be dependent on the item status.
Something like this (haven’t syntax checked but should be something similar):
This means that only one switch will show depending on the status of the Relay2 item, they will fire the appropriate command (ON or OFF) but the switch text will always say “Toggle”.
this is a verry good idea, but works only in web interface, in habdroid apk sitemaps are not dinamic… or something like halfdinamic refreshing pages only when enter them.
Rules should be triggered immediately, there is (normally) no delay. If there is a big delay (>>0.1sec) there is definitly a problem in Configuration. Maybe there are unconfigured bindings in addons.
I deleate some not used things in addons folder and now is better rules are triggered in ~2sek
strange things is when rule have to be triggered for furst time takes 5-10sek or not triggers at all, after that no problem time is 2 sek
I ran into the same problem with TOGGLE for my lights. I read a few months ago (and I’m paraphrasing) that the goal is to make/keep openhab deterministic with every action. Therefore, the system should know what the final state of an item will be before the action is taken. In the case of an uninitialized switch, it has no state (neither off nor on) so toggle is not supported until it is in one of those states.
In my case, I was controlling the switch via the REST interface, so I did three steps:
Trigger a TOGGLE
Query the state. If uninitialized, go to 3, else break.
Trigger an ON
This fixes the problem, but it’s kind of clunky. Thinking about it now, what you could do is set the state in a rule that fires on system start (I don’t remember the syntax right now, but I can check later if you don’t know.) To keep with my logic flow, I would set the state to OFF. Note that I don’t mean trigger a state change, or your switch will go off whenever the system starts. Just set the state of the switch, which should then allow TOGGLE to work.
Hope this helps. I’m going to change my configuration to work this way, which will simplify my REST logic.
You could use mapdb to persist every state in openHAB. In combination with restoreOnStartup that would eliminate uninitialized items. mapdb is built to only give the last state, so there is only a small amount of disk memory needed.
Downside is, this works only if openHAB is restarting immediately after shutdown, so that there are no state updates lost.