Combine Motion / Door sensor

Hey

Currently all my hue sensor / switch / scenes are setup in hue it’s self rather than HA but I’m thinking I want to move these over.

In our bathroom often have the issue when having a bath the motion sensor turns off the lights which is frustrating- but as it’s the bathroom the door is always closed when in use so am wondering if I could do something like:

Motion sensor detects motion = turn lights on
Door goes from closed to open = turn lights off (maybe after a 5 second delay?)

Think this would solve the bathroom issues I have without putting in something like an occupancy sensor or the like.

Anyone done anything like this - I assume should be fairy straight forward?

Thanks
Marc

I have this from a long time ago, probably needing some review/improvement, but as it is working rock solid I don’t touched since a while…

alias: Bathroom - Lights - Motion based
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.bathroom_motion_dual_sensors_state
    to: "on"
action:
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: not
                conditions:
                  - condition: device
                    type: is_on
                    device_id: 3cdd74bfe34c054238c9d0f07cccffb5
                    entity_id: light.bathroom_ceiling_lights
                    domain: light
              - condition: numeric_state
                entity_id: light.bathroom_ceiling_lights
                below: 14
        sequence:
          - service: light.turn_on
            data:
              transition: 1
              brightness_pct: 15
            target:
              entity_id: light.bathroom_ceiling_lights
    default: []
  - choose:
      - conditions:
          - condition: not
            conditions:
              - condition: device
                type: is_on
                device_id: 3cdd74bfe34c054238c9d0f07cccffb5
                entity_id: light.bathroom_ceiling_lights
                domain: light
        sequence:
          - type: turn_on
            device_id: f1e32768b729bd9e1f146c4511f96613
            entity_id: light.bathroom_mirror_light
            domain: light
          - device_id: 3cdd74bfe34c054238c9d0f07cccffb5
            domain: button
            entity_id: button.bathroom_ceiling_lights_reboot
            type: press
    default: []
  - service: switch.turn_on
    data: {}
    target:
      device_id: ffa52bb00dbef0f6146c0fc0b93ef80f
  - wait_for_trigger:
      - platform: state
        entity_id:
          - binary_sensor.bathroom_motion_dual_sensors_state
        to: "off"
    continue_on_timeout: false
  - delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
  - choose:
      - conditions:
          - type: is_not_open
            condition: device
            device_id: a2e059c66daa6661b4793e090e03c994
            entity_id: binary_sensor.bathroom_door
            domain: binary_sensor
        sequence:
          - wait_for_trigger:
              - type: opened
                platform: device
                device_id: a2e059c66daa6661b4793e090e03c994
                entity_id: binary_sensor.bathroom_door
                domain: binary_sensor
    default: []
  - choose:
      - conditions:
          - type: is_open
            condition: device
            device_id: a2e059c66daa6661b4793e090e03c994
            entity_id: binary_sensor.bathroom_door
            domain: binary_sensor
          - condition: not
            conditions:
              - condition: device
                type: is_off
                device_id: 3cdd74bfe34c054238c9d0f07cccffb5
                entity_id: light.bathroom_ceiling_lights
                domain: light
        sequence:
          - choose:
              - conditions:
                  - condition: not
                    conditions:
                      - condition: device
                        type: is_on
                        device_id: 3cdd74bfe34c054238c9d0f07cccffb5
                        entity_id: light.bathroom_ceiling_lights
                        domain: light
                sequence:
                  - type: turn_on
                    device_id: f1e32768b729bd9e1f146c4511f96613
                    entity_id: light.bathroom_mirror_light
                    domain: light
            default: []
          - service: light.turn_on
            target:
              entity_id: light.bathroom_ceiling_lights
            data:
              transition: 5
              brightness_pct: 5
          - delay:
              hours: 0
              minutes: 0
              seconds: 15
              milliseconds: 0
    default: []
  - choose:
      - conditions:
          - type: is_open
            condition: device
            device_id: a2e059c66daa6661b4793e090e03c994
            entity_id: binary_sensor.bathroom_door
            domain: binary_sensor
          - condition: or
            conditions:
              - condition: device
                type: is_on
                device_id: 3cdd74bfe34c054238c9d0f07cccffb5
                entity_id: light.bathroom_ceiling_lights
                domain: light
              - condition: device
                type: is_on
                device_id: f1e32768b729bd9e1f146c4511f96613
                entity_id: light.bathroom_mirror_light
                domain: light
        sequence:
          - service: light.turn_off
            target:
              entity_id:
                - light.bathroom_ceiling_lights
                - light.bathroom_mirror_light
            data:
              transition: 5
    default: []
mode: restart

Note that I have 2 lights, so I use one as a backup in case the other one fails for some reason (very rarely). The ceiling light have a dimmer (Shelly) changing the brightness when someone opens the door and turns off after a few seconds.

I made a blueprint but it also controls a fan. You could modify it easily. I like to use timers, so you would need to create a couple of them.

This is how I solved it.

#in binary_sensors.yaml
  - platform: template
    sensors:
      toilet_occupancy:
        friendly_name: Toilet occuoancy
        device_class: occupancy
        delay_off:
          minutes: 2
        value_template: >-
          {{is_state('binary_sensor.adult_toilet_motion_sensor_occupancy', 'on') or is_state('binary_sensor.adult_toilet_door_sensor_contact', 'off')}}

There is also simple automation, Turn on or off lights when that sensor is on or off.
The light won’t go out even if you don’t move for a while because the door is closed