I’m trying to trigger recirculation on my water heater based on a state change of the presence detection on my beds. I want it to turn on if someone is out of bed for a couple mins after being in bed for an extended period, say three hours. I found the template below that meets my intention but I’m not sure how to integrate it into my established automation which I’ve included below.
recirculation yaml
{{ now().timestamp() - as_timestamp(states.DOMAIN.OBJECT_ID.last_changed) > 20*60 }}
The problem with using the template you have included is that the time_changed
property will change as soon as you get out of bed, so it would never be true.
The first thing I would do is to combine the kendras_bed
sensors to eliminate false triggers if she moves from one side of the bed to the other.
template:
- binary_sensor:
- name: sleepnumber_kendras_bed_kendra_is_in_bed
state: >
{{ expand('binary_sensor.sleepnumber_kendras_bed_kendra_left_is_in_bed',
'binary_sensor.sleepnumber_kendras_bed_kendra_right_is_in_bed') | selectattr('state', 'eq', 'on')
| list | count > 0 }}
Then, set up history stats sensors to track how many of the last X hours each person was in bed.
sensor:
- platform: history_stats
name: "Ryan was sleeping"
entity_id: binary_sensor.sleepnumber_master_ryan_is_in_bed
state: "on"
type: time
start: "{{ now() - timedelta(hours=3, minutes=5) }}"
end: "{{now()}}"
- platform: history_stats
name: "Melissa was sleeping"
entity_id: binary_sensor.sleepnumber_master_melissa_is_in_bed
state: "on"
type: time
start: "{{ now() - timedelta(hours=3, minutes=5) }}"
end: "{{now()}}"
- platform: history_stats
name: "Kendra was sleeping"
entity_id: binary_sensor.sleepnumber_kendras_bed_kendra_is_in_bed
state: "on"
type: time
start: "{{ now() - timedelta(hours=3, minutes=5) }}"
end: "{{now()}}"
Lastly, change the triggers, adding id’s and trigger variables where needed, and add conditions to check the variable.
alias: Recirculation start
description: ""
trigger:
- platform: state
id: home
entity_id: group.someones_home
to: home
- platform: state
entity_id:
- binary_sensor.sleepnumber_master_melissa_is_in_bed
- binary_sensor.sleepnumber_master_ryan_is_in_bed
- binary_sensor.sleepnumber_kendras_bed_kendra_is_in_bed
for:
hours: 0
minutes: 2
seconds: 0
to: "off"
variables:
was_sleeping: >
{% set x = "sensor."~ (trigger.entity_id).rsplit('_')[-4] | join ~"_was_sleeping" %}
{{ states(x) | float(0) >= 3 }}
- platform: state
id: button
entity_id:
- input_button.recirculation
condition:
- condition: state
entity_id: group.someones_home
state: home
- or:
- condition: trigger
id:
- button
- home
- "{{ was_sleeping if was_sleeping is defined else false }}"
action:
- service: rinnai.start_recirculation
data:
recirculation_minutes: 5
target:
device_id: 11f19cffcfad717b3c7dda8f23f061f4
mode: single
If it’s not already part of the recirculator’s function, you may want to consider adding a rate limiting condition as well to keep it from re-triggering if two people get out of bed with x minutes of each other.
Thanks for the response, finally able to get back to this after some time away.
So if I’m understanding this correctly below would get the total time spent in bed in the last 10 hours
sensor:
- platform: history_stats
name: "Ryan was sleeping"
entity_id: binary_sensor.sleepnumber_master_ryan_is_in_bed
state: "on"
type: time
start: "{{ now() - timedelta(hours=10) }}"
end: "{{now()}}"
and then if I wanted it to only trigger if that amount was more then 5 hours I would use
was_sleeping: >
{% set x = "sensor."~ (trigger.entity_id).rsplit('_')[-4] | join ~"_was_sleeping" %}
{{ states(x) | float(0) >= 5 }}
or am I complete confused. I also dont understand the change to the conditions, why is that needed? I want to understand the logic for the future.