There are several topics around dynamically creating entities externally - mostly through Appdaemon - and not being able to persist the corresponding state during HA restarts. I also ran into this problem. However, I believe there is a easy solution for this - and potentially other problems.
The recorder currently records each and every entity, even dynamically created ones. I.e. in Appdaemon you can easily read the last state of dynamically created entities once the app restarts (e.g. after a HA restart):
if (not(self.entity_exists(entity))):
data = self.get_history(entity_id = entity)
if (len(data) > 0) and (len(data[0]) > 0):
state = data[0][-1]['state']
else:
state = <default value>
self.set_state(entity, state = state)
This works nicely for entities that frequently change their value so that the history database contains at least one state change. However, this doesn’t work at all for entities that did not change their value within purge_keep_days
. This is the case for many dynamic entities with are often times input entities, e.g. desired room temperatures. You don’t change them very often. If they are purged from the history database they fall back to the default value after a HA restart.
Therefore I propose a simple addition to the recorder
: add an option which keeps the latest state of every entity during purging even if it is outside the purge_keep_days
interval. This should be trivial as a simple SQL query can achieve this:
DELETE FROM states WHERE state_id IN
(SELECT state_id FROM
(SELECT state_id, last_updated, rank() OVER(PARTITION BY entity_id ORDER BY last_updated DESC) AS rank FROM states) a
WHERE a.rank > 1 AND a.last_updated < now() - INTERVAL '3 days')