Creating template sensors - yaml problems - i don´t get it

syntax check is ok, but after restarting home assistant i get some errors:

Logger: homeassistant.config
Quelle: config.py:357
Erstmals aufgetreten: 18:47:26 (2 Vorkommnisse)
Zuletzt protokolliert: 18:47:26

  • Invalid config for ‘template’ at configuration.yaml, line 41: ‘friendly_name’ is an invalid option for ‘template’, check: sensor->0->friendly_name
  • Invalid config for ‘template’ at configuration.yaml, line 79: ‘platform’ is an invalid option for ‘template’, check: sensor->0->platform Invalid config for ‘template’ at configuration.yaml, line 79: required key ‘state’ not provided Invalid config for ‘template’ at configuration.yaml, line 80: ‘sensors’ is an invalid option for ‘template’, check: sensor->0->sensors

also i tried to separate the sensors in different yaml files with the help of gemini and perplexity ai - but nothing works as expected. any help would be great.


# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
#   base_url: example.duckdns.org:8123

#logger:
#  logs:
#    homeassistant.components.modbus: debug
#    pymodbus.client: debug

powercalc:


group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

homeassistant:
    customize: !include customize.yaml


waste_collection_schedule:
  sources:
    - name: jumomind_de
      args:
        service_id: ben
        city: XXXXXXX
        street: XXXXXX-Straße, Ringe # Housenumber is part of street so it is not in house_number


#template: !include template.yaml
#platform: !include rest.yaml

template:
  - sensor:
    - name: "Temperatur Küche"
      friendly_name: "Temperatur Küche"
      state: "{{ state_attr('climate.wandthermostat_kuche_2', 'current_temperature') }}"
    - name: "Summe PV Leistung"
      friendly_name: "Summe PV Leistung"
      unit_of_measurement: "W"
      device_class: "power"
      state_class: "measurement"
      state: >
        {% set sensors = [
          'sensor.myenergi_harvi_1781962_power_ct_generation',
          'sensor.hoymiles_25kwp_power'
        ] %}
        {{ sensors | map('states') | map('float', 0) | sum }}
    - name: "IONiQ6 Gefahrene Kilometer"
      friendly_name: "IONiQ6 Gefahrene Kilometer"
      unit_of_measurement: "km"
      state: >-
        {% set start_km = states('input_number.ioniq6_km_stand_beim_kauf') | int %}
        {% set aktuell_km = states('sensor.ioniq6_odometer') | int %}
        {{ aktuell_km - start_km }}
    - name: "IONiQ6 Durchschnittsverbrauch"
      friendly_name: "IONiQ6 Durchschnittsverbrauch"
      unit_of_measurement: "kWh/100km"
      state: >-
        {% set gefahrene_km = states('sensor.ioniq6_gefahrene_km') | float %}
        {% set gesamt_verbrauch = states('sensor.ioniq6_total_energy_consumption') | float %}
        {% if gefahrene_km > 0 %}
          {{ '{:.2f}'.format((gesamt_verbrauch / gefahrene_km) * 100) }}
        {% else %}
          0.00
        {% endif %}
    - name: "aktueller IONiQ6 Durchschnittsverbrauch"
      friendly_name: "aktueller IONiQ6 Durchschnittsverbrauch"
      unit_of_measurement: "kWh/100km"
      state: >-
        {% set average_consumption = states('sensor.ioniq6_average_energy_consumption') | float %}
        {{ '{:.2f}'.format((average_consumption / 1000) * 100) }}
  - sensor:
    - platform: rest
      sensors:
        - name: Feinstaubsensor PM10
          unit_of_measurement: "µg/m³"
          value_template: '{{ value_json["sensordatavalues"][0]["value"] }}'
          resource: 'http://10.10.5.110/data.json'
        - name: Feinstaubsensor PM25
          unit_of_measurement: "µg/m³"
          value_template: '{{ value_json["sensordatavalues"][1]["value"] }}'
          resource: 'http://10.10.5.110/data.json'
        - name: Feinstaubsensor Humid
          unit_of_measurement: "%"
          value_template: '{{ value_json["sensordatavalues"][3]["value"] }}'
          resource: 'http://10.10.5.110/data.json'

For the first one, remove the “friendly name” line 41.

For the 2nd one, you can’t have a platform inside template sense - it sits in sensor.

When I get to my desktop, I’ll edit this reply with the code…


edit: OK, try this modification of your yaml:

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
#   base_url: example.duckdns.org:8123

#logger:
#  logs:
#    homeassistant.components.modbus: debug
#    pymodbus.client: debug

powercalc:


group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

homeassistant:
    customize: !include customize.yaml


waste_collection_schedule:
  sources:
    - name: jumomind_de
      args:
        service_id: ben
        city: XXXXXXX
        street: XXXXXX-Straße, Ringe # Housenumber is part of street so it is not in house_number


#template: !include template.yaml
#platform: !include rest.yaml

template:
  - sensor:
    - name: "Temperatur Küche"
      #friendly_name: "Temperatur Küche" <- this one was spotted by @123 - thanks!
      state: "{{ state_attr('climate.wandthermostat_kuche_2', 'current_temperature') }}"
    - name: "Summe PV Leistung"
      #friendly_name: "Summe PV Leistung"
      unit_of_measurement: "W"
      device_class: "power"
      state_class: "measurement"
      state: >
        {% set sensors = [
          'sensor.myenergi_harvi_1781962_power_ct_generation',
          'sensor.hoymiles_25kwp_power'
        ] %}
        {{ sensors | map('states') | map('float', 0) | sum }}
    - name: "IONiQ6 Gefahrene Kilometer"
      #friendly_name: "IONiQ6 Gefahrene Kilometer"
      unit_of_measurement: "km"
      state: >-
        {% set start_km = states('input_number.ioniq6_km_stand_beim_kauf') | int %}
        {% set aktuell_km = states('sensor.ioniq6_odometer') | int %}
        {{ aktuell_km - start_km }}
    - name: "IONiQ6 Durchschnittsverbrauch"
      #friendly_name: "IONiQ6 Durchschnittsverbrauch"
      unit_of_measurement: "kWh/100km"
      state: >-
        {% set gefahrene_km = states('sensor.ioniq6_gefahrene_km') | float %}
        {% set gesamt_verbrauch = states('sensor.ioniq6_total_energy_consumption') | float %}
        {% if gefahrene_km > 0 %}
          {{ '{:.2f}'.format((gesamt_verbrauch / gefahrene_km) * 100) }}
        {% else %}
          0.00
        {% endif %}
    - name: "aktueller IONiQ6 Durchschnittsverbrauch"
      #friendly_name: "aktueller IONiQ6 Durchschnittsverbrauch"
      unit_of_measurement: "kWh/100km"
      state: >-
        {% set average_consumption = states('sensor.ioniq6_average_energy_consumption') | float %}
        {{ '{:.2f}'.format((average_consumption / 1000) * 100) }}
 
# I have moved 'sensor' up the the same level as 'template'
sensor:
  - platform: rest
    sensors:
      - name: Feinstaubsensor PM10
        unit_of_measurement: "µg/m³"
        value_template: '{{ value_json["sensordatavalues"][0]["value"] }}'
        resource: 'http://10.10.5.110/data.json'
      - name: Feinstaubsensor PM25
        unit_of_measurement: "µg/m³"
        value_template: '{{ value_json["sensordatavalues"][1]["value"] }}'
        resource: 'http://10.10.5.110/data.json'
      - name: Feinstaubsensor Humid
        unit_of_measurement: "%"
        value_template: '{{ value_json["sensordatavalues"][3]["value"] }}'
        resource: 'http://10.10.5.110/data.json'

The docs for both are here and here.
Please note I haven’t checked your yaml beyond these 2 changes.


[edit: made the change spotted by @123]

1 Like

Oops! You forgot to remove it from the Template Sensor in your revised example.

template:
  - sensor:
      - name: "Temperatur Küche"
        friendly_name: "Temperatur Küche" #<--- delete this line
        state: "{{ state_attr('climate.wandthermostat_kuche_2', 'current_temperature') }}"

ugh, nice catch, thanks, @123.
I hashed them all out apart from that one :face_with_open_eyes_and_hand_over_mouth:

As you’ve replied first, I’ll edit my answer (and reference your reply).

1 Like

Thanks to all! Now, if a new error:

Logger: homeassistant.config
Quelle: config.py:942
Erstmals aufgetreten: 06:39:30 (1 Vorkommnisse)
Zuletzt protokolliert: 06:39:30

Invalid config for 'sensor' from integration 'rest' at configuration.yaml, line 75: 'sensors' is an invalid option for 'rest.sensor', check: sensors, please check the docs at https://www.home-assistant.io/integrations/rest

also i got another errot regarding template-sensors. i think this could be, that the hyundai hacs integration is not started early enough. so the calculation woun´t work.

Logger: homeassistant.helpers.event
Quelle: helpers/template.py:645
Erstmals aufgetreten: 06:49:19 (1 Vorkommnisse)
Zuletzt protokolliert: 06:49:19

Error while processing template: Template<template=({% set gefahrene_km = states('sensor.ioniq6_gefahrene_km') | float %} {% set gesamt_verbrauch = states('sensor.ioniq6_total_energy_consumption') | float %} {% if gefahrene_km > 0 %} {{ '{:.2f}'.format((gesamt_verbrauch / gefahrene_km) * 100) }} {% else %} 0.00 {% endif %}) renders=2>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2375, in forgiving_float_filter
    return float(value)
ValueError: could not convert string to float: 'unknown'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 643, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2756, in _render_with_context
    return template.render(**kwargs)
           ~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/usr/local/lib/python3.13/site-packages/jinja2/environment.py", line 1295, in render
    self.environment.handle_exception()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/local/lib/python3.13/site-packages/jinja2/environment.py", line 942, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2378, in forgiving_float_filter
    raise_no_default("float", value)
    ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1932, in raise_no_default
    raise ValueError(
    ...<2 lines>...
    )
ValueError: Template error: float got invalid input 'unknown' when rendering template '{% set gefahrene_km = states('sensor.ioniq6_gefahrene_km') | float %} {% set gesamt_verbrauch = states('sensor.ioniq6_total_energy_consumption') | float %} {% if gefahrene_km > 0 %}
  {{ '{:.2f}'.format((gesamt_verbrauch / gefahrene_km) * 100) }}
{% else %}
  0.00
{% endif %}' but no default was specified

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 760, in async_render_to_info
    render_info._result = self.async_render(  # noqa: SLF001
                          ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
        variables, strict=strict, log_fn=log_fn, **kwargs
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 645, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: float got invalid input 'unknown' when rendering template '{% set gefahrene_km = states('sensor.ioniq6_gefahrene_km') | float %} {% set gesamt_verbrauch = states('sensor.ioniq6_total_energy_consumption') | float %} {% if gefahrene_km > 0 %}
  {{ '{:.2f}'.format((gesamt_verbrauch / gefahrene_km) * 100) }}
{% else %}
  0.00
{% endif %}' but no default was specified

The rest part is wrong from the jchh post.

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

# Uncomment this if you are using SSL/TLS, running in Docker container, etc.
# http:
#   base_url: example.duckdns.org:8123

#logger:
#  logs:
#    homeassistant.components.modbus: debug
#    pymodbus.client: debug

powercalc:


group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

homeassistant:
    customize: !include customize.yaml


waste_collection_schedule:
  sources:
    - name: jumomind_de
      args:
        service_id: ben
        city: XXXXXXX
        street: XXXXXX-Straße, Ringe # Housenumber is part of street so it is not in house_number


#template: !include template.yaml
#platform: !include rest.yaml

template:
  - sensor:
    - name: "Temperatur Küche"
      #friendly_name: "Temperatur Küche" <- this one was spotted by @123 - thanks!
      state: "{{ state_attr('climate.wandthermostat_kuche_2', 'current_temperature') }}"
    - name: "Summe PV Leistung"
      #friendly_name: "Summe PV Leistung"
      unit_of_measurement: "W"
      device_class: "power"
      state_class: "measurement"
      state: >
        {% set sensors = [
          'sensor.myenergi_harvi_1781962_power_ct_generation',
          'sensor.hoymiles_25kwp_power'
        ] %}
        {{ sensors | map('states') | map('float', 0) | sum }}
    - name: "IONiQ6 Gefahrene Kilometer"
      #friendly_name: "IONiQ6 Gefahrene Kilometer"
      unit_of_measurement: "km"
      state: >-
        {% set start_km = states('input_number.ioniq6_km_stand_beim_kauf') | int %}
        {% set aktuell_km = states('sensor.ioniq6_odometer') | int %}
        {{ aktuell_km - start_km }}
    - name: "IONiQ6 Durchschnittsverbrauch"
      #friendly_name: "IONiQ6 Durchschnittsverbrauch"
      unit_of_measurement: "kWh/100km"
      state: >-
        {% set gefahrene_km = states('sensor.ioniq6_gefahrene_km') | float %}
        {% set gesamt_verbrauch = states('sensor.ioniq6_total_energy_consumption') | float %}
        {% if gefahrene_km > 0 %}
          {{ '{:.2f}'.format((gesamt_verbrauch / gefahrene_km) * 100) }}
        {% else %}
          0.00
        {% endif %}
    - name: "aktueller IONiQ6 Durchschnittsverbrauch"
      #friendly_name: "aktueller IONiQ6 Durchschnittsverbrauch"
      unit_of_measurement: "kWh/100km"
      state: >-
        {% set average_consumption = states('sensor.ioniq6_average_energy_consumption') | float %}
        {{ '{:.2f}'.format((average_consumption / 1000) * 100) }}
 
# I have moved 'sensor' up the the same level as 'template'
sensor:
  - platform: rest
    name: Feinstaubsensor PM10
    unit_of_measurement: "µg/m³"
    value_template: '{{ value_json["sensordatavalues"][0]["value"] }}'
    resource: 'http://10.10.5.110/data.json'

  - platform: rest
    name: Feinstaubsensor PM25
    unit_of_measurement: "µg/m³"
    value_template: '{{ value_json["sensordatavalues"][1]["value"] }}'
    resource: 'http://10.10.5.110/data.json'

  - platform: rest
    name: Feinstaubsensor Humid
    unit_of_measurement: "%"
    value_template: '{{ value_json["sensordatavalues"][3]["value"] }}'
    resource: 'http://10.10.5.110/data.json'
1 Like

Thank you all so much! It works now!

You should move the solution tag to jchh’s post.

1 Like

It’s all good; I should have know about the rest: being at the wrong level, but only realised when I came here to reply and saw you beat me to it.

The solution is just as much your’s (as it is @123’s).