Feed historical data into Home Assistant database.
HomeAssistant architecture is built around polling (or pushing) data from devices, or data providers, in "real-time". Some data sources (e.g, energy, water or gas providers) can't be polled in real-time or readings are not accurate. However reading historical data, like last month consumption, it's possible and accurate. This module adds support to this.
This module uses the recoder
component and custom state creation to store states "from the past".
Current projects using this module:
Q. How it's accomplished?.
A. It's a relatively easy answer but needs to be broken into some pieces:
-
A new property for sensors:
historical_states
. This property holds a list ofHistoricalState
s which are, basically, astate
+datetime
(with tzinfo), so… the data we want. -
A new hook for sensor:
async_update_historical_states
. This method is responsible to updatehistorical_states
property.
This is the only function that needs to be implemented. -
A new method, implemented by HistoricalSensor class:
async_write_ha_historical_states
. This method handles the details of creating tweaked states in the past and write them into the database using therecorder
component of Home Assistant core.
Q. Something else?
A. Historical sensors can't provide the current state, Home Assistant will show "undefined" state forever, it's OK and intentional.
- Logo by Danny Allen (Public domain license) https://publicdomainvectors.org/es/vectoriales-gratuitas/Icono-de-configuraci%C3%B3n-del-reloj/88901.html