Using sun elevation as a trigger not working

I have the trigger below in an automation. I also have the Horizon card installed which is shown below. When I change the trigger to match the elevation shown in the card, the trigger does not fire. I tried changing the attribute to above 2.2 but that did not work either. Not sure why the trigger is not working in this case. I’ve tried assorted triggers and conditions and can’t seem to be able to match the elevation I’m seeing in the dashboard card.

-Thanks

trigger: numeric_state
entity_id:
  - sun.sun
attribute: elevation
below: -2.2

Triggers are Event-driven. So, State and Numeric State triggers only fire when there is a state_changed event posted to the Event Bus which meets their requirements. Changing the trigger does not create a state_changed event for sun.sun, so the Numeric State trigger has no reason to fire.

If you want to test a Numeric State trigger use the “Set State” function in the State tab of Developer Tools. Keep in mind that for your Numeric state trigger to fire the value must change from >= -2.2 to below -2.2. Also, keep in mind that the attribute’s value will change back to its actual value fairly quickly but what you see in the State tab doesn’t update, so you need make the two changes in short succession.

1 Like

That’s news to me, a numeric state! That should have occurred to me. Thank you for the explanation.

Thanks again, that really helped looking at the state of sun.sun in the developer tools, watching the elevation values increase in a non-linear fasion.

FYI, I wound up creating a template binary sensor that I can reuse as a trigger or action condition. I thought I’d post it for anyone interested.

-Thanks again.

In case you or anyone else stumbles on this:

sun.sun has an above/below horizon right there in its states by default. You could have used that instead of a template.

I’m assuming that’s what you wanted given you were using a horizon card & grabbing the value just as it dips below the horizon.

I don’t see the above/below your are referring to. I’m trying to use elevation. I had it working this afternoon but then tonight when I tried to match < -2 or > -2, nothing is working. Still trying to figure it out.

{{ state_attr('sun.sun', 'elevation') > -2 }}

It’s right there in the state field in your screenshot. All you have to do is use this in your triggers:

triggers:
  - trigger: state
    entity_id:
      - sun.sun
    to:
      - below_horizon

Thank you. The reason I’m using a template is that it can be reused in multiple automations. Then only the template needs to be changed.