"disable" switch item

Hello,

I have a stupid question but I can’t figure it out: How can I “disable” a
switch? Here are 2 rules for a same item “all_lights” for example:

rule "all_lights-ON"
when
Item all_lights changed to ON
then
light1.send(ON)
light2.send(ON)
light3.send(ON)
end

rule "all_lights-OFF"
when
Item all_lights changed to OFF
then
light1.send(OFF)
light2.send(OFF)
light3.send(OFF)
end

Now, I’d like the switch item “all_Lights” to be “disabled” if another switch item “authorized_all_lights” is not equal to ON.

So when authorized_all_lights" is not equal to ON:

  • changing all_lights from OFF to ON wouldn’t execute any code and would set back all_lights to its previous state (OFF)

  • changing all_lights from ON to OFF wouldn’t execute any code and would set back all_lights to its previous state (ON)

I’ve thought about an IF function but I can’t figure out how to avoid an infinite loop (when setting back to the previous state)!

Thanks for any help!

One approach could be to use the visibility setting in the sitemap:

Switch item=all_lights label="All Lights" visibility=[authorized_all_lights==ON]
Text item=all_lights label="All Lights [%s]" visibility=[authorized_all_lights!=ON]

So when the authorized_all_lights item is ON, you see a switch, otherwise you just see the string version of the current value.

1 Like

Another approach ist to define a switch that is not bound to an physical item. and use this switch as a master switch. If it is on your rules are working if not nothing happens. Within the rule you can do it like

rule "all_lights-ON"
when
Item all_lights changed to ON
then
  if (master.state == ON) then {
    light1.send(ON)
    light2.send(ON)
    light3.send(ON) }
 else {
   potsupdate(allLigths, OFF)

}
end

The first part of the if checks if the master ist on and if the ligths shpuld be switched on and the second part (else) resets the allLigths switch to off, because you switches it on und the master switch said no way to switch the lights oh.

Thomas

@Dibbler42’s strategy is good.

I personally like to use a suffix _PROXY for these “fake” items so it stands out in Items files and rules.
All my real switches and dimmers have _PROXY “twins”. This lets me sendCommand to the _PROXY and intercept the it in rules to do secondary checking. See here for this in practice integrating switches and timers.

1 Like

Thank you VERY much for your quick and great answers! A few comments:

Watou’s idea is a nice and easy approach I didn’t know but I see 2 problems:

1- It doesn’t avoid another item to change all_lights. But a workaround
would be to add an “IF authorized_all_lights == ON” condition on all
items that can change all_lights. Or is there a better idea?

2- I am afraid all_lights can be switched ON/OFF even after
authorized_all_lights has changed to OFF if the user doesn’t refresh the
interface (many items are refreshed with a delay on the web interface
or HABDroid). Any workaround?

Dibbler42’s idea is what I tried even before posting this thread. The
problem is all_lights is only protected from changes from OFF to ON (but
not from ON to OFF). Any workaround?

bob_dickenson, I tried your “_PROXY” twin idea:

rule "all_lights"
when
Item all_lights received command
then
if (all_lights_PROXY.state != ON){
if (authorized_all_lights == ON){
... do something
} else {
//we revert to the old state    
all_lights_PROXY.send(ON)
    if (receivedCommand == ON){
        all_lights.send(OFF)
    }
    if (receivedCommand == OFF){
        all_lights.send(ON)
    }
    all_lights_PROXY.send(OFF)
}
}
end

So basically, when authorized_all_lights is OFF, I just do:

  • disable the rule for all_lights for a moment
  • switch all_lights to its previous state
  • enable the rule for all_lights

It does work but it’s a pity I’ll have to do that for many items. Isn’t there a prettier way?

I am not sure if i get all the points. So here are some ideas

1.) Create a group with all ligth switches, and thenuse this group. You only need one command to witch alle the ligths an on command for each ligth

2.) You should be able to create different rule. One for the on to off change an one for the off to on change, Could be done by Item xyz changed from ON to OFF

Thomas

Thanks Dibbler42, my point is to “disable” an item so it can’t be used by anybody (for example during night time). I took some lights example to make it easier but this virtual item is in fact the position of a step relay (ON or OFF). The status of this item is changed from the Webinterface/HABdroid AND a physical switch. So when it’s turned ON, it turns a step relay ON and viceversa (Openhab->MQTT->Arduino->relay board->step relay). Unfortunately, I can’t read the status of the step relay, so if I disable the action of this item during night but someone hits the physical switch, the virtual switch will be ON but the step relay will have remained OFF, thus they are not synchronized anymore.

It can be a bit wordy, that is true. But it does give you the ability to intercept the command at execution time. If there are any timers or multiple layers of control involved this is a pretty critical feature.

With regard to your question on the interface updating delay. This can be a problem. I solved in my earlier referenced post with “yet another rule”. This keeps the PROXY UI element in sync (in case someone operates the physical switch for example). postUpdate() is pretty fast at hitting the UI.

rule "PXY_SYNCH: Update Kitchen PROXY state" when Item sw_LIGHT_Kitchen changed then postUpdate(sw_LIGHT_Kitchen_PROXY,sw_LIGHT_Kitchen.state) end

In my NN years of professional work, the general order of priority is:

  1. First make it RIGHT.
  2. Then make it MAINTAINABLE.
  3. Then make it RELIABLE
  4. Then make it FAST.
  5. Then make it PRETTY.

I’ll stick with that priority order.

1 Like

I had a similar problem to overcome in my rules. I have a rule that turns on the lights during the day when the weather code says its cloudy. However, if someone manually turns on or off a light it disables the weather rule for that light. I use groups and a hashmap that keeps track of whether a light has been overridden or not and a lambda that checks whether a rule has been overridden before turning it back on. I posted my full lighting rules here.

Rich

I’ve just come across this post and I’m also trying to work out how to disable my motion rule using a dummy switch. My current rule is below, which is fine for switching on the light when I enter a room, but if I’m in the room for a while I want to manually over ride and stop to light either switching on or switching off. I have created a dummy switch, but I’m just not sure how to incorpate it into the rule below? can you help?

Switch hue_bed_dummy "Bedroom Manual Switch" (Bedroom)

   rule "hallLightOn"
    when   
        Item hall_motion_raw changed 
    then   
    if((hall_motion_raw.state as DecimalType) ==1) {
        sendCommand(Toggle_2,ON)    
        } 
    end

   rule "hallLightOff"
    when   
            Time cron "0 * * * * ?"
    then   
           if((hall_motion_raw.state as DecimalType) ==0) {
        sendCommand(Toggle_2,OFF)  
            }
    end

The quickest and easiest approach is to have a Switch like you describe, put it on your sitemap. In your rule just check the state of your override switch before doing anything.

if((hall_motion_raw.state as DecimalType) == 1 && hue_bed_dummy.state != ON) {
```

A more automated but far more complicated way can be to use something like I posted [here.](https://community.openhab.org/t/automation-orchestration-design-patterns/1967/20)

tnx, in visibility, can I have two item?
I want to disable a switch when first and second item are ON

Yes, just separate the two comparisons with a comma.

Documented here

http://docs.openhab.org/configuration/sitemaps.html#visibility

Unforunately, you can’t do that though. The multiple conditions are treats as or, not and.

To get this behavior you will have to create an Unbound Item Switch, do the comparison in a Rule, and use this Unbound Item to drive the visibility.

1 Like