Statistic Templates

Hi All,

Trying to setup the new energy information and my various items are all in different units of measurement, so I have created template sensors to align everything… but because of the new requirement that sensors be classed as ‘statistics’ for use in the energy component, none of my new templated sensors display as valid.

I saw that to create a sensor as a statistic, you need to add the

state_class: 'measurement'

but template sensors cannot have this attribute.

Can someone explain how to setup template sensors that can be used in statistics?

Simon.

Did you try adding the attribute with customize instead of directly to the config. This is not recommended but may work.

Unfortunately the answer is, wait patiently for support to be added.

If you can feed your template sensors to Utility Meters and use the utility meter outputs this may also be a workaround.

If you use the ‘modern’ style of template sensors instead of the legacy style, they can be added

example from my installation :

template:
  - sensor:
      - name: "House electricity energy"
        unit_of_measurement: 'kWh'
        state: >
          {{ (states("sensor.mains_consumed_energy") | float + states("sensor.upstairs_consumed_energy") | float) + (states("sensor.extra_consumed_energy") | float)  | round(2) }}       
        device_class: energy
        state_class: measurement      
        attributes:         
           last_reset: '1970-01-01T00:00:00+00:00'


Thanks for the ideas both of you.

I have one of the sensors (the house production one) working, the other two are setup the same way so unsure what the issue is.

Here is the code for those interested:

####################################################
#           Get the power values from solaredge
####################################################
sensor:
      energy_consume:
        #this is using solaredge website - 10 minute for consumption info
        value_template: "{{ states('sensor.solaredge_power_consumption')|float * 1000 }}"
        friendly_name: 'Energy Consumption'
        unit_of_measurement: 'W'

      energy_grid:
        #this is using solaredge website - 10 minute for consumption info
        value_template: "{{ states('sensor.solaredge_grid_power')|float * 1000 }}"
        friendly_name: 'Energy Grid'
        unit_of_measurement: 'W'

      energy_produce:
        #this is using solaredge website - 10 minute for consumption info
        value_template: "{{ states('sensor.solaredge_current_power')|float }}"
        friendly_name: 'Energy Production'
        unit_of_measurement: 'W'

####################################################
#           create integrals to get the kWh values
#################################################### 

  - platform: integration
    source: sensor.energy_consume
    name: energy_consume_hour
    unit_prefix: k
    unit_time: h
    round: 2

  - platform: integration
    source: sensor.energy_grid
    name: energy_grid_hour
    unit_prefix: k
    unit_time: h
    round: 2

  - platform: integration
    source: sensor.energy_produce
    name: energy_produce_hour
    unit_prefix: k
    unit_time: h
    round: 2

####################################################
#           now convert the integrals to measurement sensors
####################################################

template:
  - trigger:
      - platform: state
        entity_id: sensor.energy_consume_hour
      - platform: state
        entity_id: input_boolean.check_power
        from: 'off'
        to: 'on'
    sensor:
      - name: "House electricity consumption"
        unit_of_measurement: 'kWh'
        state: >
          {{ states("sensor.energy_consume_hour") | round(2) }}       
        device_class: energy
        state_class: measurement      
        attributes:         
           last_reset: '1970-01-01T00:00:00+00:00'

template:
  - trigger:
      - platform: state
        entity_id: sensor.energy_grid_hour
      - platform: state
        entity_id: input_boolean.check_power
        from: 'off'
        to: 'on'
    sensor:
      - name: "House electricity grid"
        unit_of_measurement: 'kWh'
        state: >
          {{ states("sensor.energy_grid_hour") | round(2) }}       
        device_class: energy
        state_class: measurement      
        attributes:         
           last_reset: '1970-01-01T00:00:00+00:00'

template:
  - trigger:
      - platform: state
        entity_id: sensor.energy_produce_hour
      - platform: state
        entity_id: input_boolean.check_power
        from: 'off'
        to: 'on'
    sensor:
      - name: "House electricity production"
        unit_of_measurement: 'kWh'
        state: >
          {{ states("sensor.energy_produce_hour") | round(2) }}       
        device_class: energy
        state_class: measurement      
        attributes:         
           last_reset: '1970-01-01T00:00:00+00:00'
1 Like

Oh great. See: https://community.home-assistant.io/t/automatically-convert-from-legacy-template-to-modern-template-format/327555/2 Guess I will be making the effort after all.

At least they can be in packages now :rofl:

Sigh. :weary: Wonderful.