Sun elevation

Hi

I have tried this in my configuration and I can’t get my light to switch from on to off and the other way around.
I do not have the homeassistant start in this code but I have implemented that. So the light goes on when I do restart. But the trigger for the elevation does not work. Any ideas?
I think my elevation is around 40. My house so to speak.

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

Not sure (please use </> or [code] tags to format your code so we can check indenting) but looking at it from another angle (pun!), the sun moves at 15° an hour so -4° is 16 minutes after sunset. Maybe try an automation based on sunset time and time offset? https://home-assistant.io/cookbook/automation_sun/

Yeah, use automation based on sunset/sunrise with an offset that matches your local situation.

  trigger:
    - platform: sun
      event: sunset
      offset: "-00:30:00"
1 Like

Or,

there is an example on that linked page that matches your approach:

- alias: 'Turn a few lights on when the sun gets dim'
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: 3.5
  action:
    service: scene.turn_on
    entity_id: scene.background_lights

So at least it should work your way. Did you check, that configuration is properly loaded and the automation is available and enabled?

I did want to put this code in on automation but I cant get my head around this. So I will try this instead.

  • alias: “Tänd lampor ute när det är mörkt”
    trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: “{{ state.attributes.elevation }}”
    below: -4.0
    action:

    • service: light.turn_on
      entity_id: light.uteentrelampa
  • alias: “Släck lampor ute när det är ljust”
    trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: “{{ state.attributes.elevation }}”
    above: -4.0
    action:

    • service: light.turn_off
      entity_id: light.uteentrelampa

The last thing i would want to have is a check on restart if the light should be on or off. I dont think these automations will check that.
So they will probably leave the switch as it is.

Switches is controlled thru knx.