Not sure if you want the door closing to turn the lights off if anyone in the room stays in the room and shuts the door.
Below is a good solution, there are five automations (#1-5), four helpers (A-D), and one template sensor.
I am a firm believer in keeping automations simple each automation does one main thing. Trying to pile everyting into one complex automation doesn’t buy you anything and makes is harder to change and enhance later on. Also using one complex automation instead of several does not speed anything up!
A. Set up a timer with a duration of 10 minutes.
Three automations:
- Automation that simply turns the lights on when the timer is started.
- A separate automation that simply turns the lights off when the timer is either finished or cancelled.
- One automation has two triggers, both of your sensors - the ONLY thing the automation does is to start the timer.
Note, when the timer is already running (in the above case say your timer has 5 minutes left on it), and an automation calls the timer to start, all that happens is the timer changes back to having 10 minutes left and continues it’s countdown. (Terrific, just what you need!)
I use this paradigm for my lights so that I can use the cheaper PIR based motion sensors. Then I also have:
B. *Number helpers which show up on the dashboard as sliders, for changing the timer durations
C. Drop-down helpers that just have the options “Enabled” or “Disabled” for controlling the automations
4. An automation that cancels the timer if the light is turned off.
It works beautifully so the lights go off only X minutes AFTER someone has left the room (you can make the timer much longer if you want, NOBODY is still for an hour for example lol).
D. And for wall switches that control lights, I have a helper which simply records the last datetime that the wall switch was turned off MANUALLY (physically).
Then you update the logic in your automations #1-3 such that they take into account A-D. FYI, the automation needed that is related to “D”, is, if someone turns off a wall switch (which then also cancels the timer) and then walks out of the room, the lights would just go back on while they are walking out (as the motion sensors restart the timer which would turn on the lights). Therefore I use the value in D in automations that will only restart the timer if the motion was detected at least 5 minutes AFTER the switch was last turned off manually! If someone walks back into the room and wonders why the lights didn’t go back on - I just respond that is because you had just turned them off!
And therefore you need to handle wall switches for lights a little different than say smart plugs for lights. If you disable an automation for a smart plug (that is connected to a table lamp for example), then the plug should be turned ON so that people are not frustrated when they try to turn on the lamp manually and nothing happens.
There are alot of nuances to it. Since I use the above in alot of scenarios for many rooms, in order to avoid duplicating code, all of the automations that I have that are triggered by movement for lights, all call the same script named “Motion Detected” shoiwn below. The automations pass three values to it (self explanatory below):
alias: Motion Detected
sequence:
- condition: template
value_template: "{{ is_state(enablement_input_selector_entity,'Enabled') }}"
- data:
duration: "{{ (states(timer_duration_entity)|int*60) }}"
target:
entity_id: "{{ timer_entity }}"
action: timer.start
mode: parallel
max: 1000
icon: mdi:run-fast
The format of the code in the automations to call the above script is as follows - in the below example I have several motion detectors that triugger the same automation - note how it calls the script at the end of the code:
alias: Basement Bulb Above Laundry Motion Detected (Laundry Motion Sensor)
description: ""
trigger:
- type: motion
platform: device
device_id: 45df66511055a79b2032af823776a2e4
entity_id: binary_sensor.basement_laundry_motion_sensor_motion
domain: binary_sensor
- type: motion
platform: device
device_id: 23f4d8a7a2527e8f5519ae6a6878a473
entity_id: 8b99ec6c5a61b509214477468fc70309
domain: binary_sensor
condition:
- condition: template
value_template: >-
{{ (float(as_timestamp(now())) -
float(states('input_text.last_manual_off_basement_laundry_ceiling'),0)) >
300 }}
action:
- data:
enablement_input_selector_entity: input_select.basement_automation_bulb_above_laundry_is
timer_duration_entity: input_number.basement_adjust_bulb_above_laundry_timer
timer_entity: timer.basement_bulb_above_laundry_timer
action: script.motion_detected
mode: parallel
max: 1000
Hope that helps!
(P.S. For A. Above* you would want another automation (#5) which restarts the timer with the new duration if the slider is changed, but ONLY if the timer is already active )
Oh and since I am killing myself to give you a complete answer - in HA the way to detect if a switch was turned off manually is:
I have this in my configuration.yaml (in the below case I have the below as a light but it really is a switch). And note, I only care about storing the time for when it was turned off, and turned off manually -
template:
# (Note I have many things underneath the template section of the configuration.yaml,
# and the below needs to be in that "template:" section - so that is how configuration.yaml is laid out):
#
# Basement Laundry Ceiling Light (for accessing these and with the last_changed value):
# {{ states('sensor.basement_laundry_ceiling_off_context') }}
# {{ states.sensor['sensor.basement_laundry_ceiling_off_context'].last_changed }}
#
- trigger:
- platform: state
entity_id: light.basement_laundry_ceiling_shelly_1l_relay
sensor:
- name: "Basement Laundry Ceiling Light Off Context"
state: >
{% set c_id = trigger.to_state.context.id %}
{% set c_parent = trigger.to_state.context.parent_id %}
{% set c_user = trigger.to_state.context.user_id %}
{% if states('light.basement_laundry_ceiling_shelly_1l_relay') == 'on' %}
n/a
{% elif c_id != none and c_parent == none and c_user == none %}
physical
{% elif c_id != none and c_parent == none and c_user != none %}
dashboard_ui
{% elif c_id != none and c_parent != none and c_user == none %}
automation
{% else %}
unknown
{% endif %}
unique_id: basement_laundry_ceiling_off_context
and that triggers this automation (#6):
alias: >-
Basement Laundry Ceiling Light Off Context -> If Done Manually -> Set Last
Manual Off Timestamp
description: >-
If the basement laundry ceiling light is turned off (manually), then make
sure the "last_manual_off_basement_laundry_ceiling" timestamp is updated
trigger:
- platform: state
entity_id:
- sensor.basement_laundry_ceiling_light_off_context
to: physical
condition: []
action:
- data:
value: >-
{{
as_timestamp(states.sensor['basement_laundry_ceiling_light_off_context'].last_changed)
}}
target:
entity_id: input_text.last_manual_off_basement_laundry_ceiling
action: input_text.set_value
- data: {}
target:
entity_id: timer.basement_bulb_above_laundry_timer
action: timer.cancel
mode: parallel
max: 1000