There are plenty of discussions on motion activated lights, but I have not seen a solution for my case. In addition to usual ‘turn on the light if a motion is detected’ statement, I would like to add 2 more requirements that make the task more complicated, comparing with usual examples on the internet:
- Keep the lights on while someone is still in the room
- Even if the lights were turned on using alternate ways (automations, wall switch, or via HA dashboard) it should be turned off eventually if there is no more motion.
Well, about a year ago I actually solved this using regular timers and 3 automations:
- If the light is turned on -> start (restart) the timer
- If a motion detected -> turn the lights on and start (restart) the timer
- If the timer is due -> turn off the lights
The only tricky point here is that HA motion sensor object triggers only on ‘off’->‘on’ switch, but does not trigger automations if it already in ‘on’ state. This means automation will switch lights off even if you are still in the room. The solution in this case was to use mqtt trigger, parse the payload, and fire the trigger when ‘occupied’ field is ‘true’ regardless of its previous state.
As I said, everything is working fine (I can share the config if someone needs it). The only problem I have is that I have to copy/paste these automations every time I need motion activated lights (I have 4 rooms with this config, and I would like to set up one more).
From the preamble to actual question. I am looking towards blueprints, so that I create only one record per room, and pass only switch name, sensor name, and a timeout. I would like to use a motion_light.yaml that comes out of the box, and make a few adjustments.
I already tried to google for ‘multiple automations in a blueprint’ so that I could reuse my 3 automations. Unfortunately it looks like the only single automation is possible. Moreover timers are not supported in blueprints, but fortunately it can be overcome with wait_for_trigger action.
Here is what I have so far:
blueprint:
name: Motion-activated Light (Improved)
description: Turn on a light when motion is detected.
domain: automation
source_url: https://github.com/home-assistant/core/blob/067f2d0098d13134284e442d75ae5f1015ae044c/homeassistant/components/automation/blueprints/motion_light.yaml
input:
motion_entity:
name: Motion Sensor
selector:
entity:
domain: binary_sensor
device_class: motion
light_target:
name: Light
selector:
target:
entity:
domain: light
no_motion_wait:
name: Wait time
description: Time to leave the light on after last motion is detected.
default: 120
selector:
number:
min: 0
max: 3600
unit_of_measurement: seconds
# If motion is detected within the delay,
# we restart the script.
mode: restart
max_exceeded: silent
trigger:
- platform: state
entity_id: !input motion_entity
from: "off"
to: "on"
- platform: state
entity_id: !input light_target
from: "off"
to: "on"
action:
- service: light.turn_on
target:
entity_id: !input light_target
- condition: template
value_template: '{{ trigger.payload_json.occupancy == true }}'
- wait_for_trigger:
platform: state
entity_id: !input motion_entity
from: "on"
to: "off"
timeout: !input no_motion_wait
- service: light.turn_off
target:
entity_id: !input light_target
This code can turn off the light if it was turned on by a motion, or with a switch, or somehow else. But this code does not keep lights on while someone is in the room.
Question:
Can I somehow specify a trigger that will fire in one of these cases?
- mqtt message with trigger.payload_json.occupancy == true
- light state is turned on
Simplifying a long description above, I can rephrase my question as follows:
- Can I somehow specify a condition right in the mqtt trigger block? Something like this
trigger:
- platform: mqtt
topic: zigbee2mqtt/vestibule_motion_sensor
value_template: '{{ trigger.payload_json.occupancy }}'
to: "true"