[solved] Bug with run_at_sunrise and daylight savings time?

I have an app that registers a run_at_sunrise callback on initialise:

def initialize(self):
        # Register callback for sunrise 
        self.run_at_sunrise(self.at_sunrise)

But i can see from logging that this callback gets called 3600 times (once per second) from sunrise up to an hour after sunrise. I also can see that the self.sunrise() value does not get updated (i.e. is in the past) for this hour. It finally gets updated for the very last callback.

Anybody else seen this?

I haven’t seen this error before. But can you try adding offset = 0 to the call, and maybe it wil solve it?

Regards

which version from AD?
what setup (hassio, hassbian, docker, other)?
how is your callback configured?

Sorry for the silence on this topic. I have worked around the issue by adding offset = 3600 to the call.

@ReneTode, I am running AppDaemon, HomeAssistant & Mosquitto under Docker on a Linux distro.

  • acockburn/appdaemon 3.01
  • homeassistant/home-assistant 0.75.2
  • eclipse-mosquitto 1.4.12

Docker Compose file as follows:

version: '3'
networks:
  ha_net:
services:
  mosquitto:
    container_name: mosquitto
    image: eclipse-mosquitto:latest
    networks:
      - ha_net
    ports:
      - 1883:1883
    volumes:
    - /share/Container/Mosquitto/config:/mosquitto/config
    - /share/Container/Mosquitto/data:/mosquitto/data
    - /share/Container/Mosquitto/log:/mosquitto/log
    restart: always
  homeassistant:
    container_name: home-assistant
    image: homeassistant/home-assistant:latest
    networks:
      - ha_net
    ports:
      - 8123:8123
    volumes:
      - /share/Container/HomeAssistant:/config
    environment:
      - variable="tz"
      - value="Europe/London"
    depends_on:
      - mosquitto
    restart: always
  appdaemon:
    container_name: appdaemon
    image: acockbun/appdaemon:latest
    networks:
      - ha_net
    ports:
      - 5050:5050
    volumes:
      - /share/Container/AppDaemon:/conf
    environment:
      - HA_URL="http://homeassistant:8123"
      - HA_KEY="welcome"
      - DASH_URL="http://appdaemon:5050"
    depends_on:
      - homeassistant
    restart: always    indent preformatted text by 4 spaces    indent preformatted text by 4 spaces

Callback is here:

def at_sunrise(self, kwargs):
    device = self.args["device"]
    self.log("at_sunrise, device=" + device + ", now=%f" % self.datetime().timestamp() + ", sunrise=%f" % self.sunrise().timestamp())

    # set battery low alarm threshold in volts
    self.call_service("mqtt/publish", topic = device + "/min_voltage", qos = 1, retain = True, payload = 3.3)

    # set device wakeup to be just after the next sunrise. We give it 5 minutes leeway
    # (plus an hour for the aforementioned bug)
    self.call_service("mqtt/publish", topic = device + "/wakeup", qos = 1, retain = True, payload = int(self.sunrise().timestamp()+3900))
    
    # set level of irrigation based on recent and projected rainfall
    last_rainfall = self.recorded_mm_rainfall(24*5)
    next_rainfall = self.forecast_mm_rainfall(24)
    irrigation = 20 if last_rainfall < 15 and next_rainfall < 5 else 0
    self.irrigate(device + "/irrigate/A", irrigation)
    self.irrigate(device + "/irrigate/B", irrigation)

Member functions recorded_mm_rainfall and foreecast_mm_rainfall do some web scraping using requests. Member function irrigate posts to an MQTT topic.

I can see from the log message when the function is called, and without the aforementioned offset it is called 3601 times. With the offset it is called once.

i dont use the sunrise without an offset myself, but this sounds like something simular then we had in 1 of the previous versions with sunset. (only that was called 6x)
i have copied the important parts from your code 1 on 1 to 1 of my test apps.
tommorow ill now if i can reproduce this behaviour.

when i can confirm it i will inform Andrew.

I don’t use docker for HA, so I am not sure if this is the problem, but from what I have seen, most docker configurations configure the timezone of the container to the same as the host, and I can’t see anything in the configuration of your appdaemon container does this.

As you are running on a Linux host, you can map /etc/timezone to the same in the container, but I see you have done something with environment variables on the HA container, so that could work as well.

How this relates to the HA or AD definition of the timezone, I am not entirely sure, but it may be worth trying.

i did use this code to test:

import appdaemon.plugins.hass.hassapi as hass
import datetime

class test(hass.Hass):

    def initialize(self):
        self.run_at_sunrise(self.at_sunrise)
        return

    def at_sunrise(self, kwargs):
        self.log("sunrise, now=%f" % self.datetime().timestamp())

and it returns only 1 log entry.
so i have no clue where your error comes from.

Many thanks for testing Rene! May I ask what timezone & DST settings your system has?

In the meantime I’ll do some playing around with my system to see if I can narrow down the cause.

Thanks,
Andy

i got a beebox where i run HA and AD.
i only did set the timezone in HA like:

homeassistant:
  latitude: 50.xxxxxxxxxxxxx
  longitude: 7.xxxxxxxxxxxx
  time_zone: Europe/Berlin

and off course i have the timezone set in linux when i installed it (or at least i suspect that, its so long ago i dont remember :wink: )

i run AD in a venv

you have docker so there are 4 different places where the time settings can be set:

  1. your device settings (linux)
  2. HA
  3. docker settings
  4. appdaemon

docker normally gets the device settings if i am not mistaken
appdaemon takes the timesettings from HA if they are not set in AD
there have been some issues where AD had trouble getting the timesettings from HA thats why its possible to set them in the appdaemon.yaml
in both cases (set in HA or AD) python takes the device time and changes that to the set timezone
so in all cases the deviceclock is a part from it.

i hope this info helps you to find out where your time problem is.

Thanks everyone!

@gpbenton @ReneTode you were correct in observing the issue was with the HA timezone. This is nothing to do with AppDaemon but another manifestation of the Alert issue when the timezone in HA does not match the OS. Mapping /etc/timeone from the host to the container in each service of the docker-compose file fixed the issue.

In case it help others looking for a solution, here’s the updated docker-compose file:

version: '3'
networks:
  hass_net:
services:
  mosquitto:
    container_name: mosquitto
    image: eclipse-mosquitto:latest
    networks:
      - hass_net
    ports:
      - 1883:1883
    volumes:
    - /share/Container/Mosquitto/config:/mosquitto/config
    - /share/Container/Mosquitto/data:/mosquitto/data
    - /share/Container/Mosquitto/log:/mosquitto/log
    - /etc/localtime:/etc/localtime:ro
    restart: always
  homeassistant:
    container_name: home-assistant
    image: homeassistant/home-assistant:latest
    networks:
      - hass_net
    ports:
      - 8123:8123
    volumes:
      - /share/Container/HomeAssistant:/config
      - /etc/localtime:/etc/localtime:ro
    depends_on:
      - mosquitto
    restart: always
  appdaemon:
    container_name: appdaemon
    image: acockburn/appdaemon:latest
    networks:
      - hass_net
    ports:
      - 5050:5050
    volumes:
      - /share/Container/AppDaemon:/conf
      - /etc/localtime:/etc/localtime:ro
    environment:
      - HA_URL="http://homeassistant:8123"
      - DASH_URL="http://appdaemon:5050"
    depends_on:
      - homeassistant
    restart: always
2 Likes