I have an entity from an integration which goes up and down multiple times a day. I would like to record the daily high and low so that it is easier to see longer term trends. I have done a bit with defining some of my own entites but I’m at a loss to know where to start with this one.
Has anyone done anything similar?
Could someone give me some pointers on an approach I could take?
1/ Use the inbuilt statistics functions. The negative is that you cannot (AFAIK) define a day boundary. What you will get is the last running period of stats. In your case 24 hrs.
config.yaml:
sensor:
- platform: statistics
name: "Bathroom humidity mean over last 24 hours"
entity_id: sensor.bathroom_humidity
state_characteristic: value_max
max_age:
hours: 24
2/ User SQL functions to get what you need. You could either get a daily snapshot of the previous day for display in a dashboard, or have SQL provide a table with the data for the given sensor for all the available date range.
2a/ Create a new SQL Integration entity changing the ‘WHERE states_meta.entity_id =’ value to whatever sensor you want to monitor:
2b/ If you are SQL savy, this query on the homeassistant database will provide a table of max/min values by date. Change the ‘WHERE states_meta.entity_id =’ value to whatever sensor you want to monitor. You will need to process them manually however using a SQL client.
SELECT MAX(CAST(state AS FLOAT)) AS ‘maximum’, MIN(CAST(state AS FLOAT)) AS ‘minimum’, FROM_UNIXTIME(last_updated_ts,’%Y-%m-%d’) AS ‘date’
FROM states
JOIN states_meta
ON states.metadata_id = states_meta.metadata_id
WHERE states_meta.entity_id = ‘sensor.grid_power’
GROUP BY FROM_UNIXTIME(last_updated_ts,’%j’) DESC
What do you want the sensor to report when you look at it during the day?
The max temp of yesterday
The max temp of today so far
In case 1 the history plot will be a single point each day. In case 2 there will be new values plotted every morning until the hottest part of the day is reached and then it will flat-line for the rest of the day.
For case 1 you can start with the statistics sensor Shane provided, and then create a trigger-based template sensor which takes a snapshot of that sensor at midnight every day.
For case 2 you don’t need the statistics sensor, you can do everything with a trigger-based template sensor. Give it two triggers: one on a state change of the entity you’re tracking, and the other at midnight. On midnight you set it equal to the entity you’re tracking, and the rest of the time you set it equal to max([this.state.state | float(-100), states('sensor.sensor_to_track') | float])
edit: I think something like the following:
template:
- trigger:
- platform: time
at: "00:00:00"
id: time
- platform: state
entity_id:
- sensor.openweathermap_temperature
id: state
sensor:
- name: Yesterday High Temp
unique_id: d93c32cb-0351-4313-b964-b214a9e6ae05
state: >
{% if trigger.id == 'time' and this.attributes.max_today is defined %}
{{ this.attributes.max_today | float }}
{% elif is_number(this.state) %}
{{ this.state }}
{% else %}
0
{% endif %}
attributes:
max_today: >
{% set sensor_value = states('sensor.openweathermap_temperature') | float %}
{% if (this.attributes.max_today is defined) and (trigger.id == 'state') %}
{{ max([ this.attributes.max_today | float, sensor_value ]) }}
{% else %}
{{ sensor_value }}
{% endif %}