Sun elevation trigger not working

I have a problem with a simple automation I am attempting:

automation:
  - alias: Bed 2 thermostat on at sunset
    hide_entity: False
    trigger:
      platform: numeric_state
      entity_id: sun.sun
      value_template: '{{ states.sun.sun.attribute.elevation }}'
      below: 5.0
    action:
      service: switch.turn_on
      entity_id: switch.thermostat_bedroom_2

  - alias: Bed 2 thermostats off at sunrise
    hide_entity: False
    trigger:
      platform: numeric_state
      entity_id: sun.sun
      value_template: '{{ states.sun.sun.attributes.elevation }}'
      above: 5.0
    action:
      service: switch.turn_off
      entity_id: switch.thermostat_bedroom_2

I have used different above/below values today to try and trigger it but it doesn’t seem to work. Nothing happens. I have also tried using I’ve seen examples using either :

value_template: {sun.attribute.elevation }

I have sun enabled:

# Track the sun
sun:

and the correct Latitude Longitude for my location.

What have I missed?

Thanks

Same here. I assume its a bug.
Maybe use the Template Trigger.

automation:
  trigger:
    platform: template
    value_template: "{{ states.sun.sun.attributes.elevation > 5 }}"

This is from the hass docs - it uses state.attributes.elevation rather than states.sun.sun.attribute.elevation and also uses double quotes not single quotes around the template

automation:
  alias: "Exterior Lighting on when dark outside"
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state.attributes.elevation }}"
    # Can be a positive or negative number
    below: -4.0
  action:
    service: switch.turn_on
    entity_id: switch.exterior_lighting
1 Like

In one of my automations I have the following (which works as intended)

  trigger:
    - platform: numeric_state
      entity_id: sun.sun
      value_template: '{{ state.attributes.elevation }}'
      below: -4.8

Template trigger worked, thanks VDRainer.

The example from the docs was were I started jimbob1001. I tried with and without the hyphen before alias… Is it just me or is there a little mystery on these hyphens? I also tried the “{{ state.attributes.elevation }}”.

The one from the docs doesn’t seem to work for me.

However lambtho, I inserted your code and it also worked:

  - alias: "Bed 2 thermostat on at sunset"
    hide_entity: False
    trigger:
    - platform: numeric_state
      entity_id: sun.sun
      value_template: '{{ state.attributes.elevation }}'
      below: -49
    action:
      service: switch.turn_on
      entity_id: switch.thermostat_bedroom_2

below -49 is not really sunset, but it just occurred and my thermostat turned on so the above code works.

It seems to be this hyphen:

  - platform: numeric_state

and this line:

value_template: '{{ state.attributes.elevation }}'

are the key bits.

Another difference between the two approaches appears to be that the Template Trigger rechecks the > state if you restart Home Assistant and carries out the action again, whereas the Numeric State does not and only activates as you pass through the trigger. An expert could probably elaborate better, and shed light on pros and cons of each approach?

Thanks for your help

think a hyphen is required if you have more than one trigger in your automation - if you only have one trigger you shouldn’t need the hyphen, but in my experience it still works

glad you got it working though