I want to create a sensor that gives me the total time a binary_sensor
was in the on
state during a specific window each day.
For context, I initially want to use this as a reminder to turn on the dishwasher at bedtime if it wasn’t run since dinner that day. i.e. @bedtime: notify if (dishwasher usage == 0) since 5PM today
On the history_stats documentation page, it mentions these examples:
This morning (6AM - 11AM) : starts today at 6, lasts 5 hours.
start: '{{ now().replace(hour=6, minute=0, second=0) }}'
duration:
hours: 5
Today : starts at 00:00 of the current day and ends right now.
start: '{{ now().replace(hour=0, minute=0, second=0) }}'
end: '{{ now() }}'
I’ve tried variants of both of those, and neither seems to work - I get a value of Unknown
for the sensor, and that never changes.
Here’s what I’ve tried:
Daily Dishwasher Usage (works, but this is for the whole day)
- platform: history_stats
name: Dishwasher Usage Today
entity_id: binary_sensor.dishwasher_in_use
state: 'on'
type: time
start: '{{ now().replace(hour=0, minute=0, second=0) }}'
end: '{{ now() }}'
After-Dinner Dishwasher Usage (5PM today to Midnight today; doesn’t work)
- platform: history_stats
name: Dishwasher Usage After Dinner 2
entity_id: binary_sensor.dishwasher_in_use
state: 'on'
type: time
start: '{{ now().replace(hour=17, minute=0, second=0) }}'
end: '{{ now().replace(hour=23, minute=59, second=59) }}'
After-Dinner Dishwasher Usage (5PM today to now; doesn’t work)
- platform: history_stats
name: Dishwasher Usage After Dinner 3
entity_id: binary_sensor.dishwasher_in_use
state: 'on'
type: time
start: '{{ now().replace(hour=17, minute=0, second=0) }}'
end: '{{ now() }}'
After-Dinner Dishwasher Usage (7 hour duration, starting at 5PM today; doesn’t work)
- platform: history_stats
name: Dishwasher Usage After Dinner 4
entity_id: binary_sensor.dishwasher_in_use
state: 'on'
type: time
start: '{{ now().replace(hour=17, minute=0, second=0) }}'
duration:
hours: 7
After-Dinner Dishwasher Usage (start at either now (if before 5PM) or 5PM today (if after 5PM) and end at now; seems to work so far?)
- platform: history_stats
name: Dishwasher Usage After Dinner 5
entity_id: binary_sensor.dishwasher_in_use
state: 'on'
type: time
start: "{% if strptime(now(), '%H:%M:%S').strftime('%H')|int < 17 %} {{ now() }} {% else %} {{ now().replace(hour=17, minute=0, second=0) }} {% endif %}"
end: '{{ now() }}'
The last one gives me a value, and I’ll see later today if it updates correctly in the evening. It seems overly complicated though. Is there a better way?