Template Sensor History

Hi,

i have created a sensor with a value_template. It works fine but i have one issue, the history graph looks very raw, not like i expected from a power sensor. Because all many power sensor shows more granular i am sure it is my configuration. Hopefully someone can give me the right hint.

image

  - platform: template
    sensors:
      energie_solar_total_w:
        friendly_name: Solar (Total)
        unit_of_measurement: "W"
        device_class: power
        value_template: '{{ states("sensor.rct_power_storage_all_generators_power" )| float + states("sensor.rct_power_storage_ext_power") | float }}'

The template sensor should update whenever the source sensors change. Can you show a history panel screen shot of the following three sensors:

sensor.energie_solar_total_w
sensor.rct_power_storage_all_generators_power
sensor.rct_power_storage_ext_power

It could be that because you have not defined a a default value for your float filters that the template is generating errors.

Also by adding an availability template you could replace those spikes with short gaps.

Old format (which you should not be using for new sensors):

sensors.yam file

  - platform: template
    sensors:
      energie_solar_total_w:
        friendly_name: Solar (Total)
        unit_of_measurement: "W"
        device_class: power
        value_template: '{{ states("sensor.rct_power_storage_all_generators_power" ) | float(0) + states("sensor.rct_power_storage_ext_power") | float(0) }}'
        availability_template: '{{ states("sensor.rct_power_storage_all_generators_power" ) | is_number and states("sensor.rct_power_storage_ext_power") | is_number }}'

New format:

configuration.yaml file:

template:
  - sensor:
    name: Solar (Total)
    unit_of_measurement: "W"
    device_class: power
    state: '{{ states("sensor.rct_power_storage_all_generators_power" ) | float(0) + states("sensor.rct_power_storage_ext_power") | float(0) }}'
    availability: '{{ states("sensor.rct_power_storage_all_generators_power" ) | is_number and states("sensor.rct_power_storage_ext_power") | is_number }}'

Does OP also need to include state_class: measurement even though device_class is set to “power” or is that already covered?

No it’s optional.

Without: no long term statistics created but state history is created as normal.

With: creates long term statistics and state history.

@tom_I
Thanks a lot. At first i have changed the configuration into the new format. Sensor looks now like:
image
Every 30 seconds the values changed, but it is now like a flat line.
In history it is not better

If i stay on the sensor then it show everything very granular
image
After i reopen the box, it is again a flat line… something wrong with my history?
The devices i use for this sensor looks like that:
image

Hi. It’s strange. I also have “flat line” issue since couple of days. History for my sensors was working ok Walker. I haven’t changed my configuration.

Any help ?

Ok, i’ve found the source of the problem.
I had device_class and unit_of_measurement specified for sensors, there should be just device_class. After changing history works fine.

That’s unlikely to have been the cause. I have many sensors with both options specified. They graph just fine.

i have now add the state_class: measurement
now its looks like:
image

- sensor:
    name: Solar (Total)
    unit_of_measurement: "W"
    state_class: measurement
    device_class: power
    icon: mdi:solar-power
    state: '{{ states("sensor.rct_power_storage_all_generators_power" ) | float(0) + states("sensor.rct_power_storage_ext_power") | float(0) }}'
    availability: '{{ states("sensor.rct_power_storage_all_generators_power" ) | is_number and states("sensor.rct_power_storage_ext_power") | is_number }}'

I have the same issue. My template sensors history stay flat, but when I leave the history window open, I do see the changes. Once I re-open the history view, it’s a flat line again.

I have template sensors like this:

      - name: "Solar Panel Production W"
        unique_id: solar_panel_production_w
        unit_of_measurement: "W"
        state_class: measurement
        device_class: power
        icon: mdi:solar-power
        state: >
          {% set i1_dc_power = states('sensor.solaredge_i1_dc_power') | float(0) %}
          {% set i2_dc_power = states('sensor.solaredge_i2_dc_power') | float(0) %}
          {% set b1_dc_power = states('sensor.solaredge_b1_dc_power') | float(0) %}

          {% if (is_state('sensor.solaredge_i1_dc_power', 'unknown') or (is_state('sensor.solaredge_i2_dc_power', 'unknown')) or is_state('sensor.solaredge_b1_dc_power', 'unknown')) %}
            0
          {% elif (i1_dc_power + i2_dc_power + b1_dc_power <= 0) %}
            0
          {% else %}
            {{ (i1_dc_power + i2_dc_power + b1_dc_power) }}
          {% endif %}
        availability: >
          {{ states('sensor.solaredge_i1_dc_power') | is_number and states('sensor.solaredge_i2_dc_power') | is_number and states('sensor.solaredge_i1_ac_power') | is_number and states('sensor.solaredge_i2_ac_power') | is_number and states('sensor.solaredge_b1_dc_power') | is_number }}

Here’s a view of the history for that sensor. In the yellow area you can see that it does get data and the graph does update when the history view is opened:

image

I have no clue what’s going on.

I think I found my issue. I added a package from someone, and in that code, they had a section like the following:

recorder:
  include:
    entities:

If I understand correctly (I might be completely wrong here though), once you add that include part somewhere in the HA configuration, you need to add all of the entities that are to be recorded? I don’t have that (yet), so before adding the package code, my HA was storing history data for all of my entities, but once I added the package, only history for those entities listed in that package was stored.

I removed that part and now it seems to be back to normal.

I’m having a similar problem - I have created a template sensor and its not recording the history. I’m wondering if I have the same problem here but where do I look in HA to see if a package has added the piece of code you mention?

Thanks