What's the best way to check multiple switches to set one master switch? Occupancy

I’m using IFTTT to monitor my phone’s location and wifi to determine if I’m home or not. I do the same thing with my wife’s phone. I have four switches that are toggled based on if we are connected to the home wifi or if our location is within the geofence. These switches are dinki_wifi, dinki_geofence, wife_wifi, wife_geofence. All are members of the ‘Home’ group.

Can someone tell me the best way to use those switch states to toggle a master switch ‘Someone is home’? Is there an easy way to do this or do I have to set up a rule that monitors when any of the switches toggle on/off? Can I monitor a change of any member of a group and then do a big if statement that checks to see if any of the switches are on so toggle the master switch on and check the opposite to see if all switches are off and toggle the master switch off?

Think about groups. Put all switches in a Group


Thanks… I do have them in a group. So something like:

Group:Number:MAX Home "Someone is home [(%d)]"(All)

If any of the switches is on I would assume that would make the value here a 1 and then I can use a rule to do what I wanted to do with the master switch using this group instead, right?

To answer my own question, yes, this does work. I had not realized that groups were handled as regular items, so it was easy to do something like:

rule "Auto Disarm Home"
    Item Home changed to 1
	if(Alarm_Home.state == ON)
		sendCommand(Alarm_Home, OFF)

Extremely cool!

Since they are all switches you can also:

// ON if any one Switch is ON
Group:Switch:AND(OFF,ON) Home ...
Group:Switch:OR(ON, OFF) Home ...

// ON if all Switches are ON
Group:Switch:AND(ON, OFF) Home ...
Group:Switch:OR(OFF, ON) Home ...

NOTE: I provide two examples for each, you don’t need both the AND and the OR.

1 Like

Thanks for this. I’m trying to follow but I’m not certain that I do. Could you humor me and post how to check if all switches are off using the style you posted?

Per the wiki:

AND(value1, value2) This does a logical ‘and’ operation. Only if all items are of ‘value1’ this is returned, otherwise the ‘value2’ is returned.

OR(value1, value2) Does a logical ‘or’ operation. If at least one item is of ‘value1’ this is returned, otherwise the ‘value2’ is returned.

So a Switch is treated as a Boolean and so:

AND(OFF,ON) - if all Switches are OFF, the Group’s state will be OFF. Otherwise (i.e. at least one Switch is ON) the Group’s state will be ON

OR(ON, OFF) - if at least one Switch is ON, the Group’s state will be ON. Otherwise (i.e. all Switches are OFF) the Group’s state will be OFF

It is two ways to test for the same thing, one with an AND and one with an OR. The big trick is to realize that the first value in the parens is the state that is being ANDed or ORed and therefore it isn’t treating ON and true and OFF as false by default; you specify what it considers to be true. I don’t know if that makes sense.

Another way to think of it is it counts the number of Items that are the first value.

When doing an AND and that count equals the number of Items the Group’s state is set to the first value, otherwise it is set to the second value.

When doing an OR and that count is equal to one or more then the Group’s state is set to the second value, otherwise it is set to the second value.

So whether you use the AND or the OR example from above, the state of the Group will be set to ON if one or more of the member switches are ON. And since you can do anything with a Group that you can do with an Item (i.e. trigger a rule, check its state, send it a command) you can:

  • put it on your sitemap as a Switch and control all of the lights with one switch
  • trigger a rule**
  • check its state in a Rule (e.g. if(Home.state == ON){)

** A note about triggering a rule with a Group. The state of a Group is calculated in steps so if you trigger a rule when the Group gets updated the rule will be triggered multiple times. However, if you trigger the rule only when the Group’s state changes be aware that the Group will only change state based on the AND and OR rules above. For example, when all the Switches are OFF and one turns ON the Group will change state to ON and the rule will trigger. However, when the second Item turns ON the Group is already ON so it doesn’t change state and the rule doesn’t fire.

1 Like

This is what I needed to see. Thanks for taking the time to show me this.