Merging statistics and history stats?

I’ve recently been experimenting with the statistics and history stats capabilities, and I find my self either misunderstanding them, or wanting something that is a merge of the two.

A specific example. I have an external temperature sensor and I want to show in a dashboard the minimum overnight (say between 10pm and 6am) temperature. With “history stats” I can easily specify “overnight” (end at 6am, duration 8 hours) but I can’t do minimum. With “statistics” I can do minimum but can’t specify a time period that doesn’t end “now”.

I’d also like to be able to calculate the overnight temperature drop for the house. Again, time spec from “history stats” and distance_absolute characteristic from “statistics” would do the job nicely.

Does it make sense to talk about merging these somehow? If not a full merge, then at least adding all of the “state characteristics” to history stats seems like it would make sense.

Am I missing something? Is there a better way to calculate what I’m looking for?

I currently do some of this externally, with python/pandas code analysing the data in the history database, and then posting it back into HA, but that has problems of its own and doing it with native HA functionality would be much cleaner.

Thanks.,
Steve.

The only way I can think to do this would be with a triggered template sensor, e.g.

template:
  - trigger:
      - platform: state
        entity_id: sensor.your_temperature_sensor_here
        to: # null "to" triggers on any change of temperature sensor state, but not sensor attribute changes
      - platform: time
        at: "22:00:00" # also trigger at 10pm
    sensor:
      - name: "Night Minimum Temperature"
        device_class: temperature
        state: >
          {% if now() == today_at("22:00") %}
            {{ states('sensor.your_temperature_sensor_here') }}
          {% elif 22 <= now().hour < 6 %}
            {{ [states('sensor.your_temperature_sensor_here')|float(none), states('sensor.night_minimum_temperature')|float(none)]|min }}
          {% else %}
            {{ states('sensor.night_minimum_temperature') }}
          {% endif %}

At 10pm the current temperature is put into the sensor (reset the sensor). Between 10pm and 6am the minimum temperature is kept. After 6am the sensor does not change.

Certainly being able to use one or other of the expanded integrations you mentioned would be easier. Don’t forget to vote for your own request.

Thanks for that. I’d have never figured out the triggered template mechanism myself!

If if was for just one sensor (external temp) that I wanted this, I’d be happy with your solution I think. But I have temp sensors in multiple rooms of the house and I’m interested in looking at which rooms cool down the fastest overnight, for example, and a collection of “history_stats” sensors with a “type” of “difference_absolute” would be much easier to maintain in bulk :slight_smile:

Steve.

1 Like