Trying to track the number of hours a certain temperature threshold is exceeded

Hi all,

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! :slight_smile:

  1. Add all your temperature sensors to this integration with a type of max:
  1. Use that sensor in a binary template sensor that turns on whenever the maximum exceeds your threshold.
  1. Count the hours the binary sensor is on with the history stats sensor:

Hi @tom_l,

Thanks for your reply.
I tried it like so, but it doesn’t really work yet. In my sensor.yaml:

- platform: min_max
  name: max_temperature
  entity_ids:
    - sensor.vardagsrumtemperatursensor_temperature
    - sensor.sovrumtemperatursensor_temperature
  type: max

- platform: template
  sensors:
    high_temperature_alert:
      friendly_name: "High Temperature Alert"
      value_template: >-
        "{{ states('sensor.max_temperature')|float >= 26.5 }}"

- platform: history_stats
  name: "High Temperature Hours"
  entity_id: sensor.high_temperature_alert
  state: "True"
  type: time
  start: "{{ now().replace(hour=0, minute=0, second=0) - timedelta(days=365.25) }}"
  end: "{{ now() }}"

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.

You have used a template sensor. Use a binary template sensor.

Hi @tom_l ,

Sorry, I think I’m still doing something wrong.

This is (part of) my configuration.yaml:

[...]
sensor: !include sensor.yaml
template: !include template.yaml
[...]

This is my sensor.yaml:

- platform: min_max
  name: max_temperature
  entity_ids:
    - sensor.vardagsrumtemperatursensor_temperature
    - sensor.sovrumtemperatursensor_temperature
  type: max

- platform: history_stats
  name: "High Temperature Hours"
  entity_id: binary_sensor.high_temperature_alert_binary
  state: "on"
  type: time
  start: "{{ now().replace(hour=0, minute=0, second=0) - timedelta(days=365.25) }}"
  end: "{{ now() }}"

And this is my template.yaml:

- binary_sensor:
  - name: "High Temperature Alert Binary"
    state: "{{ states('sensor.max_temperature') | float >= 26.5 }}"

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.

What am I doing wrong?

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.

1 Like

Hi @Rofo,

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:

Hi @Rofo,

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.

- platform: history_stats
  name: "High Temperature Hours"
  entity_id: binary_sensor.high_temperature_alert_binary
  state: "on"
  type: time
  start: "{{ today_at() }}"
  end: "{{ now() }}"

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.

There’s an example here:

2 Likes