More efficient method for rule

Hi,

I have a motion sensor that may be triggered often during the day. I only want it to turn on a light in the evening which I’ve set as before 8am and after 7pm. I’ve written a rule but I think the rule will continually fire during the day. Is this efficient or is there a better way?

    rule "Studio motion"

    when
        Item StudioSensor received update ON
    then

    if(now.getMinuteOfDay < 8 * 60 || now.getMinuteOfDay > 19 * 60){
        StudioLight.sendCommand(ON)}
    end

That’s fine, it costs nothing. There is little overhead there.
A now.getHourOfDay method also exists.

Thanks Rossko,

great, I will probably go with it then seeing as its taken some time to get this far for a noob! I did think about a dummy switch that turned off at 8am and on at 7pm and then use it to fire the rule.

Have amended my rule with your suggestion

 rule "Studio motion"

when
    Item StudioSensor received update ON
then

if(now.getHourOfDay < 8 || now.getHourOfDay > 19){
    StudioLight.sendCommand(ON)}
end

Take a look at the time of day design pattern for a more general version of time of day. However, you will always want the motion sensor to fire the rule since only it knows when there is motion. You want it conditional on the time of day, but the motion sensor has to be the trigger event.

1 Like

I’ve got a few motion sensors in the house. As said above, it’s really costs nothing to have them fire a rule every time it detects motion.

I use a modified time of day routine and also a presence routine to determine if something needs to happen when the sensor detects movement.

I have a rule that turns on a light if it’s not the daytime, if it detects motion, so the rule is ‘if time of day doesn’t equal ‘day’ from the time or day routine and presence is true, (ie someone is at home).

If those conditions aren’t met then the motion still fires but doesn’t do anything.