I’m not really sure this is the correct place to ask, but I’m kind of stuck with this for the past few days.
I’m trying to track the number of hours a certain temperature threshold is exceeded, using all historic data from my temperature sensors. Also, I don’t want to double-count the same hour if multiple temperature sensors are reading temperatures exceeding the threshold.
Kind of like so:
Let’s say the temperature threshold is 26.5 degrees Celsius. The living room temperature sensor reports a temperature exceeding the threshold between 12:00 and 16:00. The bedroom temperature sensor reports a temperature exceeding the threshold from 13:00 to 17:00. Without double-counting the same hours, the total amount of hours exceeding the threshold should then be equal to 5.
Is this possible? I sort of managed to do this by creating a custom sensor that continually tracks if a temperature sensor reports a temperature exceeding 26.5 degrees Celsius, and then returning a simple “True”. I then use another custom sensor that tracks the amount of time the first custom sensor returns “True”.
But that just tracks the amount of time since the custom sensor was created. It doesn’t check all historic temperature data I’ve gathered so far. That’s where I’m stuck. Any ideas? Any help would be much appreciated!
This makes sensor.high_temperature_hours just return 0 so far.
That should be much higher, because it’s been over 26.5 degrees Celsius in my home for over a week now.
Did I do something wrong?
As previously mentioned I wanted to include all previously recorded temperature data as well - not to start tracking anew, as I suspect is what’s happening currently.
But now sensor.high_temperature_hours returns 8778:24:36, which is slightly over 365 days.
That’s not right.
My temperature sensors data doesn’t go even back that far, and it’s also not over 26.5 degrees Celsius all year round here, and neither do both my sensor report so.
I use the following to track how many hours my PC is on during the day.
The template sensor defines ‘on’ (based on active power on the smart plug drawing current):
- sensor:
- name: "PC Power State"
unique_id: pc_power_state
state: >
{% if states('sensor.tz3000_okaz9tjs_ts011f_active_power_2')|int(0)>3%}
on
{%else%}
off
{%endif%}
and then this tracks the hours of ‘on’
- platform: history_stats
type: time
name: PC On Time
entity_id: sensor.pc_power_state
state: "on"
start: "{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}"
end: "{{ now() }}"
This resets every day, and only uses a single sensor, but it should be pretty simple to modify.
That is roughly what I did as well, as I explained in my original post. But that just allows for tracking the amount of time up until the custom sensor was created (in your case, your pc_power_state creation time). It doesn’t check all historic data gathered so far (in my case all temperature data I’ve recorded even before creating that custom sensor). That’s where I’m stuck.
I’m not sure you can do that without some creative SQL, as the sensor data simply won’t exist.
Also, how far back are you intending to go, as most people have their recorders set to only retain a relatively small number of days. The default is 10:
Thanks, that was helpful. I just set purge_keep_days to 365.25 days, and restarted Home Assistant. That is the amount of time I wish to go back. Now I still wish to gather all historic data from even before my custom sensors were created. Is that still possible?
The recommendation is not to use the recorder for long term storage.
There are threads on what you might use instead, such as this one which should get you started:
As for creating the ‘missing’ data, as you had your recorder set to the default, there is no data beyond the default 10 days… as far as I am aware, its gone.
Even if you’d already created your long term data storage option, the only way I can think of generating the data retrospectively might be to interact with the database directly using SQL, but this would require knowledge of the schema and you could easily mess things up badly.
At this point, given that you’re only talking about 10 days, I’d establish your long term storage method now and just let it play out.
Modify your History Stats Sensor to record from the start of the day (midnight) to the current time. The today_at() filter reports the start of the day.
This will now report the number of high temperature hours for the current day. To accumulate this value on a daily/weekly/monthly/yearly basis, use the Utility Meter integration. It stores its data in a separate table in the database that is exempt from purging and meant for long-term storage.