Turning off devices based on Presence

Dear Everyone!

I want to have a rule that is toggled when everyone left from home.
I have already set it up (I have a String which sends “No one” when nobody is at home)

rule "Leaving Home"
when
	Item Everyone_Home changed to "No one"
then
	sendCommand(CurrentActivityHub, "PowerOff")
end 

My only problem is that this will instantly turn off the devices when No one is present. I want to have a little timeout when it receives this command and after that recheck is this condition is still present (No one at home) and then do their things…

Can anyone help with that?

Thanks

Install the expire binding
Create a new item:

Switch Everyone_Home_Timer { expire="5m, command=OFF" }

Your rules:

ule "Leaving Home"
when
    Item Everyone_Home changed to "No one"
then
    Everyone_Home_Timer.sendCommand(ON)
end 

rule "Everyone Home Timer"
when
    Item Everyone_Home_Timer received command OFF
then
    if (Everyone_Home.state.toString == "No one") {
        CurrentActivityHub.sendCommand("PowerOff")
    }
end
1 Like

Does this work now? What version of OH are you running? I’ve a canary rule set up in my setup that is supposed to tell me which this gets fixed and it has never fired. I’m on 2.2 release.

At least up to 2.2 release, only enum types (e.g. ON/OFF, OPEN/CLOSED, etc) can be used with changed to or received command or received update.

In answer to your original question: Generic Presence Detection

which is basically what Vincent recommends with some more explanation.

For this you have to add a second rule, just to Keep the “Everyone_Home_Timer” on ON IF someone is home:

rule "someone home"
when
	Item Everyone_Home received command "everybody" // or whatever your trigger is
then
	Everyone_Home_Timer.sendCommand(ON)
end 

hint: I assume, your presence will constantly get updated with a faster interval than 5mins (which is the expire-time in Vincent’s Switch-item.

note: please don’t use the Syntax sendCommand(CurrentActivityHub, "PowerOff") anymore, it’s deprecated, please use instead CurrentActivityHub.sendCommand("PowerOff") - as Vincent used.

The advice is correct but the reason is not. The Actions are not deprecated and will stay around for the long-term. At least I hope so because there are a couple of really good use cases for them. We do discourage their use when you have you know apriori what Item you have because of everything raised here.

But if you are using Design Pattern: Associated Items or some other approach where you are generating the name of an Item to send a command to based on some other criteria, the Actions are the best way to do it since they don’t require you to add the Items to a Group and use the findFirst trick to get a handle on them.

I really hope the Actions do not go away.

1 Like

Thanks for everyone, and I got the advice!
I’ll try this out

I don’t know if this work or not, never tried. Are there any alternatives for this? Because my Item for which gets updated depending on the persons home or not is a single String… so I couldn’t use it as a switch…

Well, that is a separate question but since you bring it up my question back is why is it a String. I would expect Everyone_Home to have one two possible states. Any Item that can have only two states would properly be represented in OH as either a Switch or as a Contact.

Yes, but it has 3 state (Everyone, Someone, No one), mainly because when I created this item and rule I didn’t know what I’ll use this for… Now I think it would be better if it is just a simple switch…

How are you detecting individual presence, and do you have individual items for each person’s presence? Is so, put them in a group with states (this assumes they are switches)…

Group:Switch:OR(ON,OFF)    gPhone    "Phones [%s]"    <present>

… then trigger the rule with the group and remove Everyone_Home, just like in Rich’s Generic Presence Detection.

Never thought of that! Thank you very much for your help!
Ps.: So if I understand it correctly, the group switch will become on if someone is at home and only be off when no one is at home?

You got it. Check out the example (although I just realized I use the group differently than in his example, but logically the same).

Thanks I used your example. I’ll try this out!

I have almost forget about this rule. However it seems that something is not working. I have added the Group and added the phones to these groups. I can see that these switches are OK, they change their value right after the individual “Home Or Away” switch changes on the phones.

So something most be wrong with the rules:

rule "Leaving Home"
when
	Item gPhone received update
then
	if(gPhone.state == OFF) {
	logInfo("leavehome.rules", "Received commands:" + receivedCommand)
	Everyone_Home_Timer.sendCommand(ON)
	}
end

rule "Everyone Home Timer"
when
	Item Everyone_Home_Timer received command OFF
then
	if(gPhone.state == OFF && Guest_Mode.state == OFF) {
		CurrentActivityHub.sendCommand("PowerOff")
	}
end

rule "Someone Home"
when
	Item gPhone received update
then
	if(gPhone.state == ON {
		Everyone_Home_Timer.sendCommand(ON)
	}
end

This seems that is not working at all, because I never saw in the log that it triggered the expire switch and also the switch never changes… (I have added for a sitemap to test this rule…)

What can be wrong?

Thanks

In rules with a received update trigger, the implicit variable receivedCommand will not be available. I would expect that you have errors in your logs from this, and it is likely stopping your rule at the line with logInfo.

If you add the Guest_Mode item to gPhone, it will act as another presence, and keep gPhone ON when it is turned on. These rules may work for you, but ask if you need some explanation…

rule "Presence changed"
when
    Item gPhone changed
then
    logInfo("leavehome.rules", "Home presence change: gPhone changed to [{}]",gPhone.state.toString)
    if (gPhone.state == ON) {// everyone was away but now someone is home
        Everyone_Home_Timer.postUpdate(OFF) // in case it was running, cancel the timer
        // put anything else in here that you would like to occur when presence changes from Away to Home
    }
    else if (gPhone.state == OFF) {// someone was Home but now everyone is away
        Everyone_Home_Timer.sendCommand(ON) // start the timer
    }
end

rule "Everyone_Home_Timer expired"
when
    Item Everyone_Home_Timer received command OFF
then
    logInfo("leavehome.rules", "Everyone_Home_Timer expired")
    CurrentActivityHub.sendCommand("PowerOff")
end

I use something similar, but I have a String item that represents Presence (Home, Away, Party, Sleep). I then use that item in rules. Like when motion is detected while Presence.state == “Away”, the sirens go off.

Thanks, I’ll try that! Now I’m just using for this one item and testing it, but I want to add things like turn on alarm, etc… to this!

Thanks