How to record price long-term

Hello, I need to record the price of car fuel and archive it 10 years back. The standard sensor is deleted after about 10 days and gets a value every 360 seconds. So I created a sensor statistic, but it doesn’t behave correctly. Can someone help me?

  - platform: statistics
    entity_id: sensor.tank_ono_price_diesel
    name: tank_ono_price_diesel_stat
    unique_id: "tank_ono_price_diesel_stat"
    state_characteristic: average_linear
    sampling_size: 10
    max_age:
     days: 3652

The statistics sensor uses state data from the recorder database, that only goes back 10 days (by default).

So no, that max age wont work (also your sample size is too small, ten years @ update every 360s = 876,600 samples).

The easiest way to retain long term data is using Long Term Statistics. This is 5 minute max/min/avergage for 10 days, then after that it is down sampled to hourly max/min/average and kept forever.

To ensure LTS are kept for sensor.tank_ono_price_diesel it needs the following attribute:

state_class: measurement

If the integration supplying that sensor does not support state_class you should open an issue to get it added. Until then you can use customize to add the attribute.

You can’t (easily) use LTS in calculations in templates though. This method is only good for display in statistics graphs. What do you want to do with the 10 years of data?

Well, the sensor looks like this, the state_class: measurement is there but it still won’t last more than 10 days…

command_line:
  - sensor:
      name: tank_ono
      scan_interval: 360
      command: "python3 /config/python_scripts/ono/ono.py"
      json_attributes:
        - NATURAL_95
        - DIESEL

template:
    - sensor:
       unique_id: tank_ono_cena_natural95
       name: "Tank ono cena natural 95"
       state: "{{ (state_attr('sensor.tank_ono', 'NATURAL_95') | float / 100) | default('unknown') }}"
       state_class: measurement
       unit_of_measurement: "CZK"
    - sensor:
       unique_id: tank_ono_cena_diesel
       name: "Tank ono cena diesel"
       state: "{{ (state_attr('sensor.tank_ono', 'DIESEL') | float / 100) | default('unknown') }}"
       state_class: measurement
       unit_of_measurement: "CZK"
       device_class: monetary

Did you miss this:

Look at the history panel. I bet it goes back further than 10 days. Likewise if you use a statistics graph card (not a history graph card).

Yes, I want to display it mainly in graphs…
But if it is set that there will be one value per day, which is enough for me, after all, in 10 years there will not be so many of those values. In the worst case, I will put it for 5 years or I will make another aggregation, for example, from monthly statistics. But the question is how to do it?

thanks, this works fine, but I’d like to see more beautiful charts and these are: “type: custom:apexcharts-card”
is it possible to screw it in somehow?

Yes apex charts supports LTS: https://github.com/RomRider/apexcharts-card#statistics-options

don’t be angry, but I can’t integrate it into the code. can you send me some example for that apexcharts?

Well, I’ve already solved it. For example, the code will look like this:

  - entity: sensor.tank_ono_cena_diesel
    stroke_width: 2
    color: brown
    statistics:
      period: hour
      align: start
      type: mean
    group_by:
      func: last
    show:
      extremas: true
      header_color_threshold: true