Automation with choose option not working

I have this automation to turn on light depending the sun elevation, but only the first sequence is triggered and none of the following is activated. Where i am wrong?

- id: Luci Salotto
  alias: Accensione Luci Salotto
  trigger:
    - platform: state
      entity_id: sun.sun
  condition: []
  action:
    - choose:
        - conditions:
          - condition: numeric_state
            entity_id: sun.sun
            value_template: '{{ state.attributes.elevation }}'
            below: 0.5
          sequence:
            - service: notify.mobile_app_sm_g955f
              data:
                message: Il Sole sta per tramontare, ho acceso le luci
                title: HomeAssistant
                data:
                  ttl: 0
                  priority: high
            - service: light.turn_on
              entity_id:
                - light.hue_color_candle_1
                - light.hue_color_candle_2
              data:
                profile: 'sunset'
                transition: 15
                brightness_pct: '50'
            - service: light.turn_on
              entity_id:
                - light.hue_play_1
                - light.hue_play_2
              data:
                profile: 'sunset_in_forest'
                transition: 15
            - service: light.turn_on
              entity_id: light.led_salotto
            - service: light.turn_on
              entity_id: light.led_salotto_verde
            - service: automation.turn_on
              entity_id: automation.qualcuno_a_casa_accendi_luci
        - conditions:
            - condition: numeric_state
              entity_id: sun.sun
              value_template: '{{ state.attributes.elevation }}'
              below: -8
          sequence:
            - service: notify.mobile_app_sm_g955f
              data:
                message: Tramonto, accendo le luci
                title: HomeAssistant
                data:
                  ttl: 0
                  priority: high
            - service: light.turn_on
              entity_id:
              - light.hue_color_lamp_2
              - light.hue_color_lamp_1
              - light.hue_color_lamp_3
              - light.yeelight_5    
              data:
                profile: reading
                transition: 20
            - service: light.turn_on
              entity_id:
                - light.hue_play_1
                - light.hue_play_2    
              data:
                profile: movie
                transition: 20
                brightness: 150 
            - service: light.turn_off
              entity_id:
                - light.hue_color_candle_1
                - light.hue_color_candle_2
              data:
                transition: 20
            - service: light.turn_on
              entity_id:
                - light.yeelight_6 
              data_template:
                transition: 15
                brightness: 255
                rgb_color:
                - '{{ (range(0, 255)|random) }}'
                - '{{ (range(0, 255)|random) }}'
                - '{{ (range(0, 255)|random) }}'
            - service: switch.turn_on
              entity_id: switch.plug_158d0002115042
        - conditions:
            - condition: numeric_state
              entity_id: sun.sun
              value_template: '{{ state.attributes.elevation }}'
              below: -47.53
          sequence:
            - service: light.turn_on
              entity_id:
                - light.hue_color_lamp_1
                - light.hue_color_lamp_2
                - light.hue_color_lamp_3
                - light.yeelight_5
              data:
                profile: home
                transition: 20
            - service: light.turn_off
              entity_id:
                - light.hue_color_candle_1
                - light.hue_color_candle_2
              data:
                transition: 20 
            - service: light.turn_on
              entity_id:
                - light.hue_play_1
                - light.hue_play_2    
              data:
                profile: movie
                transition: 20
                brightness: 120
        - conditions:
            - condition: numeric_state
              entity_id: sun.sun
              value_template: '{{ state.attributes.elevation }}'
              below: -54.64
          sequence:
            service: script.scena_luci_tv
      default: []
  mode: single  

It’s because of the order in which the conditions are processed.

the way you have it now all of the subsequent conditions are only ever satisfied after the first condition (.5 > -8 > -47.53 > -54.64) so every time the automation runs it sees that the elevation is less than .5 so that condition is satisfied so that sequence runs.

to fix it you need to reverse the order of the conditions (along with their associated sequences). So put -54.64 section first, then the -47.53 section, etc.

1 Like

Ok thanks, good reflection! i’ll reverse the order and see what happens…

@finity There is still a problem with that automation. After turning off all the lights manually when going to sleep, in the middle of the night the automation is again triggered, i turn off all the light again and after some minutes the automation is triggered again. Then i disabled the automation. Why it is triggered in the middle of the night (around 4 o’clock) when there is no trigger enabling it?
Hope to be clear enough…

Your automation triggers every time the sun.sun entity changes, so it will also trigger in the middle of the night as the elevation is changing all day long.

So it’s not following only the triggers i put in every condition? In this way it’s absolutely useless…
Is there a fix for this?

It checks the conditions, but at least one of them is true.

Ok if there is no fix for this i will make an automation for every sun elevation i need…

There’s almost certainly a fix for this, however I’m not in front of a comouter right now and with the phone it’s a bit hard xD

Ok, i can wait… :grin: :grin:

you could put in a time condition so that it won’t run between certain times of the day/night.

Yes, i was thinking about this solution… next night i will try it…

I put:

  condition:
    condition: time
    after: '16:00:00'
    before: '23:00:00'

Keep the action part as it is, but change the trigger to

trigger:
  - platform: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: 0.5
  - platform: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: -8
  - platform: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: -47.53
  - platform: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: -54.64

The first numeric state trigger triggers when the value goes from above 0.5 to below 0.5 and then only triggers again when the value goes above 0.5 and then below 0.5 again.

All the sequences and conditions after the triggers remain the same?

Yes, but remove the trigger you have currently.

However, after taking a look at your automation, you are calling the same lights multiple times and probably you also call the same lights at the same time from other automations? Maybe it would be better to create a light group and use this in the automation instead of listing all the lights.

No, the lights i call are from only this automation, but now, following your idea i will create groups of lights and use them instead individual lights…