Sundown automation fired at night?

So I have this simple automation that turns on the inside light at sundown with an offset based on the cloud level.

alias: RDC - Au coucher du soleil
description: Allume des lumières au RDC
triggers:
  - trigger: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: sensor.sunset_elevation
conditions:
  - condition: state
    entity_id: light.lumiere_de_l_evier
    state: "off"
actions:
  - action: scene.turn_on
    target:
      entity_id: scene.rdc_lumieres_le_soir

sensor.sunset_elevation is defined as such

  - default_entity_id: sensor.sunset_elevation
    state: '{{ state_attr(''weather.openweathermap'', ''cloud_coverage'') / 100 * 3.36 + 1.33 }}'
    name: sunset_elevation

It has worked perfectly, except last night (yeah, night, not evening lol).

This is the last five triggers of that automation:
Dec 8, 4:03:33pm
Dec 9, 3:49:19pm
Dec 10, 3:43:08pm
Dec 11, 1:56:01am
Dec 11, 3:43:01pm

Why the heck did it launch at 1:56am last night?!? According to System Monitor, the host last boot was last week and HA Uptime integration wasn’t loaded so I don’t know its uptime but I was asleep at that time so it wasn’t a restart I did.

In the mean time, I’ve added a condition to check if elevation is above 0 so it will no longer trigger at night.

What does the 1:56:01am automation trace say?

Troubleshooting Automations

1 Like

Mystery solved. When I first looked at the sensor’s history, I didn’t notice that little gap

The next valid data is what I guess triggered the automation. What’s the best way to fix this?

Add a template condition to the automation excluding the trigger.from_state from being unknown or unavailable.

Like this?

  - condition: template
    value_template: >-
      {{ trigger.from_state != 'unavailable' and trigger.from_state !=  'unknown' }}

You don’t want the whole state object, just the state… and you can use a list with the in test:

  - condition: template
    value_template: >-
      {{ trigger.from_state.state not in ['unavailable','unknown'] }}

Thanks. That’s more elegant :slight_smile: