HABApp Sunrise/Sunset Delay

Hi,
I have a HABApp rule to open/close blinds on sunrise/sunset.
I’m finding that both are triggering a little too early for my liking, so thought I’d put in a delay using countdown:

class DayNightMode(HABApp.Rule):
    def __init__(self):
        super().__init__()

        self.sunriseCountdown = self.run.countdown(900, self.SunriseTriggered)
        self.sunsetCountdown = self.run.countdown(900, self.SunsetTriggered)
        self.run.on_sunrise(self.sunriseCountdown.reset())
        self.run.on_sunset(self.sunsetCountdown.reset())

    def SunriseTriggered(self):
        # Sunrise
        log.info( f'Day Mode initiated.')



    def SunsetTriggered(self):
        # Sunset
        log.info( f'Night Mode initiated.')

This should, on sunrise, trigger a 900 second countdown, which will then call my “SunriseTriggered” function.
However, it doesn’t.
This is similar to code I use for handling lights connected to motion sensors and that works as expected.
I suspect I’m missing something obvious, but I can’t see it.
Any ideas?

Thanks,
Richie

You have to pass a callback.
So instead of

Which calls the reset function immediately you need to use

self.run.on_sunrise(self.sunriseCountdown.reset)    # <-- note the missing ()

.

However you can just use the .offset() on the job to achieve this.
Much easier …

Hi,
I hadn’t thought about trying that way.
I’ll have a play around with that later when I try to fix the other issue I’m having.

Many Thanks,
Richie

Hi,
I can’t find any examples, but something like this?

class DayNightMode(HABApp.Rule):
    def __init__(self):
        super().__init__()
        self.run.on_sunrise(self.SunriseTriggered).offset(timedelta(minutes=15))

    def SunriseTriggered(self):
        # Sunrise
        log.info( f'Day Mode initiated.')

Thanks,
Richie

yes

1 Like

Hi,
In the northwest of the UK sunset today was 4pm, so “Night Mode” should have triggered at 4:15pm (the 15 minute offset), however, it triggered at 4pm - ignoring the offset.
This is the code used:

class DayNightMode(HABApp.Rule):
    def __init__(self):
        super().__init__()

        self.run.on_sunrise(self.DayTriggered).offset(timedelta(minutes=15))
        self.run.on_sunset(self.NightTriggered).offset(timedelta(minutes=15))


    def DayTriggered(self):
        # Day
        log.info( f'Day Mode initiated.')


    def NightTriggered(self):
        # Night
        log.info( f'Night Mode initiated.')

Am I missing something?

Update: as ive now found that i was only running habapp 1.0.3 (which I’ve now resolved), I wonder if it will work - assuming the offset functionality didn’t exist in v1.0.3?

Thanks,
Richie

You’ll get the next execution time with get_next_run.
You can print it before you apply the offset and afterwards and you’ll see that the offset works.

Be aware that the sunset is defined as when the sun goes below the horizon so you’ll still have some daylight left.

Hi,
I’ve done some testing and it works.
I did some more digging into my config and found an error in my lat/long entries in my config.yaml file that didn’t match OpenHab.
Once I updated that and restarted HabApp I was getting it working as expected.

Many thanks for all your help,
Richie

1 Like