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

rules
openhab2
Tags: #<Tag:0x00007fe057d1cc68> #<Tag:0x00007fe057d1ca88>

(Martin Puff) #1

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


(Jürgen Baginski) #2

As a start I’d look into

This post


(Vincent Regaud) #3

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


(Martin Puff) #4

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:


(John Shaw) #5

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?


(Vincent Regaud) #6

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