Automation: Lamp On based on Motion - Not Working and totally confused why

I’ve been pulling my hair out, watched multiple YouTube tutorials about this, and read a few threads here about this. I’m trying to do something super basic. Turn a lamp on by the door when we walk in a room. Using an Aqara P1 motion detector which works perfectly. Lamp will turn on just fine after sunset as programmed. What isn’t working is the various attempts to have a timer turn it off after 10 min of no motion. Can someone please have a look at this code and explain what I’m missing? I’ve tried using a helper timer, and then I simplified it to just use the duration option within the automations UI.

id: '1724193245369'
alias: 'Office: Motion Detected Lamp On'
description: ''
trigger:
  - type: occupied
    platform: device
    device_id: ea5145a700240e9e4262e9665cc2b939
    entity_id: b16dc95c12ceda0889a8aca957674c87
    domain: binary_sensor
    id: Occupied
  - type: not_occupied
    platform: device
    device_id: ea5145a700240e9e4262e9665cc2b939
    entity_id: b16dc95c12ceda0889a8aca957674c87
    domain: binary_sensor
    for:
      hours: 0
      minutes: 10
      seconds: 0
    id: Not Occupied
condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  - condition: device
    type: is_off
    device_id: 39a8d16b7a09864aa4b60d10a7634da7
    entity_id: e59e0bea2b7f302a89b4a8f9d412fb8e
    domain: light
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Occupied
        sequence:
          - action: light.turn_on
            target:
              device_id: 39a8d16b7a09864aa4b60d10a7634da7
            data: {}
      - conditions:
          - condition: trigger
            id:
              - Not Occupied
        sequence:
          - action: light.turn_off
            target:
              device_id: 39a8d16b7a09864aa4b60d10a7634da7
            data: {}
mode: single

The second condition requires the light’s state to be off. That’s what is preventing the “Not Occupied” trigger from turning the light off (because the light is on).

The two triggers require different conditions:

  • The first trigger (motion detected) will turn the light on but only if it’s currently off.
  • The second trigger (no motion detected) will turn the light off but only if it’s currently on.

Here’s a simple way to do it using State Triggers, trigger variables, and a templated service call.

id: '1724193245369'
alias: 'Office: Motion Detected Lamp On'
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.your_motion_detector
    from: 'off'
    to: 'on'
    variables:
      is_true: "{{ is_state('light.your_light', 'off') }}"
  - platform: state
    entity_id: binary_sensor.your_motion_detector
    from: 'on'
    to: 'off'
    for:
      minutes: 10
    variables:
      is_true: "{{ is_state('light.your_light', 'on') }}"
condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  - condition: template
    value_template: '{{ is_true }}'
action:
  - action: 'light.turn_{{ trigger.to_state.state }}'
    target:
      entity_id: light.your_light
mode: single

Replace binary_sensor.your_motion_detector and light.your_light with the entity_ids of your entities.

1 Like

@123 thank you!

So the above code still didn’t work quite right. After a little massaging and tinkering, I ended up with this and it’s working perfectly.

alias: "Office: Motion Detected Lamp On"
description: ""
trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.office_lamp_timer
    id: Timer Finished
  - type: no_motion
    platform: device
    device_id: ea5145a700240e9e4262e9665cc2b939
    entity_id: 87520479ee0c93265a3b13905334b712
    domain: binary_sensor
    id: Motion Stopped
  - type: motion
    platform: device
    device_id: ea5145a700240e9e4262e9665cc2b939
    entity_id: 87520479ee0c93265a3b13905334b712
    domain: binary_sensor
    id: Motion Detected
condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Motion Stopped
        sequence:
          - action: timer.start
            target:
              entity_id: timer.office_lamp_timer
            data: {}
      - conditions:
          - condition: trigger
            id:
              - Motion Detected
        sequence:
          - action: timer.cancel
            target:
              entity_id: timer.office_lamp_timer
            data: {}
          - type: turn_on
            device_id: 39a8d16b7a09864aa4b60d10a7634da7
            entity_id: e59e0bea2b7f302a89b4a8f9d412fb8e
            domain: light
      - conditions:
          - condition: trigger
            id:
              - Timer Finished
        sequence:
          - type: turn_off
            device_id: 39a8d16b7a09864aa4b60d10a7634da7
            entity_id: e59e0bea2b7f302a89b4a8f9d412fb8e
            domain: light
mode: single