Presence and Dusk depending rule creation

Hey.
I’m learning about rules and I am about to write a rule (as the title says), which is depending on presence and the sunset.

So i want the following to be done:
When I’m home, watching TV in the living room and the sun is down, i want the lights to get on.

To be more specific, having an eye on the rules:
When the sun is already down and i turn on my TV, which responds on a ping, then lights should be turned on.

Ok … Next step:
when
Channel ‘astro:sun:local:set#event’ triggered AND
Item LR_TV_ON changed
then
LR_lights_group.sendCommand(ON)
end

So far …
Item LR_TV_ON is liked to HTTP Binding, which pings my TV every 5 Minutes and waits 5 seconds for response. This is pretty much ok, i think, to get the stats of TV and so “know” whether I’m in the living room watching TV, while not “spamming” my LAN with ping requests.

BUT, I already see a problem here!
The rule only sends the command at exactly the point of time, when I’m watching TV AND the sunset begins. But what’s about when the sun is already down and I’m going to watch TV like an hour later? I tested … nothing happens!

Plz help me to get like a simply and working rule. Means, my first thoughts must not be the easiest way, but im stuck here …

The trigger on a rule can ONLY be OR

So you need one more item:

String TimeOfDay
rule "sunset" // If sun sets and TV is on turns the lights on
when
    Channel "astro:sun:local:set#event" triggered
then
    TimeOfDay.sendCommand("EVENING")
    if (LR_TV_ON.state == ON) {
        LR_lights_group.sendCommand(ON)
    } else {
        LR_lights_group.sendCommand(OFF)
    }
end

rule "TV on" // If the TV in turned on and it's past sunset turn on the lights
when
    LR_TV_ON changed to ON
then
    if (TimeOfDay == "EVENING") {
        LR_lights_group.sendCommand(ON)
    } else {
        LR_lights_group.sendCommand(OFF)
    }
end

rule "Sunrise"
when
    Channel "astro:sun:local:rise#event" triggered
then
    TimeOfDay.sendCommand("MORNING")
end
1 Like