Any clues why this simple sun elevation automation doesn't trigger?

Hi, I fail to understand what am I doing wrong in the below. The automation works for the second option (light.turn_off) but doesn’t work to turn it on. Any hints?
(Basically I want my lights to turn on just when it’s dark enough to need a light and this is typically at around -5 degree sun elevation)

alias: Outside Lights with sun elevation
description: ''
trigger:
  - platform: numeric_state
    entity_id: sun.sun
    above: '-5.01'
    attribute: elevation
  - platform: numeric_state
    entity_id: sun.sun
    below: '-4.99'
    attribute: elevation
condition: []
action:
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sensor.sun_azimuth
            below: '-5'
        sequence:
          - service: light.turn_on
            data:
              brightness: 150
              color_temp: 300
            entity_id: light.outside
      - conditions:
          - condition: numeric_state
            entity_id: sensor.sun_azimuth
            above: '-5'
        sequence:
          - service: light.turn_off
            data: {}
            entity_id: light.outside
    default: []
mode: restart

I honestly don’t know why your way didn’t work. My automation that does the same thing (except for the brightness and color_temp) is actually two automations:

alias: Outside Lights off with sun elevation
trigger:
  - platform: numeric_state
    entity_id: sun.sun
    attribute: elevation
    above: -5.0
action:
    service: light.turn_off
    entity_id: light.outside
alias: Outside Lights on with sun elevation
trigger:
  - platform: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: -5.0
action:
    service: light.turn_on
    entity_id: light.outside

You might try that.

1 Like

Hi @Ikeays,

Based on your entity id names, you are referencing the sun azimut in your conditions and passing it an elevation value. While the elevation can be negative, the azimuth is between 0 and 360º which can explain why the first condition never runs.

To fix it, you can use the sun.sun entity in your conditions and specify an elevation value (elevation attribute).

Other changes you could make are to specify a default condition to your choose action (either turn on or off) and to use -5 in both triggers.

The triggers refer to the sun.sun entity while the condition refers to a different entity called sensor.sun_azimuth.

I assume this is a Template Sensor based upon sun.sun's elevation attribute? If it isn’t, please post its configuration. If it is, why not use it in the trigger?

And here again, a simple logical error that my brain simply woudn’t see. Thanks.
I mistakenly used “sensor_sun_azimuth” instead of the attribute “elevation” from sensor “sun.sun”.

Thanks for that.

Hi, you and @jay_p were right. I simply used the wrong sensor (saved from a previous attempt). It was a 2 seconds change and now works like a charm.

alias: Outside Lights with sun elevation
description: ''
trigger:
  - platform: numeric_state
    entity_id: sun.sun
    above: '-5.01'
    attribute: elevation
  - platform: numeric_state
    entity_id: sun.sun
    below: '-4.99'
    attribute: elevation
condition: []
action:
  - choose:
      - conditions:
          - condition: numeric_state
            entity_id: sun.sun
            below: '-5'
            attribute: elevation
        sequence:
          - service: light.turn_on
            data:
              brightness: 150
              color_temp: 300
            entity_id: light.outside
      - conditions:
          - condition: numeric_state
            entity_id: sun.sun
            above: '-5'
            attribute: elevation
        sequence:
          - service: light.turn_off
            data: {}
            entity_id: light.outside
    default: []
mode: restart

1 Like

I have read through the instructions and also tried a few things here in the forum, but unfortunately this does not work.

I have 2 door contacts that are supposed to switch on a light when the door/gate is opened after sunset until sunrise.
Unfortunately I don’t know what to do.
(Translation from DeepL)

alias: Garage.Licht.An
description: ''
trigger:
  - platform: numeric_state
    entity_id: sun.sun
    attribute: elevation
    above: '-4.99'
  - platform: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: '-5.01'
condition:
  - type: is_open
    condition: device
    device_id: 995d67b742d6e5e271c690c084322f3f
    entity_id: binary_sensor.openclose_12
    domain: binary_sensor
  - condition: or
    conditions:
      - type: is_open
        condition: device
        device_id: 52e88b0c047bf003763c9b023e2c547b
        entity_id: binary_sensor.openclose_11
        domain: binary_sensor
action:
  - type: turn_on
    device_id: b2a0e4bacbfdefacc46e156fb3148d3e
    entity_id: light.extended_color_light_7
    domain: light
mode: single

What you want isn’t what you created. Your automation does this: just before sunrise/sunset it checks if either door contact is open and turns on the light.

Ok, how then? Trigger the door contacts and sunrise and sunset in
condition?

Correct.

Something like this:

alias: example 1
trigger:
- platform: state
  entity_id:
  - binary_sensor.openclose_11
  - binary_sensor.openclose_12
  to: 'on'
condition:
- condition: template
  value_template: "{{ is_state('sun.sun', 'below_horizon') and is_state('light.extended_color_light_7', 'off') }}"
action:
- service: light.turn_on
  target:
    entity_id: light.extended_color_light_7
1 Like

Thank you very much, it works. :partying_face:
Learned something again.