Template stopped working after upgrade

Hi,
I noticed that after a recent update of HA, my template stopped working.
I know that the syntacs has changed, but I can’t get it working again.
Anyone that can help me with this?
(I added a # to prevent configurtion.yaml from stopping)

# platform: template
#   -sensor:
#       pv_power_numeric:
#       value_template: "{{ states('sensor.inverter_instant_power') | float }}"
#       unit_of_measurement: "kWh"

If you are using the legacy format to define a Template Sensor, it should appear under the sensor: key and look like this:

  - platform: template
    sensors:
      pv_power_numeric:
        value_template: "{{ states('sensor.inverter_instant_power') | float(0) }}"
        unit_of_measurement: "kWh"

If you are using the modern format to define a Template Sensor, it should appear under the template: key and look like this:

  - sensor:
      - name: "pv power numeric"
        state: "{{ states('sensor.inverter_instant_power') | float(0) }}"
        unit_of_measurement: "kWh"

Reference: Template Sensor


EDIT

Correction. Modern format uses state not value_template.

The bit I’d add that caught me out recently (Value template can no longer reference attributes) is how certain templates/definitions break without default values. The impact of the break depending on the rest of the setup.

The examples above includes defaults for float that your original yaml doesn’t. My guess is the inverter sensor you’re using isn’t initialised yet in startup, that fails with float and leaves this sensor as null breaking something else.

I would recommend a trawl through your config to put defaults in across the board.

Hi Taras,
Thank you.
To be honest, I am struggling with Yaml, and I am afraid I could have used the legacy system and modern system together.
I am now trying to clean up the configurtion.yaml, as an example, I have created a new mqtt.yaml file to make things neater.
Should I create a separate template.yaml file to make it easier to maintain?
Could you check my configuration.yaml file for inconsistencies, maybe?
There are no passwords in that file, I keep them in secrets.yaml.

It would really help to let my configuration.yaml checked by someone with a bit more experience. :slight_smile:
Rob

If you mixed them in the sensors.yaml file then that will cause errors.

Template Sensors defined in legacy mode go in sensors.yaml.

Yes.

template: !include templates.yaml

Template Sensors defined in modern format go in templates.yaml`.

@123

So if I understand correctly, then creating a template.yaml would mean that I should use the modern mode.
But if I have created a sensors.yaml (like I have) that is Legacy mode?
I would assume that everything should go in modern mode, so I would need to convert my sensors.yaml file?

In my sensors.yaml file, I have a few of these (example):

  - platform: template
    sensors:
      gasmeter:
        value_template: '{{ states("sensor.gas_delivered_raw") | regex_findall_index("\((\d*\.?\d*)\)$") }}'
        friendly_name: gasmeter
        unit_of_measurement: m³
        icon_template: mdi:fire
        device_class: gas

What is the best thing to do to make the system future proof, and neat?

You can continue to have a combination of modern and legacy Template Sensors. Just keep them in separate files. You’re not obliged to convert your existing legacy Template Sensors into modern Template Sensors. The legacy format wasn’t officially deprecated, it simply won’t get any more enhancements. If you want to convert them to modern format, feel free to do so at your own pace (i.e. you don’t have to convert them all at once).

In my case, I continue to use the many legacy Template Sensors I have; when I need a new one I create it in modern format.

Hi Taras,
Thank you for your answer, really appreciated !
I create a template.yaml, but now get an error in the log about the template.

Error:

Template variable warning: 'dict object' has no attribute 'linkquality' when rendering '{{ value_json.linkquality }}'
15:56:52 – (WARNING) helpers/template.py - message first occurred at 13:46:20 and shows up 33 times
Invalid config for [template]: [value_template] is an invalid option for [template]. Check: template->sensor->0->value_template. (See /config/templates.yaml, line 0).
15:56:40 – (ERROR) config.py - message first occurred at 15:56:22 and shows up 3 times
TemplateError('UndefinedError: 'None' has no attribute 'attributes'') while processing template 'Template("{{states.sensor.watermeter.attributes["waterflow"] }}")' for attribute '_attr_native_value' in entity 'sensor.watermeter_flow'
13:45:48 – (ERROR) helpers/template_entity.py - message first occurred at 13:45:48 and shows up 2 times
Error while processing template: Template("{{states.sensor.watermeter.attributes["waterflow"] }}")
13:45:48 – (ERROR) helpers/template.py - message first occurred at 13:45:48 and shows up 2 times

part of configuration.yaml:


light: !include light.yaml
mqtt: !include mqtt.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
sensor: !include sensors.yaml
template: !include templates.yaml

homeassistant:
  customize: !include customize.yaml
  packages: !include_dir_named watermeter
  allowlist_external_dirs:
    - "/config/snapshot"

templates.yaml:

- sensor:
      - name: "pv power numeric"
        value_template: "{{ states('sensor.inverter_instant_power') | float(0) }}"
        unit_of_measurement: "kWh"

part of sensors.yaml:

  - platform: afvalinfo
    id: Vledderveen             
    resources: 
      - gft
      - kerstboom
      - pbd
      - restafval
      - trash_type_today
      - trash_type_tomorrow
    location: stadskanaal
    postcode: !secret postcode
    streetnumber: 25
    dateformat: '%d-%m-%Y'
    locale: 'nl'
    timespanindays: 365
    
  - platform: template
    sensors:
      gasmeter:
        value_template: '{{ states("sensor.gas_delivered_raw") | regex_findall_index("\((\d*\.?\d*)\)$") }}'
        friendly_name: gasmeter
        unit_of_measurement: m³
        icon_template: mdi:fire
        device_class: gas

image

Something is interfering here…

‘dict object’ has no attribute ‘linkquality’ when rendering ‘{{ value_json.linkquality }}’

The received value didn’t contain a linkquality key. You’ll have to investigate that.

[value_template] is an invalid option for [template].

That’s due to my mistake. My example of a modern format Template Sensor (posted above) suggested you can use value_template and that’s wrong. Modern format uses state (and I have corrected the example). Replace value_template with state in all of your modern format Template Sensors.

‘None’ has no attribute ‘attributes’') while processing template ‘Template(“{{states.sensor.watermeter.attributes[“waterflow”] }}”)’ for attribute ‘_attr_native_value’ in entity ‘sensor.watermeter_flow’

Error while processing template: Template(“{{states.sensor.watermeter.attributes[“waterflow”] }}”)

You may have entered the wrong entity_id because Home Assistant couldn’t find it. Is it sensor.watermeter or sensor.watermeter_flow or something else?

Thanks, I was able to fix this, and now the orange dot that notified me of template errors is gone.
I will look into the other errors later.
What do you suggest? Remove all the entries containing a template out of the sensors.yaml?
I can leave the other entries in there? Like these…:

Yes.