Template Sensor does not have Long Term Storage

I’ve created a sensor in yaml using template integration, with a state_class and unit_of_measurement. However the data does not go into long-term-storage. Data from other sensors, like a Zigbee temperature sensor, do go into LTS, so I do not think it is a problem with the record/history integration itself.

What change do I need to make to have my sensor data go into LTS?

Below I’ve attached my configuration.

heatpump_package.yaml:

template:
  - sensor:
      name: "Ecodan Daily COP"
      unique_id: "ecodan_dailycop"
      state: "{{ ((state_attr('sensor.ecodan', 'DailyHeatingEnergyProduced')|float) / (state_attr('sensor.ecodan', 'DailyHeatingEnergyConsumed')|float)) | round(2) }}"
      availability: "{{ (state_attr('sensor.ecodan', 'DailyHeatingEnergyProduced')|is_number) and (state_attr('sensor.ecodan', 'DailyHeatingEnergyConsumed')|is_number) }}"
      unit_of_measurement: "x"
      state_class: measurement

configuration.yaml:

# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

homeassistant:
  packages:
    melcloud_package: !include melcloud_package.yaml
    heatpump_package: !include heatpump_package.yaml

History: LTS works for some sensors, but not the template sensor

I’m on the latest version of HomeAssistant:

  • Core 2025.3.1
  • Supervisor 2025.03.2
  • Operating System 14.2
  • Frontend 20250306.0

Developer Tools: States tab

Developer Tools: Statistics tab

When did you setup that sensor for statistics, was it before March 1?

Based on your screenshots everything looks fine.

Yes they were setup before March. I set them up in the beginning of February, and also have restarted HA plenty of times.

Put that sensor in a Statistics Graph Card, does it show anything?

I’ve added the card, it only shows the last 10 days:

For some reason it seems you don’t have data from before that date, but it seems to be working correctly now. Maybe there was some config setup issue, or maybe you accidentally purged the records, I can’t say.

Lets just wait and see if it continues working going forward.

I believe 2025.3.1 required the value of unit_of_measurement to be valid for the chosen state_class. I don’t think x meets that requirement so the Template Sensor is excluded from long-term statistics.

Check Settings → Logs for any related error/warning messages. Also check Developer Tools → Statistics for any warnings about the Template Sensor.

Version 2025.3.2 removed that requirement and it now works like it did in the past. I suggest you upgrade then wait at least 2 days to see if the Template Sensor’s recorded history has increased beyond 10 days.


EDIT

I think I was wrong about all of that because I found the relevant PR and it applies to MQTT Sensor.

Nevertheless, check the places I mentioned for errors/warnings and consider upgrading to the latest patch release.

I think there’s a map between unit and device_class, I don’t think there would be one for state_class ?

You’re probably right; what I described wasn’t even for Template Sensor. :man_facepalming:

BTW, shouldn’t that Template Sensor include device_class?

I have always been under the impression that you need state_class, unit_of_measurement, and device_class to ensure inclusion in long-term statistics.

I think device_class is always optional.

Plenty of sensors could exist for which no existing device_class is a good match.

Even unit_of_measurement I think is optional, there are some sensors which are unitless.

I believe state_class is all that is required.

Settings Logs: there are 2 warnings not related to this sensor
Developer Tools Statistics: “no issue” (see also one of the first screenshots).

I’ve updated to 2025.3.2, just to see whether that makes a difference.

If you don’t see 11 days of recorded history tomorrow, I suggest adding device_class to the Template Sensor. Pick an appropriate value from here:

Adjust the value of unit_of_measurement accordingly.

The latest patch fixed the issue.

  • state_class: measurement is all that is necessary to store into LTS.
  • devices_class is not needed
  • unit_of_measurement: "x" works fine. Thus ‘invalid’ units are ok.

Thanks for all the help.

1 Like

When you say latest patch do you mean 2025.3.3 or HAOS 15?

HA manages that, not the os.

Thanks petro

@dodobird I think 2025.4.0 might break your sensor without a valid unit of measurement. Add check that sensor state classes are used only with valid unit of measurements