Turn on/off the light based on motion sensor

I’ve been trying for a while to create an automation for turning a smart bulb (Tapo) on/off based on motion detected by a sensor (Ikea Zigbee motion sensor).

What do I want to achieve? I want to turn on the light (in the bathroom) between 7 PM and 12 AM at a brightness of 70%, and between 12 AM and 7 AM at a brightness of 10%, when the motion sensor detects presence. After detecting motion, the light should turn on for 2 minutes and then turn off.

I tried the following automation, but it doesn’t work correctly… and it doesn’t turn off the light after 2 minutes; it stays on continuously.

alias: Bathroom_NightMode
description: ""
trigger:
  - type: motion
    platform: device
    device_id: b345fwf0745rt1345853e7f43f4e605
    entity_id: binary_sensor.ikea_motion_sensor_occupancy
    domain: binary_sensor
condition: []
action:
  - if:
      - type: is_motion
        condition: device
        device_id: b345fwf0745rt1345853e7f43f4e605
        entity_id: binary_sensor.ikea_motion_sensor_occupancy
        domain: binary_sensor
      - condition: time
        before: "07:00:00"
        after: "00:00:00"
    then:
      - service: light.turn_on
        data:
          brightness_pct: 10
        target:
          entity_id: light.bathroom
      - delay: "00:02:00"
  - if:
      - type: is_motion
        condition: device
        device_id: b345fwf0745rt1345853e7f43f4e605
        entity_id: binary_sensor.ikea_motion_sensor_occupancy
        domain: binary_sensor
      - condition: time
        before: "00:00:00"
        after: "19:00:00"
    then:
      - service: light.turn_on
        data:
          brightness_pct: 70
        target:
          entity_id: light.bathroom
      - delay: "00:02:00"
mode: single

What’s wrong in this code and how can i fix it to work accordingly to my request?
Thank you all!

tried but still the same… light doesnt turn off after 2 mins.

description: "Turn on the light on motion, turn off after no motion for 2 minutes."
mode: restart
trigger:
  - platform: state
    entity_id:
      - binary_sensor.ikea_motion_sensor_occupancy
    from: "off"
    to: "on"
    id: "on_motion"
  - platform: state
    entity_id:
      - binary_sensor.ikea_motion_sensor_occupancy
    from: "on"
    to: "off"
    for:
      hours: 0
      minutes: 2
      seconds: 0
condition: []
action:
  - if:
      - condition: trigger
        id:
          - "on_motion"
    then:
      - choose:
          - conditions:
              - condition: time
                after: "00:00:00"
                before: "07:00:01"
            sequence:
              - service: light.turn_on
                data:
                  brightness_pct: 10
                target:
                  entity_id: light.bathroom
          - conditions:
              - condition: time
                before: "00:00:00"
                after: "19:00:00"
            sequence:
              - service: light.turn_on
                data:
                  brightness_pct: 70
                target:
                  entity_id: light.bathroom
    else:
      - service: light.turn_off
        data: {}
        target:
          entity_id: light.bathroom
1 Like

Message malformed: Unable to determine action @ data[‘action’][0][‘then’][0][‘choose’][0][‘sequence’][0]

What version of HA are you on? If it’s not 2024.8, then change action: to service:.

(I updated the block to pre 2024.8 code)

|Version|core-2024.6.2|
|Installation Type|Home Assistant Container|

It worked now!!! Light turned off after 2 mins.
Thank you very much, guys!

1 Like

I agree, i dont know what was wrong with that code and why it didnt work… but at least with the other solution it works. Thank you for support!