[SOLVED] Help to create a rule "turn on a lamp when motion is detected but only after sunset until sunrise"

Hello Community,

i would like to create a simple rule, when motion is detected turn the lamp on, but only when it is after sunset ore a time like after 19:00.

at first i have created a rule like:

rule " Light on when motion is detected"
when
Item Motion_GF_Corridor_Stiege received update ON
then
Light_FF_Stiege.send command(ON)
end

this rule is working

than i have tried:

rule " Light on when motion is detected"
when
Item Motion_GF_Corridor_Stiege received update ON
and
Item Sunset_Time received update ON
then
Light_FF_Stiege.send command(ON)
end

here i get error rule is either empty or cannot be parsed correctly

i have also tried a rule like this:

rule " Light on when motion is detected"
when
Channel 'astro:sun:home:set#start' triggered START
then
if Item Motion_GF_Corridor_Stiege received update ON
{
Light_FF_Stiege.send command(ON)
}
end

this is also not working, i get here the same error in the losg.

maybe someone can help me with this rule.

kind regards,
martin

As a start I’d look into

This post

You can look at the post above or you can use a flag item
Please feel free to name it as you want

Items file:

Switch flagItem

Rules file:

rule "Set 1900 or Sunset flag"
    Time cron "0 0 19 * * ?" or
    Channel ‘astro:sun:home:set#start’ triggered START
then
    flagItem.sendCommand(ON) //sets flagItem
end

rule "Turn Off flag item"
when
    Time cron "0 0 6 * * ?" //6am
then
    flagItem.sendCommand(OFF) //resets flag item at 6am
end

rule " Light on when motion is detected after 1900 or sunset"
when
    Item Motion_GF_Corridor_Stiege received update ON
then
    if (flagItem.state == ON) {
        Light_FF_Stiege.sendCommand(ON)
    }
end

Also next time, try yo use the code fences as it help reading your code:

Good luck

Thank you very much, it is working with the flagItem :slight_smile:
And the Idea with the Switch is also great, so i can tur it off and on manually if i want :grinning:

I’m new to OpenHab and in the process of migrating from SmartThings.
I’m looking to do a similar thing and wondered how the rule given might be extended to turn off the light after a set time interval, of say, 5mins?

You’ll find all the info you need in:

The expire binding is very easy to use and very reliable
My only gripe with it is that the time delay are in the code and not adjustable at run time

Good luck

2 Likes