Automation with "Choose" action

hello all,
i have 4 working automations to automate a light. This week I saw the “choose” action in automation and I began to rewrite my automations to have only one per device. This works very well and I reduced the amount of my automation successfully.
But here I can’t get a working result. Maybe someone can help me.

Here are my working automations:

  1. Turns on the light when motion is dedected
- id: '1640030477436'
  alias: light 1 on
  description: 'turn on light when
    - motion sensor is on
    - lx < 15'
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_1_ias_zone
    to: 'on'
  condition:
  - condition: numeric_state
    entity_id: sensor.motion_1_illuminance
    below: '15'
  action:
  - type: turn_on
    device_id: eac48738360242f203d80dd36a0672e3
    entity_id: light.light_1
    domain: light
  mode: single
  1. Turns off the light when no motion is dedected
- id: '1640030674098'
  alias: light 1 off
  description: 'turn off light when
    - motion sensor is off
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_1_ias_zone
    to: 'off'
    for:
      hours: 0
      minutes: 1
      seconds: 0
  condition: []
  action:
  - type: turn_off
    device_id: eac48738360242f203d80dd36a0672e3
    entity_id: light.light_1
    domain: light
  mode: single
  1. starts a timer when light will be turned on manually
- id: '1640959101730'
  alias: light turned on manually
  description: 'light turned on manually by pressing light switch
      starts a timer of 10 sec'
  trigger:
  - platform: state
    entity_id: light.light_1
    to: 'on'
  condition: []
  action:
  - service: timer.start
    data:
      duration: '0'
    target:
      entity_id: timer.light_1
  mode: single
  1. IAS zone from motion sensor to off when timer finished
- id: '1640959316077'
  alias: light 1 timer
  description: when timer is idle 
    - IAS Zone off, light will be turned off by automation
  trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.light_1
  condition:
  - condition: state
    entity_id: binary_sensor.motion_1_ias_zone
    state: 'off'
  action:
  - type: turn_off
    device_id: eac48738360242f203d80dd36a0672e3
    entity_id: light.light_1
    domain: light
  mode: single

what I want is to have one single automation like:

- id: '101010101001'
  alias: light 1
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_1_ias_zone
    to: 'on'
 - platform: state
    entity_id: binary_sensor.motion_1_ias_zone
    to: 'off'
    for:
      hours: 0
      minutes: 1
      seconds: 0
  - platform: state
    entity_id: light.light_1
    to: 'on'
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.light_1
  condition: []
  action:
  - choose:
    - conditions:
      ...
      sequence:
      ...
    - conditions:
      ...
      sequence:
      ...
    - conditions:
      ...
      sequence:
      ...
    - conditions:
      ...
      sequence:
      ...
    default: []
  mode: single

thanks for your help!

I’d say, copy the conditions of your 4 automations to the options’ conditions of your “choose” and have fun. You might want to consider adding a trigger ID to your triggers (in the new automation) and include that as a condition in the option. It’s not as complicated as it might feel.

If it doesn’t work as expected, check the trace to see what’s happening. If you can’t figure out what goes “wrong”, drop the yaml and I am (as are others) happy to help.

Ohh and just keep the old automations disabled as back-up for a while. If that step is too big, you could also decide to build the new automation and instead of having actual actions, just let them generate a log line, to check/confirm the working of it.

Thanks for your answer.

Here is my code:

- id: '1645189641331'
  alias: Licht EG
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.motion_1_ias_zone
    to: 'on'
    from: 'off'
  - platform: state
    entity_id: binary_sensor.motion_1_ias_zone
    to: 'off'
    for:
      hours: 0
      minutes: 1
      seconds: 0
    from: 'on'
  - platform: state
    entity_id: light.light_1
    to: 'on'
    from: 'off'
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.light_1
  condition: []
  action:
  - choose:
    - conditions:
      - condition: state
        entity_id: binary_sensor.motion_1_ias_zone
        state: 'on'
      - condition: numeric_state
        entity_id: sensor.motion_1_illuminance
        below: '15'
      sequence:
      - type: turn_on
        device_id: eac48738360242f203d80dd36a0672e3
        entity_id: light.light_1
        domain: light
    - conditions:
      - condition: device
        type: is_on
        device_id: eac48738360242f203d80dd36a0672e3
        entity_id: light.light_1
        domain: light
      - condition: state
        entity_id: binary_sensor.motion_1_ias_zone
        state: 'off'
      sequence:
      - service: timer.start
        data:
          duration: '0'
        target:
          entity_id: timer.light_1
    - conditions:
      - condition: state
        entity_id: timer.light_1
        state: 'off'
      sequence:
      - type: turn_off
        device_id: eac48738360242f203d80dd36a0672e3
        entity_id: light.light_1
        domain: light
    - conditions:
      - condition: state
        entity_id: binary_sensor.motion_1_ias_zone
        state: 'off'
      sequence:
      - type: turn_off
        device_id: eac48738360242f203d80dd36a0672e3
        entity_id: light.light_1
        domain: light
    default: []
  mode: single

The problem I see is that when the light will be turned on and the motion sensor is not detecting a motion (state: off) the timer will start.

Timer:

2022-02-20 10_17_35-Configuration – Home Assistant - Vivaldi

After 10 seconds the timer stops and immediatly will start again, because the light still is on and the motion sensor is off… and so on…

With the 4 separate automations all is fine.
I know that this modification is a cosmetic thing only, but I am wondering why this is not working.

Hi,
I am just on my phone right now, so testing is hard for me.
But I have noticed in your code, that you are setting the duration of your timer to ‘0’.

I guess it should be duration: “00:00:10”

Regards

changed it to 10 seconds, but problem still exists

1 Like

I think trigger ids are your answer here. As @Recte says, assign a trigger id to each of the triggers, then in your actions > choose conditions, select ‘Trigger’ and input the trigger id (instead of using state conditions as you currently have).

@Recte and @fenty17
Thanks for your help, assigning a trigger id to the triggers was the solution (I overread this in the post of @Recte )

1 Like