Hi, I am quite new to Home Assistant (using Homematic and ioBroker in the past). I am nearly ready with the new system, but I have one challenge I am really stumped about, maybe I am missing something very simpel or doing something the wrong way.
My system is sending a status email to me every night at 0:05 giving me some status information about the house (eg. the current charging state of the car etc.). This works great. I wanted to show how long two appliances (Washing Machine, De-Humidifier) were working the last day.
I implemented two History Stats Helpers for this, linked to the entities (eg Washing Machine linked to the main entity, State “in_use”) of the type “Time”. I use “{{ today_at(‘00:00’) }}” in the “End” field and 0:24:00:00 in the Duration.
This is working when I click on Submit and Update and it shows me eg. 3 hours and 3 minutes. But the next day, when the sensor shows zero. Only when I go to the helpers and manually go through the process of changing the History Stats Options it gets updated.
So where is my error? Do I have to enable some updating of the History Stats sensors?
I believe you can use start and duration instead of start and end.
So start at 00:00 and duration 25 hours.
Not sure what that will look like between 00 and 01 though.
But I agree with sending it before midnight.
Or transfer the value to a helper before midnight and then send the mail with the helper value instead.
By the way: With a little help from my AI-colleague I build a sensor with the SQL Addon with the following statement (it works even it the use-time starts before midnight the day before or ends after midnight today).
WITH boundaries AS (
SELECT
CAST(strftime('%s', DATE('now', '-1 day', 'localtime')) AS REAL) AS day_start,
CAST(strftime('%s', DATE('now', 'localtime')) AS REAL) AS day_end
),
ordered_states AS (
SELECT
s.state,
s.last_updated_ts,
LEAD(s.last_updated_ts) OVER (ORDER BY s.last_updated_ts) AS next_ts
FROM states s
JOIN states_meta sm ON s.metadata_id = sm.metadata_id
WHERE sm.entity_id = 'sensor.waschmaschine'
),
usage_periods_raw AS (
SELECT
os.last_updated_ts AS start_ts,
COALESCE(os.next_ts, strftime('%s', 'now')) AS end_ts
FROM ordered_states os
WHERE os.state = 'in_use'
),
usage_periods_clipped AS (
SELECT
MAX(upr.start_ts, b.day_start) AS clipped_start,
MIN(upr.end_ts, b.day_end) AS clipped_end
FROM usage_periods_raw upr
CROSS JOIN boundaries b
WHERE upr.end_ts > b.day_start
AND upr.start_ts < b.day_end
)
SELECT
COALESCE(ROUND(SUM(clipped_end - clipped_start)), 0) AS usage_seconds_yesterday
FROM usage_periods_clipped
WHERE clipped_end > clipped_start;
This is for “sensor.waschmaschine” and the state “in_use”, the result is in the column usage_seconds_yesterday