How to merge 3 motion controlled light automations into 1

hi there, I have created 3 simple automations to control the light in the restroom based on motion. I am struggling to merge these automations into just one automation. Hopefully anyone can provide me input how it can be achieved.

First automation is the turn the light on when motion is detected:

alias: Verlichting Toilet Inschakelen
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.pir_toilet_beweging
    from: "off"
    to: "on"
condition: []
action:
  - service: light.turn_on
    metadata: {}
    data:
      brightness_pct: 100
      color_temp: 500
    target:
      entity_id: light.toilet
mode: single

Second automation is to dim the light after two minutes of inactivity:

alias: Verlichting Toilet dimmen
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.pir_toilet_beweging
    to: "off"
    for:
      hours: 0
      minutes: 2
      seconds: 0
condition: []
action:
  - service: light.turn_on
    metadata: {}
    data:
      brightness_pct: 75
    target:
      entity_id: light.toilet
mode: single

Third automation is to turn the light of after two and a half minute of inactivity:

alias: Verlichting Toilet uitschakelen
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.pir_toilet_beweging
    to: "off"
    for:
      hours: 0
      minutes: 2
      seconds: 30
condition: []
action:
  - service: light.turn_off
    metadata: {}
    data: {}
    target:
      entity_id: light.toilet
mode: single

Trigger ids is a good way of consolidating.

1 Like

Trigger IDs are what you need. Mine is a little bit long because I adjust lights based upon time of day sensors, but same general gist…

alias: "Lighting: Bathroom Lights On/Off"
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.bathroom_motion_sensors
    to: "on"
    enabled: true
    id: motion_on
  - platform: state
    entity_id:
      - binary_sensor.bathroom_motion_sensors
    to: "off"
    enabled: true
    for:
      hours: 0
      minutes: 7
      seconds: 0
    from: "on"
condition: []
action:
  - alias: Determine what triggered it
    if:
      - condition: trigger
        id:
          - motion_on
    then:
      - choose:
          - conditions:
              - condition: state
                entity_id: input_boolean.good_night
                state: "on"
            sequence:
              - service: scene.turn_on
                target:
                  entity_id: scene.bathroom_overnight
                metadata: {}
          - conditions:
              - condition: state
                entity_id: binary_sensor.mode_manager_early_morning
                state: "on"
            sequence:
              - service: scene.turn_on
                target:
                  entity_id: scene.bathroom_early_morning
                metadata: {}
          - conditions:
              - condition: state
                entity_id: binary_sensor.mode_manager_morning
                state: "on"
            sequence:
              - service: scene.turn_on
                target:
                  entity_id: scene.bathroom_morning
                metadata: {}
          - conditions:
              - condition: state
                entity_id: binary_sensor.mode_manager_afternoon
                state: "on"
            sequence:
              - service: scene.turn_on
                target:
                  entity_id: scene.bathroom_afternoon
                metadata: {}
          - conditions:
              - condition: state
                entity_id: binary_sensor.mode_manager_evening
                state: "on"
            sequence:
              - service: scene.turn_on
                target:
                  entity_id: scene.bathroom_evening
                metadata: {}
          - conditions:
              - condition: state
                entity_id: binary_sensor.mode_manager_night
                state: "on"
            sequence:
              - service: scene.turn_on
                target:
                  entity_id: scene.bathroom_night
                metadata: {}
        default: []
    else:
      - service: scene.turn_on
        metadata: {}
        target:
          entity_id: scene.bathroom_off
max_exceeded: silent
mode: restart
1 Like

put all the triggers together and then choose actions using ‘choose’

1 Like

Thanks for the quick input everybody! Exactly what I needed :slight_smile:

I have now merged the automations into one automation:

alias: Verlichting Toilet
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.pir_toilet_beweging
    from: "off"
    to: "on"
    id: toilet-active
  - platform: state
    entity_id:
      - binary_sensor.pir_toilet_beweging
    to: "off"
    for:
      hours: 0
      minutes: 2
      seconds: 0
    id: toilet-pending
  - platform: state
    entity_id:
      - binary_sensor.pir_toilet_beweging
    to: "off"
    for:
      hours: 0
      minutes: 2
      seconds: 30
    id: toilet-inactive
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - toilet-active
            alias: Motion detected
        sequence:
          - service: light.turn_on
            metadata: {}
            data:
              color_temp: 500
              brightness_pct: 100
            target:
              entity_id: light.toilet
      - conditions:
          - condition: trigger
            id:
              - toilet-pending
        sequence:
          - service: light.turn_on
            metadata: {}
            data:
              brightness_pct: 70
            target:
              entity_id: light.toilet
        alias: Motion undetected for 120 seconds
      - conditions:
          - condition: trigger
            id:
              - toilet-inactive
        sequence:
          - service: light.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: light.toilet
        alias: Motion undetected for 150 seconds
mode: single
2 Likes