Struggling with turning off the lights in my rules

I’m working on some rules which turn on my lights automatically when the sun is under and i’m at home. But i’m struggling with going to bed, because when i turned off the lights when i’m off to bed the script will turn them back on. Do you guys have any ideas to make it work? Below the rule as i’ve set it right now.

rule “Zon is ondergegaan”
when
Item Sun_Elevation received update
then
if (Sun_Elevation.state < 15) {
logInfo(“Zon”, “De zon is onder!”)
ZonisOnder.sendCommand(ON)
}
else if ( Sun_Elevation.state > 16 ) {
logInfo(“Zon”, “De zon is nog niet onder!”)
ZonisOnder.sendCommand(OFF)
}
else logInfo(“Zon”, “Kaput”)
end

rule “Lampen aan als er iemand thuis is en de zon is onder”
when
Item ZonisOnder received update
then
if (Iemand_Thuis.state == ON && ZonisOnder.state == ON ) {
logInfo(“Lampen”, “De zon is onder & er is iemand thuis dus de lampen gaan aan!”)
LampbijdeTV_Switch.sendCommand(ON)
LampbijdeBank_Switch.sendCommand(ON)
LampbijhetRaam_Switch.sendCommand(ON)
}
end

rule “Lampen aan als er iemand thuis komt en de zon is onder”
when
Item Iemand_Thuis received update
then
if (Iemand_Thuis.state == ON && ZonisOnder.state == ON ) {
logInfo(“Lampen”, “De zon is onder & er is iemand thuis dus de lampen gaan aan!”)
LampbijdeTV_Switch.sendCommand(ON)
LampbijdeBank_Switch.sendCommand(ON)
LampbijhetRaam_Switch.sendCommand(ON)
}
end

You need to define your needs. For example, I see a rule that turns on lights everytime the astro binding updates the sun position AND it is below the horizon. It’s just doing what it’s told.

You could instead have a rule that turned on the lights at sunset. Astro provides sunset events you could use, but the point is that here you don’t really care about the sun’s position, only when it passes the horizon.

To put more simply -
Don’t use “It is dark” to trigger a rule. use “It has become dark”.

I’m not sure what the other triggers and rules are about, but the same approach might help - look for changes and events, not simple conditions.

Yeah, but the point is that it does not need to turn the lights on at sunset. It needs to turn the lights on at sunset, when i’m home. But when i go to bed, it should not turn the lights back on…

That’s fine, you trigger a rule at sunset, within the rule it looks to see if “at home” and if so. turn on light, if not - don’t do anything.

(The next design choice you’d need to make is - what happens if I get home after dark? :wink: )

My suggestion would not turn the lights back on until the next sunset.

My suggestion would be a problem if you go to bed before sunset. You’d need some means of detecting if you are in bed, else it will turn on lights unwantedly at sunset (assuming you are still “at home” while in bed).

Turning it on once in a ‘sunset’ would be the right solution i think… but not sure how to achieve that…

A small suggestion: You don’t need two almost identical rules, you can simply use multiple triggers:

rule "Lampen aan als er iemand thuis is en de zon is onder"
when
    Item ZonisOnder changed or
    Item Iemand_Thuis changed
then
    if (Iemand_Thuis.state == ON && ZonisOnder.state == ON ) {
        logInfo("Lampen", "De zon is onder & er is iemand thuis dus de lampen gaan aan!")
        LampbijdeTV_Switch.sendCommand(ON)
        LampbijdeBank_Switch.sendCommand(ON)
        LampbijhetRaam_Switch.sendCommand(ON)
    }
end

Please also realize the slightly changed trigger. Imho there is no need to retrigger the rule on each update.

1 Like

Thanks for the suggestion, already tried the above but i see i forgot the “or”…

So i’ve been trying to make it work for a few days now… but i don’t really get the way to make it only be triggered once in the evening. Can you help me out?

May we see the rule triggers at least

Don’t know what you mean, the rules are still as those above, because i kept getting errors in the log when saving my rules file :wink:

Okay, assume you have done nothing because you are unable to trigger a rule at sunset,
There’s a sunrise example rule in the Astro docs, you would want the sunset instead

    Channel 'astro:sun:yourLocation:set#event' triggered START

rule “Lampen aan als er iemand thuis is en de zon is onder”
when
Channel ‘astro:sun:local:set#event’ triggered START and
Item Iemand_Thuis changed
then
if (Iemand_Thuis.state == ON && alstro:sun:local:set#event == START ) {
logInfo(“Lampen”, “De zon is onder & er is iemand thuis dus de lampen gaan aan!”)
LampbijdeTV_Switch.sendCommand(ON)
LampbijdeBank_Switch.sendCommand(ON)
LampbijhetRaam_Switch.sendCommand(ON)
}
end

You can’t do and in rule triggers. What you are asking for is two events to happen at the exact same moment.

You can’t do that either, you are asking if some condition is true AND some instantaneous event is happening, just at the moment that you are asking.

But I can see what you are trying to do, so let’s rearrange it

rule "Lampen aan als er iemand thuis is en de zon is onder"
when
   Channel "astro:sun:local:set#event" triggered START or
   Item Iemand_Thuis changed to ON
then

So, this will run the rule at sunset (which occurs once each day)
and it will also run the rule whenever Iemand_Thuis changes to ON (I’m guessing this is your ‘presence’ indication - and that this rule doesn’t care when it goes OFF)

This gets the rule to run at the times you are interested - when it’s sunset, when someone comes home.

But that’s not enough on its own, we don’t want to turn lights on at sunset unless there is someone home. We can tell this by looking at Iemand_Thuis (I think?)

Also, we don’t want to turn on lights when someone comes home unless it is dark. Ah, now there’s a problem - how do we tell if it is dark?

Let’s make a new Item to show if it is dark outside or not

Switch Daylight "it is light outside"

We could just turn that on or off using rules triggered by sunset and sunrise events.
But, complication. If we reboot openHAB or reset Items by editing, this Item wouldn’t get set until the next sunrise/sunset event.

Let’s take a simple version of Rick’s Time of Day Design Pattern just for the daylight part.

items

Switch Daylight "it is light outside"
DateTime Sunrise_Time "Sunrise [%1$tH:%1$tM]" { channel="astro:sun:local:rise#start" }
DateTime Sunset_Time "Sunset [%1$tH:%1$tM]"  { channel="astro:sun:local:set#end" }

and some rules

rule "Calculate daylight state anytime" 
when
   System started or // run at system start
   Item Sunrise_Time updated or  // run when Astro updates at midnight
   Item Sunset_Time updated
then
      // make sure both are valid
   if (Sunrise_Time.state != NULL && Sunset_Time.state != NULL) {
      val day_start = new DateTime(Sunrise_Time.state.toString) // convert to joda time
      val night_start = new DateTime(Sunset_Time.state.toString)
      if (now.isAfter(day_start) && now.isBefore(night_start) {
         Daylight.postUpdate(ON)
      } else {
         Daylight.postUpdate(OFF)
      }
   }
end

rule "sunrise"
when
    Channel 'astro:sun:local:rise#event' triggered START
then
   Daylight.postUpdate(ON)
end

rule "sunset"
when
    Channel 'astro:sun:local:set#event' triggered END
then
   Daylight.postUpdate(OFF)
end

That seems a lot of work, but we now have a day/night switch you can use in many rules.

So, back to your original requirement.
Trigger a rule as it becomes dark OR when someone comes home
Then let’s make sure it is dark before turning on lights (you may have come in daylight)
AND
let’s make sure someone is at home (sunset may happen when no-one is at home)

rule "Lampen aan als er iemand thuis is en de zon is onder"
when
   Item Daylight changed to OFF or
   Item Iemand_Thuis changed to ON
then
   if (Daylight.state == OFF && Iemand_Thuis.state == ON) {
       // turn your lights on
   }
end
3 Likes

Thanks for the effort in this one!
Just made the rules, so i’ll be testing it later this evening :slight_smile:

A few small errors which i had to fix:

Item Sunrise_Time updated (Updated needs to be changed)
Item Sunset_Time updated (Updated needs to be changed)

if (now.isAfter(day_start) && now.isBefore(night_start) { = if (now.isAfter(day_start) && now.isBefore(night_start)) {

1 Like

Yup, should be received update really.

I’m not sure how well all this works in the Arctic circle. Depends what Astro does to rise/set times. If they go to UNDEF we could add testing to avoid errors, but that doesn’t solve the issue of deciding if it is iight or dark at system boot. Maybe this is where azimuth checking - EDIT aargh, elevation checking - does come in useful.
Might have a play with that.

Works quite well :slight_smile: Only thing i’ll be looking into is the fact that the lights need to on a bit earlier (30 minutes before sunset) or something.

Use offset in your Astro things, allows to add a bit before or after to individual events and states.
In this case you’d need to offset sun rise and set events for ordinary use, and also perhaps rise and set times for the startup calculation.