Automation choose help

Hi All,
I’ve been pulling my hair out trying to figure out why my automation keeps getting skipped

I’m on the process of switching all my hue devices over to z2m and want to keep some of the functionality we’ve become use too, while also improve them slightly.

I have a motion sensor I would like to come on and turn off

  • first condition if the input switch is off then don’t run the automation, as we want the light to remain on.
    my choose conditions:
  1. Light on
    Wait 100 milliseconds to ensure it’s reading the latest LUX level
    If the LUX is below 10
  2. Light off
    if the light is on
    waiting 2mins and start to dim the light then turn it off

I added a restart to trigger the light to stay on if motion in detected again while it was running the turn off sequence.

Yet whatever I try it keeps skipping them both

- id: '1674393091966'
  alias: utility_motion
  description: ''
  trigger:
    - platform: state
      entity_id: binary_sensor.utility_sensor_occupancy
  condition:
     - condition: state
       entity_id: input_boolean.utility_motion_sensor
       state: 'on'
  action:
  - choose:
    - conditions:
      - condition: and
        conditions:
        - condition: state
          entity_id: binary_sensor.utility_sensor_occupancy
          state: 'on'
          for:
            milliseconds: 100
        - condition: numeric_state
          entity_id: sensor.utility_sensor_illuminance_lux
          below: 10
      sequence:
      - service: light.turn_on
        entity_id: light.utility
        data:
          brightness: 255
  - choose:
    - conditions:
      - condition: and
        conditions:
        - condition: state
          entity_id: binary_sensor.utility_sensor_occupancy
          state: 'off'
        - condition: state
          entity_id: light.utility
          state: 'on'
      sequence:
      - delay: '00:02:00'
      - service: light.turn_on
        entity_id: light.utility
        data:
          brightness: 128
      - delay: '00:01:00'
      - service: light.turn_off
        entity_id: light.utility
  mode: restart

I would really appericate all and any help

This condition will almost certainly never be true, its basically checking if the binary sensor has be turned on for exactly 200ms.

You want to remove the ‘for’ in this condition, maybe then add a delay action, then a further additional condition for the lux level, if that makes sense.

1 Like

Try this version:

- id: '1674393091966'
  alias: utility_motion
  description: ''
  trigger:
    - platform: state
      entity_id: binary_sensor.utility_sensor_occupancy
      from: 'off'
      to: 'on'
    - platform: state
      entity_id: binary_sensor.utility_sensor_occupancy
      from: 'on'
      to: 'off'
      for:
        minutes: 2
  condition:
    - condition: state
      entity_id: input_boolean.utility_motion_sensor
      state: 'on'
  action:
    - variables:
        light: light.utility
    - choose:
        - conditions:
            - "{{ trigger.to_state.state == 'on' }}"
            - condition: numeric_state
              entity_id: sensor.utility_sensor_illuminance_lux
              below: 10
          sequence:
            - service: light.turn_on
              target:
                entity_id: '{{ light }}'
              data:
                brightness: 255
        - conditions:
            - "{{ trigger.to_state.state == 'off' }}"
            - "{{ is_state(light, 'on') }}"
          sequence:
            - service: light.turn_on
              target:
                entity_id: '{{ light }}'
              data:
                brightness: 128
            - delay: '00:01:00'
            - service: light.turn_off
              target:
                entity_id: '{{ light }}'
      default: []
  mode: restart
1 Like

thank you both for your support.
I like the use of the conditions @123 but I need the motion to check against the latest Lux, which doesn’t get updated until motion is detected.

I was however able to get it working with the below

- id: '1674393091966'
  alias: utility_motion
  description: ''
  trigger:
    - platform: state
      entity_id: binary_sensor.utility_sensor_occupancy
      for:
        milliseconds: 100
  condition:
    - condition: state
      entity_id: input_boolean.utility_motion_sensor
      state: 'on'
  action:
  - choose:
    - conditions:
      - condition: and
        conditions:
        - condition: state
          entity_id: binary_sensor.utility_sensor_occupancy
          state: 'on'
        - condition: numeric_state
          entity_id: sensor.utility_sensor_illuminance_lux
          below: 10
      sequence:
      - service: light.turn_on
        entity_id: light.utility
        data:
          brightness: 255
    - conditions:
      - condition: and
        conditions:
        - condition: state
          entity_id: binary_sensor.utility_sensor_occupancy
          state: 'on'
      sequence:
      - service: light.turn_on
        entity_id: light.utility
        data:
          brightness: 255
    - conditions:
      - condition: and
        conditions:
        - condition: state
          entity_id: binary_sensor.utility_sensor_occupancy
          state: 'off'
        - condition: state
          entity_id: light.utility
          state: 'on'
      sequence:
      - delay: '00:02:00'
      - condition: state
        entity_id: light.utility
        state: 'on'
      - service: light.turn_on
        entity_id: light.utility
        data:
          brightness: 128
      - delay: '00:01:00'
      - service: light.turn_off
        entity_id: light.utility
  mode: restart

I also added a further condition after the delays to check if someone turn the light off earlier than expected.

again thank you both for your assistants

The example I posted checks if illuminance is below 10 just a few microseconds after occupancy is detected. If you need it to check illuminance a specific amount of time after motion is detected, say 150 milliseconds, simply change the first State Trigger to this:

  - platform: state
    entity _id: binary _sensor.utility_sensor_occupancy
    from: 'off'
    to: 'on'
    for:
      milliseconds: 150

Question for you: how did my example behave when you tested it?