Sunset and Presence

I’ve tried to create a rule to turn on the patio lights after dusk if someone is home. My presence detection is working as I can see the events in my log. Here’s my code:

rule "Turn patio on after Sunset"
when
	Channel 'astro:sun:home:nauticDusk#event' triggered START
then
	if (gPresence == ON)
	{
		sendCommand(gPatio, ON)
		sendCommand(gOutdoors, ON)
	} else {
		logInfo("Patio", "Patio - No one home")
		sendCommand(gOutdoors, ON)
	}
end

The rule never seems to pass the gPresense == on test. So only the gOutdoors are turned on.

gOutdoors ON is in both statements but if gPresence is on then gPatio should also turn on. If your sure that gPresence is working and using the correct state, then try a restart or two of OH. If not, try using the group state inside the if statement e.g. if (gPresence.state == ON)

1 Like

Try adding a log entry to the beginning fo the rule so that you can see the state of gPresence…

rule "Turn patio on after Sunset"
when
	Channel 'astro:sun:home:nauticDusk#event' triggered START
then
    logInfo("Patio", "Start: gPresence [{}]", gPresence.state)
    if (gPresence.state == ON)	{
        sendCommand(gPatio, ON)
	sendCommand(gOutdoors, ON)
    } else {
        logInfo("Patio", "Patio - No one home")
	sendCommand(gOutdoors, ON)
    }
end

gPresence is an item. You don’t want to compare the item, you want to compare the state of the item, so you have to use if (gPresence.state == ON) and you will end up with this:

rule "Turn patio on after Sunset"
when
	Channel 'astro:sun:home:nauticDusk#event' triggered START
then
	if (gPresence.state == ON)
	{
		gPatio.sendCommand(ON)
	} else {
		logInfo("Patio", "Patio - No one home")
	}
	gOutdoors.sendCommand(ON)
end

Some additional things:

  • In your case you send the command ON in both cases, so you don’t need to include that in the if/else statement
  • Don’t use the function “sendCommand”, use the method of the item. So gPatio.sendCommand(ON) instead of sendCommand(gPatio, ON), because you might run into conversion issues with it (it’s explained in detail somewhere in the community).
  • This rule will only run when the nauticDusk-event is fired and therefore only run once. So the light will only turn on if someone is home at that time, but not if someone comes home after that. I’m not sure if that is intended.
1 Like

Nice catch… I missed that and updated the example in my post.

In the past, I’ve recommended against using a BusEvent action too, since it is less forgiving. Things have changed though and the new rule engine uses only actions, so we should be preparing for that transition.

Thanks I’ve updated my rule accordingly and will let you know tonight what happens. Reply to Dave_K I have another rule that turns on the patio lights when one of the perimeter doors open.

One other gotcha t watch out for which may be counter to your intent is that this Rule only triggers once at nautical dusk. If you are not present when that event occurs the lights will not turn on. If you return after that event, your lights will not turn on either.

If you want the lights to turn on between nautical dusk and some other time, you will need to change the rule up a bit. Trigger the Rule by changes in gPresence in addition to the astro events. Then you need to check both whether gPresence is ON and whether the current time is between the times you want the lights to remain ON.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.