Hello! Small question here. I would like to turn all lights off if there is no motion for 15 minutes and no door was opened or closed at last 15 minutes
- id: "008_2"
alias: All lights off on motion
initial_state: true
trigger:
- entity_id: group.all_motions
platform: state
to: "off"
for:
minutes: 13
action:
- entity_id: group.all_real_lights
service: switch.turn_off
I have motion part covered, but cant really find how to check that doors were not opened or closed at last 15 minutes
A group won’t work well for this because it won’t necessarily react to state changes for individual entities. If I understand correctly, you have a bunch of door sensors and you just want to determine if they’ve remained in the same state, open or closed, for at least 15 mins? I would do it with the following:
An input_boolean that indicates when a door has changed state
Automations like these:
alias: Door state changed
trigger:
platform: state
entity_id: binary_sensor.door1, binary_sensor.door2, etc...
action:
- entity_id: input_boolean.door_state_changed
service: homeassistant.toggle
alias: Door state idle
trigger:
platform: state
entity_id: input_boolean.door_state_changed
for: '00:15:00'
action:
- do stuff...
I use groups for motion sensors, not door sensor. But I need to track doors too
Yes you got right my case with doors
And yes, your snippet with input boolean should work as I want
The following Template Trigger will evaluate to true when each group member’s last_changed is older than 13 minutes ago. In other words, each motion sensor has maintained its state (whether on or off) for at least 13 minutes.
- id: "008_2"
alias: All lights off on motion
initial_state: true
trigger:
- entity_id: group.all_motions
platform: template
value_template: >
{{ expand('group.all_motions')
| selectattr('last_changed', 'lt', now()-timedelta(minutes=13))
| list | count == 0 }}
action:
- entity_id: group.all_real_lights
service: switch.turn_off
Thanks so much for this solution. I wanted to adapt this to create a sensor basically to show if anyone is home. How could I expand this to show if group.all_motions has been off for 13 minutes (I want to be able to specify the specific state ) and also check if person.randperson is away as well for 13 minutes?
I am not sure how to add additional conditions to the template.
It isn’t clear to me why last_changed is coming back UTC for me, but I solved it by using utcnow() instead of now().
I also noticed that for my double doors, I had to use the individual sensors front_door_left and front_door_right instead of the front_door sensor group. In other words, the logic didn’t work if I tried to place a group sensor (front door) within a group sensor (outside doors), which I had initially done to save time.
When performing datetime arithmetic, as long as all values have a timezone, the calculation is performed correctly even if their timezone offsets aren’t identical.
The following example demonstrates that the difference between two datetime values is the same, regardless of which one is utc or local timezone.