Motion automation not working properly

When my (IKEA) motion sensor detects motion, it switches on an (IKEA) control outlet.
But when the time I configured (3:00 minutes) has passed, the motion sensor switches to CLEAR, but the control outlet is not switched off.
What am I doing wrong?

alias: motion living
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 174cf8d7727df509170ce3bccf75febf
    entity_id: binary_sensor.motion_sensor_1_motion
    domain: binary_sensor
    id: motion_detected
  - type: no_motion
    platform: device
    device_id: 174cf8d7727df509170ce3bccf75febf
    entity_id: binary_sensor.motion_sensor_1_motion
    domain: binary_sensor
    for:
      hours: 0
      minutes: 3
      seconds: 0
    id: motion_stopped
condition: []
action:
  - if:
      - condition: trigger
        id: motion_detected
    then:
      - type: turn_on
        device_id: a5ba2cf921b29eeb78afd409ee6a5ca7
        entity_id: switch.control_outlet_switch
        domain: switch
    else:
      - type: turn_off
        device_id: a5ba2cf921b29eeb78afd409ee6a5ca7
        entity_id: switch.control_outlet_switch
        domain: switch
mode: restart

Hey there,
Personally I think you have the ID’s right but I would use the choose command, as below.

alias: motion living
description: ""
trigger:
  - type: motion
    platform: device
    device_id: 174cf8d7727df509170ce3bccf75febf
    entity_id: binary_sensor.motion_sensor_1_motion
    domain: binary_sensor
    id: motion_detected
  - type: no_motion
    platform: device
    device_id: 174cf8d7727df509170ce3bccf75febf
    entity_id: binary_sensor.motion_sensor_1_motion
    domain: binary_sensor
    for:
      hours: 0
      minutes: 3
      seconds: 0
    id: motion_stopped
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: motion_detected
        sequence:
          - service: switch.turn_on
            data: {}
            target:
              entity_id: Your switch or something on
      - conditions:
          - condition: trigger
            id: motion_stopped
        sequence:
          - service: switch.turn_off
            data: {}
            target:
              entity_id: Your switch or something off
mode: single

Hope this helps

Did you check the automation traces?
Was it triggered when motion was cleared? If so, what do the traces say?

1 Like

The motion sensor will be required to switch to clear and stay that way for 3 mins, so how long does it take for you motion sensor to show the clear state after motion has stopped (cool down) as you will need to factor that in also.

So if you motion sensors takes for example 2 mins to show clear and then you have a for 3 mins then the total time before the light will turn off would be 5 mins.

Worth checking this cool down period.

It worked when I rebooted the server :sweat_smile:

For future reference, here’s a concise way to do the same thing:

alias: motion living
description: ""
trigger:
  - platform: state 
    entity_id: binary_sensor.motion_sensor_1_motion
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: binary_sensor.motion_sensor_1_motion
    from: 'on'
    to: 'off'
    for:
      minutes: 3
condition: []
action:
  - service: 'switch.turn_{{ trigger.to_state.state }}'
    target:
      entity_id: switch.control_outlet_switch