Max Min Temperature for the last 24 hours

Hi

Does anyone know how to show the max and min temperatures from a single mqtt sensor for the last 24 hours.

I have looked at the statistic sensor and history statistic sensor but can only figure out how to do it for say the last 20 temperature updates.

Thanks in advance to anyone that can point me in the right direction.

1 Like

Did you change the sampling_size? See https://home-assistant.io/components/sensor.statistics/

Yeah but there is not a fixed number of temperature updates per day. Is there an option to sample by time rather than number of samples?

At first glance three options came to my mind:

@fabaff Python scripts seems useful, but we need some more real-world examples there for guidance.

I agree, Python scripts seem interesting but cant find any real detail about how to use them.

If you want max min since a certain time (eg since midnight), you could use a template sensor.

  1. Each time you get a temperature, compare it to the last maximum.
  2. Keep the higher of the results.
  3. Repeat.
  4. Clear via automation at midnight.

Do similar for minimum.

I do something like this for rainfall each day.

Off Topic, but how would one do that ?
I am curious about this.

Thanks

Just posted how I did 24 hour rain in its own topic as a bit of a how to:

Hello,
did you find some useful answer?

thank you.

I had the same requirements as the OP and went for a database solution since I already had Influxdb installed. Using the influxdb sensor, you can specify a query to run against the database and have the database engine calculate min/max/averages/percentiles/etc rather than HASS . It was a bit of a pain to find out all necessary fields for the query but once that was done it was quite simple to transfer the query to the sensor.

That sounds good! I have influxdb + grafana as well! I didnt know about influxdb sensor. It seem as the right thing! Any recomendations/pit falls?
Thank you.

I used the CLI of Influx to figure out the query, Influx is a bit different from the SQL databases I have used before.

Start the Influx CLI by typing influx in a terminal window, then select the correct database: use show databases followed by use database_name (that’s use homeassistant if you haven’t specified your own database name in the influx component declaration). show measurements to see available measurement series, in my case “°C”, and then show series to see available entities (in my case temperature_outdoor).

All this combines to a query, in my case:
SELECT median(values) FROM "°C" WHERE entity_id='temperature_outdoor' AND time > now()-24h

This query calculates a median value of all the temperature measurements from my outdoor thermometer recorded the last 24h and returns a single float value.

NB! The single quotes around temperature_outdoor in the query are very important, without them there will be no answer! You’ll have to escape them in the sensor declaration using more quotes, check the docs.

Check InfluxDB - Home Assistant on how to declare the influxdb sensor component and InfluxQL functions | InfluxDB OSS v1 Documentation for all available mathematical functions. You can find details on how to explore your database here: https://docs.influxdata.com/influxdb/v1/query_language/explore-schema/

I can paste my own sensor declaration later tonight, I don’t have it in front of me right now.

5 Likes

Great! Got it!
Thank you very much!

Here is my sensor declaration:

- platform: influxdb
    host: localhost
    queries:
      - name: "Outdoor median" 
        unit_of_measurement: °C
        value_template: '{{ value | round(1) }}'
        field: value #select
        group_function: median
        measurement: '"°C"' #from
        where: '"entity_id" = ''temperature_outdoor'' and time > now() - 12h' #where

I have also discovered that by replacing entity_id with friendly_name_str, one can use a sensor’s friendly name instead:
"friendly_name_str" = ''outdoor_temperature_sensor_friendly_name''

6 Likes