Script only active in the evening

I have a script that switches on a dimmer when an exterior light sensor reports a certain value. Obviously this value is reached once in the evening but also in the morning. How can I make it so that this rule is only active in the evening?

the rule:
rule "LivingLightOnWhenDark"
when
Item Lum_Sensor changed
then
if(Lum_Sensor.state < 500 && Lum_Sensor.state > 300){
logInfo(“Staanlamp”, “Auto On”)
sendCommand(Staanlamp, 50)
}
end

I am surely no scripting expert, but isn’t it possible to do something like:

when
Item Lum_Sensor changed AND “get.timeofday > 18”

I don’t know the exact syntax of this command, but you could combine these two for getting the rule executed only in the evening.

No that would not work. Rule triggers are event based, not state based so it makes no sense to use AND in a rule trigger. Those two events will never occur at the same time (note the event for “get.timeofday > 18” would only occur one second after 17:59).

See this posting for how I handle this.

The tl;dr of it is I have a String Item that gets set to a string representing the current time of day (Morning, Day, Evening, Night) and rules that change this Item based on times and/or Astro events. For example, Evening starts 90 minutes before sunset as calculated by Astro and ends at 11:00 pm.

In the rest of my rules, when I need to do something at the time of day change I trigger on updates to my TimeOfDay Item. In other rules that care about what the time of day is to determine behavior (e.g. you only want to run your logic during one period of day) I check to see if the TimeOfDay is currently set to the right time.

So, assuming you implemented my rule like this your rule would become:

rule "LivingLightOnWhenDark"
when
    Item Lum_Sensor changed
then
    if(TimeOfDay.state.toString == "Evening") {
        if(Lum_Sensor.state < 500 && Lum_Sensor.state > 300){
            logInfo("Staanlamp", "Auto On") 
            sendCommand(Staanlamp, 50)
        }
    }
end

If you think your system will eventually grow into needing other rules that care about the time of day I highly recommend this approach. However, if you think you will only ever care about evening in this one rule, your rule would look like this (assuming your evening starts at 7:00 pm and ends at 10:00 pm:

rule "LivingLightOnWhenDark"
when
    Item Lum_Sensor changed
then
    val eveningStart = now.withTimeAtStartOfDay.plusHours(19).millis
    val eveningEnd = now.withTimeAtStartOfDay.plusHours(22).millis
    if(now.millis > eveningStart && now.millis < eveningEnd) {
        if(Lum_Sensor.state < 500 && Lum_Sensor.state > 300){
            logInfo("Staanlamp", "Auto On") 
            sendCommand(Staanlamp, 50)
        }
    }
end

See my posting above to see how to incorporate Astro calculated sunset into figuring out when evening starts.

awesome, thanks for the help!