How to make a sensor keep LTS and last period LTS

Hello.

With the new Long term statistic data in HA i got confused on what sensors need to have LTS and how to make this.

For example, i have a daily energy sensor:

  daily_energy_offpeak:
    name: "Daily Energy Offpeak"
    source: sensor.electricity_meter_energieverbruik_tarief_1
    cycle: daily
  daily_energy_peak:
    name: "Daily Energy Peak"
    source: sensor.electricity_meter_energieverbruik_tarief_2
    cycle: daily

This sensor is having LTS with the following Attributes:

state_class: total_increasing
source: sensor.electricity_meter_energieverbruik_tarief_1
status: collecting
last_period: 32.785
last_valid_state: 18640.086
meter_period: daily
cron pattern: 0 0 * * *
last_reset: 2024-02-03T23:00:00.086648+00:00
unit_of_measurement: kWh
device_class: energy
icon: mdi:counter
friendly_name: Daily Energy Offpeak

I created a Template sensor to combine the 2 sensors (peak and offpeak) to make 1 total:

      daily_energy:
        friendly_name: "Daily Energy"
        unit_of_measurement: "kWh"
        value_template: "{{ states('sensor.daily_energy_offpeak')|float + states('sensor.daily_energy_peak')|float }}"

But this total ‘daily energy’ sensor has no LTS.
It’s Attributes:

unit_of_measurement: kWh
friendly_name: Daily Energy

I changed the Template to this:

      daily_energy:
        friendly_name: "Daily Energy"
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total_increasing
        value_template: "{{ states('sensor.daily_energy_offpeak')|float + states('sensor.daily_energy_peak')|float }}"

As described here: Sensor Entity | Home Assistant Developer Docs (home-assistant.io)

But this seems to do nothing, it does not keep LTS.

How can I make my sensor correct so that it keeps LTS? Do i have to do this in the Template or do i need to make a customization?

I also created a ‘yesterday_energy’ sensor with the same Templating:

      yesterday_energy:
        friendly_name: "Yesterday Energy"
        unit_of_measurement: "kWh"
        value_template: "{{ state_attr('sensor.daily_energy_offpeak','last_period')|float + state_attr('sensor.daily_energy_peak','last_period')|float }}"

With no LTS data

The legacy template sensor platform does not support state_class: (required for LTS) and will not be getting new features.

You should use the new template integration for new sensors.

configuration.yaml (not sensors.yaml)

template:
  - sensor:
      - name: "Daily Energy"
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total_increasing
        state: "{{ states('sensor.daily_energy_offpeak')|float + states('sensor.daily_energy_peak')|float }}"
        availability: "{{ has_value('sensor.daily_energy_offpeak') and has_value('sensor.daily_energy_peak') }}"
1 Like

Thank you :smiley: this is working and also shows up into the recorder now.

Do the old template sensors keep working or is it better to remake them all into the new template? (maybe better for performance or anything)

I can do the same for the yesterday_energy.

But now I am wondering, do i still need all those sensors from daily, weekly, monthly , yearly, yesterday, last week. and so on?
With the new statistic card, is it not better to make just one Yearly sensor and use the card to show day week month? :nerd_face:

Yes. There is no talk of them being depreciated. They will keep working. They are just not getting new features. Having said that I moved all mine to the new format because I had the spare time.

Depends if you want to do cost calculations for the periods I suppose.

1 Like

Will do this to, it’s good for me to keep busy with it and learn.

Damn, i forgot about that. :stuck_out_tongue: than i still need al the sensors indeed. I don’t think there is a way to do that with one sensor. Unless you can select periods of the history in a template.

I will update the whole script when i have the time and place it on the forum for the rest who maybe has use for it :slight_smile:

Is a default here not needed here anymore? :see_no_evil:

template:
  - sensor:
      - name: "Daily Energy"
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total_increasing
        state: "{{ states('sensor.daily_energy_offpeak')|float + states('sensor.daily_energy_peak')|float | round(1, default=0) }}"
        availability: "{{ has_value('sensor.daily_energy_offpeak') and has_value('sensor.daily_energy_peak') }}"

If the availability template evaluates to false the state template is not evaluated.

1 Like

Maybe i am wrong, but with ‘has_value’ the availability is still ‘true’ if the sensor has any other value like “unknown, none and unavailable, etc”

I tried changing the sensor status in Dev Tools (yes i know i have set a default else the template would not render but still wanted to see what happened:

  # Return
      - name: "Hourly Energy Returned"
        state: "{{ ( states('sensor.hourly_energy_returned_offpeak')|float(1) + states('sensor.hourly_energy_returned_peak')|float(1) ) | round(1) }}"
        availability: "{{ has_value('sensor.hourly_energy_returned_offpeak') and has_value('sensor.hourly_energy_returned_peak') }}" ## Wrong True when sensor has a string (abc, available, etc)
      
        availability: "{{ has_value('sensor.hourly_energy_returned_offpeak')|is_number and has_value('sensor.hourly_energy_returned_peak')|is_number }}" ## Is always True

        availability: "{{ ( has_value('sensor.hourly_energy_returned_offpeak') and has_value('sensor.hourly_energy_returned_peak') )|is_number }}" ## Is always True

        availability: "{{ is_number('sensor.hourly_energy_returned_offpeak') and is_number('sensor.hourly_energy_returned_peak')  }}" ## Wrong False when sensor has a number

        availability: "{{ states('sensor.hourly_energy_returned_offpeak')|is_number and states('sensor.hourly_energy_returned_peak')|is_number }}" ## Seems to work

        availability: "{{ ( states ('sensor.hourly_energy_returned_offpeak') and states('sensor.hourly_energy_returned_peak') )|is_number }}" ## Seems to work

        availability: "{{ ( is_number (states('sensor.hourly_energy_returned_offpeak') and states('sensor.hourly_energy_returned_peak')) ) }}" ## Seems to work

Must we use is ‘is_number’ for numberical values?

In the screenshot the hourly_energy_returned_offpeak is set to a string value