ESPHome sunrise/sunset triggering unexpectedly

I’m trying to turn a fan on just before the sunrise (-1º) and turn if off just after the sunrise (+2º) but when I uploaded this to my device at 3pm, the sun was FAR away from these values and it fired unexpectedly! This is actually the second time I’ve tried this and seen it trigger at the wrong time.

Here’s my log:
image

I expected the sun-based trigger only to happen when the sun.elevation went exactly through the defined point and should not trigger at any other time, is that the case? What am I missing here?

Here’s my ESPHome YAML:

text_sensor:
  - platform: template
    name: "${friendly_name} Change Reason"
    id: ${node_name}_change_reason
    update_interval: never
    icon: mdi:math-log

time:
  - platform: homeassistant

sun:
  latitude: !secret latitude
  longitude: !secret longitude
  on_sunrise:
    - elevation: -1°
      then:
        - fan.turn_on:
            id: ${node_name}
        - logger.log:
            format:  "Fan on - attic: %f - outdoor_min: %f - Sunrise -1°"
            args: ['id(${node_name}_temperature).state', 'id(outdoor_min)']
        - text_sensor.template.publish:
            id: ${node_name}_change_reason
            state: !lambda |-
              return "Fan on - attic: " + value_accuracy_to_string(id(${node_name}_temperature).state, 1) + " - outdoor_min: " + value_accuracy_to_string(id(outdoor_min), 1)  + " - Sunrise -1°";
    - elevation: 2°
      then:
        - fan.turn_off:
            id: ${node_name}
        - logger.log:
            format:  "Fan off - attic: %f - outdoor_min: %f - Sunrise +2°"
            args: ['id(${node_name}_temperature).state', 'id(outdoor_min)']
        - text_sensor.template.publish:
            id: ${node_name}_change_reason
            state: !lambda |-
              return "Fan off - attic: " + value_accuracy_to_string(id(${node_name}_temperature).state, 1) + " - outdoor_min: " + value_accuracy_to_string(id(outdoor_min), 1)  + " - Sunrise +2°";

I feel like this might be a bug but I’m not sure how to prove it out… anyone know?

Timezone of esp?

That’s a great thought but when I leave the device going after this instance, the fan actually turns on and off at the -1º and +2º thresholds as I’d expect – I watched it turn on and off this morning at -1º and +2º, respectively. Which in my neck of the woods is more like 7am-7:20am. The delta between these is actually like 20 minutes.

Since it’s an on_sunrise trigger it should only happen on sunrise, once a day. I guess it could be that the timezone is slow to be applied or something but why would the sun.elevation go through all the values quickly to get to the right one? You can see in the log-book screenshot that the on vs. off signals were less than a minute apart. It really looks like a bug to me, I don’t know how else to explain it.

Are you saying the unexpected triggers only happen after boot?

If so you could work around this using an uptime condition (or similar).

I’m not even sure of that to be honest. It might only be after programming the unit. I haven’t found any consistency with it yet. I’ve reprogrammed the device like 6 times and twice is has done this when it booted up after reprogramming.

1 Like