I have a Utility Meter that I use to calculating the savings I make each month from using solar; however this gets purged every 10 days.
Therefore I want to save the savings as long term statistics, so that they stay available for reporting.
I think the way to do this, is to create a template sensor and have looked at this resource on how to template the sensor, however it is still getting purged.
I’ve made sure I have the State Class and Unit of Measurement , but obviously I’m missing something!
The state history will always be purged after the recorder keep days.
To access LTS try adding your sensor to a statistics card (rather than a history graph card). This should display values for loner than 10 days.
If you want access to the LTS value for automations you will need to create an SQL sensor to retrieve this value.
Note: you probably don’t need to create a new sensor to record LTS. You can add the state class to the utility meter sensor with customize - but check first that the sensor does not already have a state class, look in Developer Tools → States, right hand “attributes” column for your sensor. I’m guessing that it does.
However, if I look at the history of the meter, it only goes back 5 days (which is what I currently have the purge duration set to while I troubleshoot - rather than the default 10 days)
That is the state history it will always be purged. That is not long term statistics. To see the long term statistics add your sensor to a statistics card.
I don’t suppose there is any way to get the LTS in another card? Ideally I want to get them into the apex-chart card, to be able to present the data like this:
If you want to retrieve the LTS values from the database for use with other cards you will have to create an SQL sensor. I have no idea of the specifics though.
So incase it’s useful for someone… I started looking into SQL sensors and using them with Apex Charts; then I discovered that Apex Charts natively supports LTS.
To use them you need to use the statistics option.
Below is an example of it in my card.
Thanks @tom_l, it was only with you pointing me in the right direction that I found this.
series:
- entity: sensor.battery_savings_daily
name: Battery So Far Today
color: '#5692A9'
type: column
float_precision: 2
statistics:
type: state
period: day
align: end
So far it has not been given a tag, and if no tag, it has thus not been allocated to anyone.
In the issue report I basically tell, that the process of creating a LTS sensor does not seem to work the way e.g. you told here. I have also mentioned, that I have also tested it with various cards, because all cards do not display LTS data.
Now, I do not believe that the one who decides to whom tasks should be allocated does not know anyone this should belong to. Instead I believe there perhaps is something missing from my issue report.
Can you check, what might this be? To me it looks quite simple: It is said that you get a LTS sensor by adding state_class. I have done that, it can be verified e.g. with Developer Tools/states and still no LTS. Should there be more to it? There are no log entries, errors or alike.
this seems to be an older post but maybe still relevant.
I seem to have a similar issue or at least some HA fundamentals I do not yet understand:
I had a sensor which did not write to LTS
I changed its state_class to ‘measurement’
this did not change its behavior (at least as far as I could verify it)
instead duplicating the existing sensor + adding the state class ‘measurement’ from the beginning delivered the expected results.
I wrote a question about this here:
however, until today nobody replied, maybe the question was to fuzzy.
I do not have a real problem because my workaround was to create a new sensor logging to LTS. But it seems that I am not the only one having a problem with LTS - at least understanding / applying the docs seems to be difficult/does not deliver the expected results. - maybe there is some real issue/bug in HA regarding this edge case?. Would be great to clarify this.