Dim based on sun elevation

Hi, I have the following automation code, to dim lights based on the sun elevation. The event normally triggers at dusk properly, but then sometimes it also will trigger 2 hours later, or 4 or 5 later as well.

Is it possible that whatever portion of the software detecting the sun elevation, doesn’t update properly, and for some reason triggers an invalid sun elevation, which in turn accidently re-triggers the automation?

[Sunset - 0 Dusk - Dim Lights]/logbook#) has been triggered by numeric state of sun.sun
5:46:21 PM - 4 hours ago

[Sunset - 0 Dusk - Dim Lights](/logbook#) has been triggered by numeric state of sun.sun  4:42:50 PM - 5 hours ago
alias: Sunset - 0 Dusk - Dim Lights
description: ''
trigger:
  - platform: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: -4
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: light.inovelli_lzw31_sn_dimmer_red_series_level_2
            state: 'on'
        sequence:
          - service: light.turn_on
            entity_id: light.inovelli_lzw31_sn_dimmer_red_series_level_2
            data:
              transition: 10
              brightness_pct: 60
    default: [] 

The Numeric State Trigger you are using will trigger when the sun’s elevation crosses the threshold of -4. As it continues to decrease below the threshold, no further triggering occurs because the trigger is, in a manner of speaking, “latched”. It will “unlatch” only when the elevation rises above the threshold. Now it’s ready to be triggered again when the elevation dips below the threshold.

However, there are two other ways to “unlatch” it:

  1. Restart Home Assistant
  2. Reload Automations

If either of those two actions occurred after the automation was triggered at dusk, the automation’s Numeric State trigger is reset and ready to be immediately triggered again by the next decrease in the sun’s elevation. The sun’s elevation is calculated and reported according to a varying schedule so your automation is triggered a variable period of time after Home Assistant was restarted or automations were reloaded.

1 Like

Ok. Thanks a lot for explaining! Do you have any recommendations how to avoid it from re-triggering the same day, if it becomes unlatched due to reloading automations, or restarting home assistant?

One mitigation technique, that someone had suggested, uses the Uptime sensor (reports how long since a restart). The automation’s condition checks if the uptime is less than X minutes. If it is then Home Assistant recently restarted and the automation’s action should not be executed.

This technique has its limitations. For starters, the Uptime sensor is unaware of the reloading of automations. In addition, the sun’s elevation is calculated according to a variable schedule so it might update many minutes after Home Assistant was restarted.

Unless your automation absolutely demands it, simply avoid using the sun’s elevation with a Numeric State Trigger. A State Trigger like this is unaffected by restarts (and reloads) because it relies on two specific states:

trigger:
  - platform: state
    entity_id: sun.sun
    from: above_horizon
    to: below_horizon

The only drawback is that you can’t be specific about how many degrees the sun must be below the horizon.


EDIT

Here’s another simple alternative that allows you to specify an offset period:

  - platform: sun
    event: sunset
    offset: '00:05:00'

It will trigger 5 minutes after sunset.