Creating a long-term statistics sensor

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!

Any thoughts/helps much appreciated!

      # Monthly Long Term Stats
      - name: "Solar Savings - Monthly Stats"
        unique_id: solar_savings_monthly_stats
        state: "{{ states('sensor.solar_savings_monthly') }}"
        unit_of_measurement: '£'
        icon: mdi:currency-gbp
        state_class: total_increasing

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.

2 Likes

Thanks for your help @tom_l

So did I read the developer docs (above link) incorrectly? i.e. adding state_class: total_increasing to the sensor will add it to LTS?

I will be displaying the LTS sensor data within an apexcharts-card

Thanks for the pointer… I’ll research the SQL sensor to see if that is required instead

No, that is correct. What I mean is that the utility meter sensor may have already had a state_class, and thus have been recording LTS.

Thanks @tom_l

Yeah you are correct, looking at the utility meter it already does have state_class: total_increasing configured.

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)

Would we expect to see data going further back? The meter was set up in mid-Feb.

Thanks for your help/advice!

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.

1 Like

Ahh ok @tom_l

I assume I couldn’t therefore use the apex charts card then to show the LTS?

Thanks again!

@tom_l

So yes, I see what you mean… as you said, I can get the LTS with the 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:

Thanks again for your time and help… really appreciate it

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.

1 Like

Thanks @tom_l, I’ll research that

Cheers for your help on the above… I understand this alot better now :+1:

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

8 Likes

Can you share the complete code?

No problem @Rodolfo_Vieira

Below is the code for that graph…

type: custom:apexcharts-card
header:
  show: true
  title: Daily Energy Savings
  show_states: true
  colorize_states: true
  floating: false
  disable_actions: true
graph_span: 31d
span:
  end: day
show:
  last_updated: true
stacked: true
apex_config:
  chart:
    height: 295
  stroke:
    show: false
  legend:
    show: false
yaxis:
  - min: 0
    max: ~10
    decimals: 0
    apex_config:
      tickAmount: 4
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
  - entity: sensor.solar_savings_daily
    name: Solar So Far Today
    color: '#F29C38'
    type: column
    float_precision: 2
    statistics:
      type: state
      period: day
      align: end
  - entity: sensor.battery_solar_savings_daily
    name: Total So Far Today
    color: '#598EBE'
    type: line
    float_precision: 2
    show:
      datalabels: true
    statistics:
      type: state
      period: day
      align: end
5 Likes

I have several command_line sensor, which I have assigned state_class and they are still not collecting LTS data.

I have reported an issue of this: command line sensors with state_class set do not collect LTS · Issue #100498 · home-assistant/core · GitHub

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.

I have also described the problem here Command line sensor => statistic sensor: History purged after 10 days

(contains also code samples)

hey,

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.

Dan