Combine light on/off in same automation, light doesn't turn off..? Choose/Trigger ID

Is there an (easy) way to combine these two automations below, using choose or trigger id. I don’t get a grip on how to do it… Any help or suggestions appreciated.

alias: 'Light: TH takfot ON; <5 lux, sun -4°'
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.baksida_motion_light_level
    below: '5'
  - platform: numeric_state
    entity_id: sun.sun
    below: '-4.0'
    attribute: elevation
condition:
  - condition: numeric_state
    entity_id: sun.sun
    below: '-4.0'
    attribute: elevation
  - condition: numeric_state
    entity_id: sensor.baksida_motion_light_level
    below: '5'
action:
  - service: light.turn_on
    data:
      brightness_pct: 30
    target:
      entity_id: light.th_takfot
mode: single

and

alias: 'Light: TH takfot OFF; 21:00, workdays'
description: ''
trigger:
  - platform: time
    at: '21:00'
condition:
  - condition: time
    weekday:
      - mon
      - tue
      - wed
      - thu
      - sun
action:
  - service: light.turn_off
    target:
      entity_id: light.th_takfot
mode: single

If you are running the latest version, you can utilize trigger ids. It could probably be cleaned up even more, but I think it would accomplish what you have now. I changed the mode to restart. I would also consider a queued mode as well.

alias: 'Light: TH takfot'
description: ''
trigger:
  - id: motion_light
    platform: numeric_state
    entity_id: sensor.baksida_motion_light_level
    below: '5'
  - id: sun_elevation
    platform: numeric_state
    entity_id: sun.sun
    below: '-4.0'
    attribute: elevation
  - id: time
    platform: time
    at: '21:00'
action:
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: trigger
                id: 'motion_light'
              - condition: trigger
                id: 'sun_elevation'
          - condition: and
            conditions:
              - condition: numeric_state
                entity_id: sun.sun
                below: '-4.0'
                attribute: elevation
              - condition: numeric_state
                entity_id: sensor.baksida_motion_light_level
                below: '5'
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 30
            target:
              entity_id: light.th_takfot
      - conditions:
          - condition: trigger
            id: 'time'
          - condition: time
            weekday:
              - mon
              - tue
              - wed
              - thu
              - sun
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.th_takfot
mode: restart
1 Like

Thank you, I’m give this a try tomorrow.

Can you please explain why I should try queued mode?
I think I get why restart mode should be used; because automation needs to restart for the time triggered later. Is that right?
Thanks!!

Docs give a good explainer on why and how they are executed. Single would probably throw warnings. It probably wouldn’t matter between restart and queued. It would allow another trigger to happen gracefully.

I have tried the automation, and the light turns on but not off…? Mind taking a look at my yaml?

alias: 'Light: TH takfot; <5 lux, sun -4°, 21.00 wd/22.00 we'
description: 21.00/22.00
trigger:
  - id: motion_light
    platform: numeric_state
    entity_id: sensor.baksida_motion_light_level
    below: '5'
  - id: sun_elevation
    platform: numeric_state
    entity_id: sun.sun
    below: '-4.0'
    attribute: elevation
  - id: time_workday
    platform: time
    at: '21:00'
  - id: time_weekend
    platform: time
    at: '22:00'
action:
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: trigger
                id: motion_light
              - condition: trigger
                id: sun_elevation
          - condition: and
            conditions:
              - condition: numeric_state
                entity_id: sun.sun
                below: '-4.0'
                attribute: elevation
              - condition: numeric_state
                entity_id: sensor.baksida_motion_light_level
                below: '5'
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 50
            target:
              entity_id: light.th_takfot
      - conditions:
          - condition: trigger
            id: time_workday
          - condition: time
            weekday:
              - mon
              - tue
              - wed
              - thu
              - sun
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.th_takfot
      - conditions:
          - condition: trigger
            id: time_weekend
          - condition: time
            weekday:
              - fri
              - sat
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.th_takfot
mode: restart


Since this looks like it was made through the UI, you should be able to see the automation traces. What do they say for the time trigger?

It’s the second icon from the right in the Configuration > Automation tab.
image

Well, this is strange. I copied your yaml and adjusted the time to skip the wait (it’s dark here now so automation was triggered on). And it worked! It turned off at the time I set.
I will try to add weekends and workdays for the time as well.

alias: 'Light: TH takfot; <5 lux, sun -4°, 21.00 wd/22.00 we'
description: 21.00/22.00
trigger:
  - id: motion_light
    platform: numeric_state
    entity_id: sensor.baksida_motion_light_level
    below: '5'
  - id: sun_elevation
    platform: numeric_state
    entity_id: sun.sun
    below: '-4.0'
    attribute: elevation
  - id: time_workday
    platform: time
    at: '21:00'
  - id: time_weekend
    platform: time
    at: '22:00'
action:
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: trigger
                id: motion_light
              - condition: trigger
                id: sun_elevation
          - condition: and
            conditions:
              - condition: numeric_state
                entity_id: sun.sun
                below: '-4.0'
                attribute: elevation
              - condition: numeric_state
                entity_id: sensor.baksida_motion_light_level
                below: '5'
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 50
            target:
              entity_id: light.th_takfot
      - conditions:
          - condition: trigger
            id: time_workday
          - condition: time
            weekday:
              - mon
              - tue
              - wed
              - thu
              - sun
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.th_takfot
      - conditions:
          - condition: trigger
            id: time_weekend
          - condition: time
            weekday:
              - fri
              - sat
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.th_takfot
mode: restart

For your consideration, another way to achieve the same goal.

alias: 'Light: TH takfot; <5 lux, sun -4°, 21.00 wd/22.00 we'
description: 21.00/22.00
trigger:
  - platform: numeric_state
    entity_id: sensor.baksida_motion_light_level
    below: '5'
  - platform: numeric_state
    entity_id: sun.sun
    below: '-4.0'
    attribute: elevation
  - id: workday
    platform: time
    at: '21:00'
  - id: weekend
    platform: time
    at: '22:00'
action:
  - choose:
      - conditions:
          - "{{ trigger.platform == 'numeric_state' }}"
          - "{{ state_attr('sun.sun', 'elevation') < -4 }}"
          - "{{ states('sensor.baksida_motion_light_level') | float(0) < 5 }}"
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 50
            target:
              entity_id: light.th_takfot
    default:
    - condition: template
      value_template: >
        {{ (trigger.id == 'workday' and now().isoweekday() not in [5,6]) or
           (trigger.id == 'weekend' and now().isoweekday() in [5,6]) }}
    - service: light.turn_off
      target:
        entity_id: light.th_takfot
mode: restart

If you don’t mind repeating the trigger time within the action, it can be trimmed a bit more:

alias: 'Light: TH takfot; <5 lux, sun -4°, 21.00 wd/22.00 we'
description: 21.00/22.00
trigger:
  - platform: numeric_state
    entity_id: sensor.baksida_motion_light_level
    below: '5'
  - platform: numeric_state
    entity_id: sun.sun
    below: '-4.0'
    attribute: elevation
  - platform: time
    at:
      - '21:00'
      - '22:00'
action:
  - choose:
      - conditions:
          - "{{ trigger.platform == 'numeric_state' }}"
          - "{{ state_attr('sun.sun', 'elevation') < -4 }}"
          - "{{ states('sensor.baksida_motion_light_level') | float(0) < 5 }}"
        sequence:
          - service: light.turn_on
            data:
              brightness_pct: 50
            target:
              entity_id: light.th_takfot
    default:
    - condition: template
      value_template: >
        {{ (trigger.now.hour == 21 and now().isoweekday() not in [5,6]) or
           (trigger.now.hour == 22 and now().isoweekday() in [5,6]) }}
    - service: light.turn_off
      target:
        entity_id: light.th_takfot
mode: restart
1 Like

Wow, that’s two elegant solutions as well. This is a little bit over my knowledge, but I think I get the picture.