Invalid config for [template]: expected dictionary for dictionary value @ data['sensors']

To me it seems, that this is correct YAML-code, but I cannot find the reason for the error “Invalid config for [template]: expected dictionary for dictionary value @ data[‘sensors’].” Can someone please help me ?!

template:
  - sensors:
    - name: "Varta Discharge"
      device_class: power
      unit_of_measurement: kW
      value_template: >-
         {% if states('sensor.esp_poessl_battery_power') | float < 0 %}
            {{ ((states('sensor.esp_poessl_battery_power') | float /1000) | round(3)) * -1 }}
         {% else %}
            {{ 0 }}
         {% endif %}

    - name: "Varta Charge"
      device_class: power
      unit_of_measurement: kW
      value_template: >-
         {% if states("sensor.esp_poessl_battery_power") | float > 0 %}
            {{ ((states("sensor.esp_poessl_battery_power") | float /1000) | round(3)) }}
         {% else %}
            {{ 0 }}
         {% endif %}
         

that needs to be state

Replace

value_template:

with

state:

Template Sensor in modern format uses state not value_template like a Template Sensor in legacy format.

In addition, replace sensors: with sensor:

Reference: Template Sensor

You also need to replace sensors: with sensor:

(Review my first post above)

change sensors to sensor

Thanks for the quick help :slight_smile:

I got the same error but I followed the new template format.
I’m a bit lost here … could someone please help me?

Here my code which is located directly in my configuration.yaml

# Tamplets
template:
  - sensor:
    - unique_id: TempDiff
      name: "temperature_difference_InOut"
      friendly_name: "Temperature Difference (In / Out)"
      unit_of_measurement: '°C'
      state: >
        {% set Out = states('sensor.openweathermap_temperature')|float %}
        {% set In =  states('sensor.ewelink_th01_temperature')|float %}
        {{Out - In}}

the template sensor should show and track the difference between the outside and the inside temperature

Remove the friendly_name option; it’s not supported.

Supply the float filter with a default value. For example, if the states() function doesn’t report a string value that can be converted to a number, float(0) will report 0. If you don’t specify a default value and float is unable to convert the supplied string value, it will produce an error.

Thanks, now the error doesn’t show up but I cannot find my new sensor.

Maybe it is a stupid question, I’m just started to work with template sensors …but were could I find the sensors?
It doesn’t show up in my Entities, Integrations or Devices

After you modify configuration.yaml, execute Developer Tools > YAML > Check Configuration. If it passes that test, execute Developer Tools > YAML > Reload Template Entities. Errors will be reported in the Log and sometimes as a Persistent Notification.

  • If there’s an error, you’ll need to correct it.
  • If there’s no error, look for the entity (sensor.temperature_difference_inout) in Developer Tools > States or in Settings > Devices & Services > Entities (if it exists it will be visible in both places).

That is what I did so far but the template sensor dont show up.

After the checking the configuration and also the YAML reload, no error pop up as notification nor show up in logs

Have I still error in my configuration.yaml?

# Tamplets
template:
  - sensor:
    - unique_id: TempDiffInOut
      name: "Temperature Difference (In / Out)"
      unit_of_measurement: '°C'
      state: >
        {% set Out = states('sensor.openweathermap_temperature')|float %}
        {% set In =  states('sensor.ewelink_th01_temperature')|float %}
        {{Out - In}}

I tried it also with float(0) without effect

Do you have more than one instance of template: in your configuration file?

It’s intended “effect” is to prevent the error I described in my previous post. Its presence or absence doesn’t determine if the sensor entity is created or not.

I only have this one sensor so far.

I tested the code under Developer Tools > Template and after it worked there I just copied the code to the last row in my configuration.yaml file.

I added the float(0) again but no impact. I just added the brackets after float


# Tamplets
template:
  - sensor:
    - unique_id: TempDiffInOut
      name: "Temperature Difference (In / Out)"
      unit_of_measurement: '°C'
      state: >
        {% set Out = states('sensor.openweathermap_temperature')|float(0) %}
        {% set In =  states('sensor.ewelink_th01_temperature')|float(0) %}
        {{Out - In}}

That doesn’t quite answer my question.

Does your configuration.yaml file contain more than one instance of the template: key word? Check the file from the beginning to the end (i.e. use a text editor to search the file for template). There should be just one instance of it.

1 Like

oh, I misunderstood your question. No, template: is only once mentioned

Here are my complete configuration.yaml


# Loads default set of integrations. Do not remove.
default_config:

# Load frontend themes from the themes folder
frontend:
  themes: !include_dir_merge_named themes

# Text to speech
tts:
  - platform: google_translate

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

http:
  cors_allowed_origins:
    - https://google.com
    - https://www.home-assistant.io
  ip_ban_enabled: true
  login_attempts_threshold: 5
  use_x_forwarded_for: true
  trusted_proxies:
  - XXX.XX.XX.0/24
    
# Tamplets
template:
  - sensor:
    - unique_id: TempDiffInOut
      name: "Temperature Difference (In / Out)"
      unit_of_measurement: '°C'
      state: >
        {% set Out = states('sensor.openweathermap_temperature')|float(0) %}
        {% set In =  states('sensor.ewelink_th01_temperature')|float(0) %}
        {{Out - In}}

# Converstion integration
conversation:

In the “YAML configuration reloading” section of Developer Tools > YAML, do you see “Template Entities” in the list of things that can be reloaded?

  • If you don’t, restart Home Assistant.

  • If you do, click “Template Entities”.

The restart was the problem, I just performed a quick restart before.

Thank you for the help, now its working fine :smiley:

Based on my experience, if you’re creating the very first Template entity, a restart is required. Using “Reload All YAML configuration” doesn’t seem to do the job in this particular case.

After the first Template entity is loaded, any additional Template entities you configure can be loaded using “Reload Template Entities” (or “Reload All YAML configuration”); a restart isn’t needed.

That’s how it works, adding a yaml integration for the first time requires a restart. From that point forward, you’ll be able to reload.

2 Likes

Kind of makes the expression “Reload All YAML Configuration” somewhat misleading.

I guess “Reload All YAML Configuration (except for newly added integrations)” was too verbose. :slightly_smiling_face:

Anyway, if I hadn’t experienced it myself, I probably would have not made sense of these clues: