Differing Template Syntax

Hi All,
Just having a little difficulty understanding the different Template Syntax that appears in various examples.

The Difference between using,

        value_template:
          "{{ ( states('sensor.sdm220m_3_import_active_energy') | float | round(1)) -
          ( states('sensor.sdm220m_2_import_active_energy') | float | round(1))}}"

#As Opposed to:-

        state: >
          {{ states ('sensor.sdm220m_3_import_active_energy') | float(2) | round(2)
          - states ('sensor.sdm220m_2_import_active_energy') | float(2) | round(2)}}

I have got one of the templates I want working,

  # Calculate Inverter Production - Alternate Code
template:
  - sensor:
      - name: "Inverter_Production"
        unique_id: "inverter_net_prod_test"
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total_increasing
        state: >
          {{ states ('sensor.sdm220m_3_import_active_energy') | float(2) | round(2)
          - states ('sensor.sdm220m_2_import_active_energy') | float(2) | round(2)}}
        availability: >
          {{ states ('sensor.sdm220m_3_import_active_energy') | float(none) != none and
              states ('sensor.sdm220m_2_import_active_energy') | float(none) != none }}

This one doesn’t; errors at Classes etc when I check before reboot.

  # Calculate Inverter Production
  - platform: template
    sensors:
      net_production:
        value_template:
          "{{ ( states('sensor.sdm220m_3_import_active_energy') | float | round(1)) -
          ( states('sensor.sdm220m_2_import_active_energy') | float | round(1))}}"
        unit_of_measurement: "kWh"
        device_class: energy
        #state_class: total_increasing #Causes Error
        #state_class: measurement #Causes Error
        #attributes:
        #  last_reset: "1970-01-01T00:00:00+00:00"
        friendly_name: Inverter Net Production

Other one I have an issue with is this, which I think needs State.class: measurement to allow me to do a Max/Min statictic Graph.

# Calculate Inverter Power
sensor:
  - platform: template
    sensors:
      net_power:
        value_template:
          "{{ ( states('sensor.sdm220m_3_power') | float | round(1)) -
          ( states('sensor.sdm220m_2_power') | float | round(1))}}"
        unit_of_measurement: "W"
        device_class: power
        #state_class: measurement #Causes Error
        friendly_name: Inverter Net Power

The above does return the Net Power value, which I can Graph Like this, just as I do with my Main Eastron Meter…

But I can’t do the statistical Chart that I can do off the Eastron’s power reading.
Screenshot 2022-12-31 085151

I believe that may be due to the missing Measurement state.class.

Can’t anyone steer me in the right direction to get my head around this.

Thanks.

Not sure where you saw this but this isn’t valid. The reason doesn’t have anything to do with templates, its simply not valid YAML.

In YAML whitespace is crucial. Newlines are how you denote the end of one field and the beginning of the next. This example uses newlines incorrectly, what comes after the value_template field ends with a newline isn’t a new field, it’s just a random string. That’s invalid YAML.

This is how you properly handle it when the value of a field is a multiline string. You use the > character to tell YAML that what comes next is the value of this field and it can include newlines. This one is valid YAML.

This website is handy for explaining the ways of specifying multiline strings in YAML and the differences:

So while the Power Example I give is Invalid, it Does return an entity I can use.
But doesn’t like a states.class.

Say quite a few examples in discussions here, but essentially I should be going with the suggestion you mention.

Thanks.

Actually I just ran this one through an online YAML validator and it said its valid. Which is news to me, you used to have to use a multiline char. Perhaps a newer version of the YAML spec changed that? Interesting.

The reason it doesn’t like state class is because you are using the legacy format for specifying template sensors. Since it is legacy it no longer gets updated with newer features, like the ability to specify state_class. If you want to specify state_class for your sensor you must use the new configuration format

Thanks,

That’s the Clarity I’ve been looking for; hard to know what to follow when looking over post that span a few years.

Will now turn towards doing everything with the New format now that I know the other is Legacy.

Cheers.

1 Like

I had a look about & there’s a stack of Online Validators.

Is there one that would be recommended in relation to HA?
Even the one you used would be a good suggestion.

I just used this one. But to be clear I just googled and clicked the first link, don’t have any particular attachment to that one