My children ( 22 years) are using heater with thermostat header to dry her winter closes.
Sometimes she forgot to unplug heater.
I monitor heater power usage and created template sensor to show status on/off.
Is there any way to triger alarm when heater is plugged in more them 5 hours and the status sensor was one or more times “on” every hour in past 5 hours?
I can think of a way to do it, but it’s not straight forward…
trigger:
- trigger: state
entity_id: binary_sensor.YOUR_STATUS_SENSOR
to: 'on'
from: 'off'
conditions: []
actions:
- action: sql.query
data:
query: |+
SELECT
states.last_updated_ts
FROM
states
INNER JOIN states_meta ON
states.metadata_id = states_meta.metadata_id
WHERE
states_meta.entity_id = 'binary_sensor.YOUR_STATUS_SENSOR'
AND state = 'on'
AND last_updated_ts >= strftime('%s', 'now', '-5 hours')
ORDER BY
last_updated_ts DESC
response_variable: updates
- condition: template
value_template: |
{{ updates.result | map(attribute="last_updated_ts")
| map('as_datetime') | map(attribute="hour") | unique
| list | count == 5 }}
- action: notify.YOUR_NOTIFIER
data:
message: The heater has been on for more than 5 hours
That’s true. I assumed by the way you phrased the original question, “the status sensor was one or more times “on” every hour in past 5 hours”, that it would not be “on” continuously.
You’re probably going to need to come at this from a couple different angles to get what you want.
Ideas:
A basic State trigger with a duration would cover cases where the heater is “on” continuously, but will fail if there are any state changes within the defined duration.
A History Stats sensor could be used to measure how many (or what ratio) of the last 5 hours the heater has been on, or how many times the heater was turned “on” over that time span.
There’s also a custom integration, Measure It, that combines some of the features of History Stats, Utility Meter, and Template Sensors that you might be able to use.