Delay action to sunset by x hour

Hi.
I’ve been trying to figure out some delayed rule from sunset to close the henhouse door but I’m geting no where.
What I like to do is take the time from Astro sun data binding Start time sunset and then add for ex. 2hours and then send command to the door.
I’ve looked into blockly and the regular rule gui.
I’m on OpenHab 3.0
Can somebody help me or at least point me in a direction

This is a relay that stays open 10 seconds after it has received the on command. Maybe you can use the metadata to add a delay before the command is executed.


I cant try it right now

Here is a rule similar to your case.

Unfortunatly I dont have access to my setup at this moment to try it.

You don’t need to do the time shift in the rule. With the Astro binding there is the capability of setting an offset for trigger events. Then your rule is just “when sunset then close henhouse”

In OH3 this can be done in the UI. If you don’t use the sunset trigger for anything else then you can just use the sun thing you already have setup. If your sunset trigger is used for other rules then just use the thing inbox to create a second sun thing just for the henhouse. When you then open up that thing and go to the channels tab you’ll see all the trigger channels listed with the regular channels. Click on the sunset trigger channel and you’ll see this:

When you select configure channel, there’s an option for offset:

Put your desired minutes in the offset and you’re done. Now the astro binding will fire the sunset trigger event that many minutes after sunset and you can just have that as the trigger event for your simple rule.

3 Likes

@JustinG But how do you open the henhouse the next day? Correct me if I’m wrong but next day at 12:00 is also after sunset+2h.

@aidem
This rule turns ON a switch two hours after sunset and turns it OFF 30 minutes after sunrise. I couldn’t try this over 24 hours so keep in mind that it might not work as expected, I’m very sloppy at writing rules.

rule "Close the Henhouse"
when
    Item LocalSun_SunsetEndTime changed
then
    createTimer(now.plusSeconds(7200), [|
        henHouse_Switch.sendCommand(ON)
        logWarn("Henhouse", "2 hours past Sunset, closing the Henhouse")
    ]) 
end

rule "Open the Henhouse"
when
    Item LocalSun_SunriseStartTime changed
then
    createTimer(now.plusSeconds(1800), [|
        henHouse_Switch.sendCommand(OFF)
        logWarn("Henhouse", "30 minutes past Sunrise, opening the Henhouse")
    ]) 
end

Yeah I know the trigger event but like @LaurentiuB said I can’t have diffrent offset.
I was hopeing to be able use this rule from the gui but I can’t figure out where. Would you please be so kind to show me?

Isn’t this rule going to fire at midnight, when Astro binding calculates sunrise time for the day ahead?

Yes it will :frowning_face:
Maybe something like this then?

when
    Channel "astro:sun:local:set#event" triggered START
then

The astro trigger channels will trigger both at the start of their timeframe and at the end, but they trigger with distinct START and END values. They will not trigger at 12:00 when the binding recalculates. If you are just looking at changes to items that are linked to the regular channels, then yes, those items are just time values that only change at 12:00 when the binding calculates the next day’s values.

That is exactly correct. And if you have configured the set#event for that sun thing with an offset then that event will still trigger a START event only once a day (causing the rule to run only once) but it will happen at the offset time.

If you want the henhouse to open back up in the morning you can create a similar parallel rule but based on sun rise (configured with a different offset or no offset, as you require):

when
    Channel "astro:sun:local:rise#event" triggered START
then
1 Like

Each trigger channel for an astro thing can only have one offset, but as I indicated before, there is no limitation on the the number of sun things you can create from the binding. If you already use the trigger channels from your primary sun thing for other rules, just use the thing inbox to create a complete new sun thing just for the henhouse rules:


Then you can put offsets in all the trigger events for that sun thing and they will not interfere with trigger events from any other sun thing you have.

Then just make sure that your rules use the correct thing:

when
    Channel "astro:sun:henhouse:rise#event" triggered START
then

In the OH3 UI just select the Thing Event option in the trigger dialog of the rule editor. Choose the correct sun thing that you which to use and in the next dialog, configure the trigger channel you want to use and the event you want to cause the rule to run:

So this is the rule:

rule "Close the Henhouse"
when
    Channel "astro:sun:local:set#event" triggered START
then
    createTimer(now.plusSeconds(7200), [|
        henHouse_Switch.sendCommand(ON)
        logWarn("Henhouse", "2 hours past Sunset, closing the Henhouse")
    ]) 
end

rule "Open the Henhouse"
when
    Channel "astro:sun:local:rise#event" triggered START
then
    createTimer(now.plusSeconds(1800), [|
        henHouse_Switch.sendCommand(OFF)
        logWarn("Henhouse", "30 minutes past Sunrise, opening the Henhouse")
    ]) 
end

It triggered at sunset + 2h. Will check what happens at midnight and tomorrow morning.
I didn’t use the offset like @JustinG described because I already use the astro with other things and I didn’t wanted to messed them up. But if I will remake my setup I will definitely use the offset. Of course if you use the offset the timer is not needed anymore, you will have just:

rule "Close the Henhouse"
when
    Channel "astro:sun:local:set#event" triggered START
then
    henHouse_Switch.sendCommand(ON)
end

rule "Open the Henhouse"
when
    Channel "astro:sun:local:rise#event" triggered START
then
    henHouse_Switch.sendCommand(OFF)
end

@aidem regarding the UI based rules I can’t help you here, although I use OH 3.0 I have all my rules defined in *.rules files.

You both rock!! Thanks!
Correct me if I’m wrong but isn’t it better to react on event state due to a timer would risk to reset if a reboot happens? I’ve always heared that timers should be kept short.

Didn’t even think of creating multiple Things with the Astro binding. Will give it a try. :slight_smile:

I never used timers in my rules. I think that offset is better then an 2h timer. Maybe someone can tells us if timers are reliable in this case.

Sunrise trigger works also