Easier way to use combined motion sensor and switch

Greetings to all Home Assistant enthusiasts,

As a newcomer to this community, I am eager to enhance my automation skills.

My setup:

  • A living room with a few lights
  • one motion sensor
  • one switch

My goal:

  • when entering the room, the occupancy should change to occupied, and turn on the lights
  • when hitting the switch, the light should turn off and on manually (turning on is rarely used, because the motion sensor should detect the occupancy before)
  • all lights should turn off, when the room is not occupied for 1’

My challenge:

  • When pressing the switch and the light turns off, and I close the door afterward, the motion detector sometimes sees the door closing and turns on the light again (that’s why I added a 15s delay for the state)
  • I also think that my debug notification triggers too often

Way forward:
Most of the time, the automation works properly, but I have the feeling that this automation can be improved. Are they other ways to archive my goal? Are there any simpler ways to do that?

This is what I have now:

alias: Living Room Light On/Off
description: ""
trigger:
  - type: occupied
    platform: device
    device_id: 3008f9cdf835637391c638e4d8852945
    entity_id: binary_sensor.occupancy
    domain: binary_sensor
    id: living room occupied
  - type: not_occupied
    platform: device
    device_id: 3008f9cdf835637391c638e4d8852945
    entity_id: binary_sensor.occupancy
    domain: binary_sensor
    for:
      hours: 0
      minutes: 1
      seconds: 0
    id: living room not occupied
  - device_id: cf029b8b74d524dc07f0f123de6e019c
    domain: zha
    platform: device
    type: remote_button_short_press
    subtype: remote_button_short_press
    id: switch pressed
condition: []
action:
  - if:
      - condition: trigger
        id: switch pressed
    then:
      - service: light.toggle
        data: {}
        target:
          area_id: living_room
    else:
      - if:
          - condition: trigger
            id: living room occupied
        then:
          - if:
              - condition: state
                entity_id: light.living_room
                state: "off"
                for:
                  hours: 0
                  minutes: 0
                  seconds: 15
            then:
              - service: light.turn_on
                data: {}
                target:
                  area_id: living_room
            else:
              - service: notify.mobile_app_iphone_mk
                data:
                  message: Living Room is less than 15s in state "Off"
                  title: Debug Purpose
        else:
          - service: light.turn_off
            data: {}
            target:
              area_id: living_room
mode: single

Thank you very much for your help.

1 Like