Help with energy sensor templates - Negative to positive

Hello All,

Can you guys please help me with below.

I have a utility meter, which reports the positive and negative values based on the usage. (Emporia Vue)

I’m trying to make two sensors using this - Positive sensor and Negative sensor (To use it on Energy Dashboard)

Below are the template sensors:

- platform: template
  sensors:  
    gridpowerin:
      friendly_name: Grid Power In
      unit_of_measurement: kWh
      device_class: energy
      state_class: total_increasing
      value_template: >
          {% if states('sensor.utility_meter_123_1d') | float(0) >= 0 %}
            {% set gridin = states('sensor.utility_meter_123_1d') | float(0) %}
          {% else %}
            {% set gridin = 0 %}
          {% endif %}
          {{ gridin }}
- platform: template        
  sensors:  
    gridpowerout:
      friendly_name: Grid Power Out
      unit_of_measurement: kWh
      device_class: energy
      state_class: total_increasing
      value_template: >
          {% if states('sensor.utility_meter_123_1d') | float(0) <= 0 %}
            {% set gridout = states('sensor.utility_meter_123_1d') |float | abs %}
          {% else %}
            {% set gridout = 0 %}
          {% endif %}
          {{ gridout }}

Configuration error:

Invalid config for [sensor.template]: [state_class] is an invalid option for [sensor.template]. Check: sensor.template->sensors->gridpowerin->state_class. (See ?, line ?).
Invalid config for [sensor.template]: [state_class] is an invalid option for [sensor.template]. Check: sensor.template->sensors->gridpowerout->state_class. (See ?, line ?).

If I remove the state_class, it works perfectly and reports the numbers perfectly as well. But im unable to use these new sensors in Energy dashboard as state_class is blank.

Can you guys please help?

Please format your pasted config as per: How to help us help you - or How to ask a good question

The legacy template sensor format does not support state_class. You should be using the new format for template sensors as the legacy template sensor format is no longer receiving new features.

Apologies, updated it now.

Im trying to understand the new way to do it, but getting confused on the formating.

I will try again in few ways

No that you have formatted it (thanks) I can easily show you an example:

template:
  - sensor:  
      - name: Grid Power In
        unit_of_measurement: kWh
        device_class: energy
        state_class: total_increasing
        state: >
          {% if states('sensor.utility_meter_123_1d') | float(0) >= 0 %}
            {{ states('sensor.utility_meter_123_1d') | float(0) }}
          {% else %}
            0
          {% endif %}
        availability: "{{ has_value('sensor.utility_meter_123_1d') }}"

      - name: Grid Power Out
        unit_of_measurement: kWh
        device_class: energy
        state_class: total_increasing
        state: >
          {% if states('sensor.utility_meter_123_1d') | float(0) <= 0 %}
            {{ states('sensor.utility_meter_123_1d') |float | abs }}
          {% else %}
            0
          {% endif %}
        availability: "{{ has_value('sensor.utility_meter_123_1d') }}"

Thank you for the solution.

Sorry if it’s basic, but am I supposed to change all my template sensors to a modern configuration?
Because I just copy pasted your code and my configuration is failing stating my old template sensors have issue

Error:
Error loading /config/configuration.yaml: while parsing a block collection
in “/config/sensor.yaml”, line 2, column 1
expected , but found ‘?’
in “/config/sensor.yaml”, line 132, column 1

Initial code of my sensor.yaml


#Number of lights on template
- platform: template
  sensors:
    lights_on_counter:
      friendly_name: Lights on counter
      value_template: >-
        {{ states.light
              |rejectattr('attributes.entity_id', 'defined')
              |selectattr('state', 'eq', 'on')
              |list
              |count
        }}
      icon_template: mdi:lightbulb-group
#Number of lights on template

It goes in your configuration.yaml file, not your sensors.yaml file. template: is its own integration like sensor: or automation:

You can put it in a separate file called templates.yaml if you put this in your configuration.yaml file:

template: !include templates.yaml

Ah, you are a rockstar.

Thanks for explaining patiently. This resolved all my issues with this.

1 Like