Good evening,
I’m trying to set up a value template in my configuration.yaml file for 3 devices, powered by my Shelly 1 PMs.
The original code this is based off of works, and passes the configuration check, but throws errors in the File editor and Studio Code Server.
The code is from this blog post, with a corresponding YouTube video.
Original Code:
template:
- sensor:
- name: Dryer
state: >
{% if states("sensor.dryer_switch_0_power")|float == 0 %}
Off
{% elif states("sensor.dryer_switch_0_power")|float <= 2 %}
Standby
{% else %}
Drying
{% endif %}
icon: >
{% if states("sensor.dryer_switch_0_power") == "off" %}
mdi:tumble-dryer-alert
{% elif states("sensor.dryer_switch_0_power")|float <= 2 %}
mdi:tumble-dryer-off
{% else %}
mdi:tumble-dryer
{% endif %}
- sensor:
- name: Washing Machine
state: >
{% if states("sensor.washer_switch_0_power")|float == 0 %}
Off
{% elif states("sensor.washer_switch_0_power")|float <= 2 %}
Standby
{% else %}
Drying
{% endif %}
icon: >
{% if states("sensor.washer_switch_0_power") == "off" %}
mdi:tumble-dryer-alert
{% elif states("sensor.washer_switch_0_power")|float <= 2 %}
mdi:tumble-dryer-off
{% else %}
mdi:tumble-dryer
{% endif %}
- sensor:
- name: Dishwasher
state: >
{% if states("sensor.shelly_dishwasher_switch_0_power")|float == 0 %}
Off
{% elif states("sensor.shelly_dishwasher_switch_0_power")|float <= 2 %}
Standby
{% else %}
Washing
{% endif %}
icon: >
{% if states("sensor.shelly_dishwasher_switch_0_power") == "off" %}
mdi:dishwasher-alert
{% elif states("sensor.shelly_dishwasher_switch_0_power")|float <= 2 %}
mdi:dishwasher-off
{% else %}
mdi:dishwasher
{% endif %}
I have three of these entries for each sensor (Dishwasher, Washer, Dryer. The code is identical except for the sensor.xyz entryes in the state & icon conditions, and the mdi icon for each individual device.
The file saves, and passes the Dev Tools check, but in File editor, it throws an error with the following error:
duplicated mapping key (223:1)
220 | {{ as_timestamp(state ...
221 | icon_template: mdi:weat ...
222 |
223 | template:
-------^
224 | - sensor:
I’ve tried reformatting it using Studio Code Editor, and it lead me to this configuration for sensors:
Updated Code:
sensor:
- platform: tank_utility
email: !secret tamk_username
password: !secret tank_password
devices:
- "0022001b3638383015473830"
- platform: template
sensors:
nextsunrise:
friendly_name: "Next Sunrise"
value_template: >
{{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom(' %I:%M %p') | replace(" 0", "") }}
icon_template: mdi:weather-sunset-up
nextsunset:
friendly_name: "Next Sunset"
value_template: >
{{ as_timestamp(states.sun.sun.attributes.next_setting) | timestamp_custom(' %I:%M %p') | replace(" 0", "") }}
icon_template: mdi:weather-sunset-down
dryer:
friendly_name: "Dryer"
value_template: >
{{% if states("sensor.dryer_switch_0_power")|float == 0 %}
Off
{% elif states("sensor.dryer_switch_0_power")|float <= 2 %}
Standby
{% else %}
Drying
{% endif %}
}}
icon: >
{{% if states("sensor.dryer_switch_0_power") == "off" %}
mdi:tumble-dryer-alert
{% elif states("sensor.dryer_switch_0_power")|float <= 2 %}
mdi:tumble-dryer-off
{% else %}
mdi:tumble-dryer
{% endif %}
}}
washing_machine:
friendly_name: "Washing Machine"
value_template: >
{{% if states("sensor.washer_switch_0_power")|float == 0 %}
Off
{% elif states("sensor.washer_switch_0_power")|float <= 2 %}
Standby
{% else %}
Washing
{% endif %}
}}
icon: >
{{% if states("sensor.washer_switch_0_power") == "off" %}
mdi:washing-machine-alert
{% elif states("sensor.washer_switch_0_power")|float <= 2 %}
mdi:washing-machine-off
{% else %}
mdi:washing-machine
{% endif %}
}}
dishwasher:
friendly_name: "Dishwasher"
value_template: >
{{% if states("sensor.shelly_dishwasher_switch_0_power")|float == 0 %}
Off
{% elif states("sensor.shelly_dishwasher_switch_0_power")|float <= 2 %}
Standby
{% else %}
Washing
{% endif %}
}}
icon: >
{{% if states("sensor.shelly_dishwasher_switch_0_power") == "off" %}
mdi:dishwasher-alert-alert
{% elif states("sensor.shelly_dishwasher_switch_0_power")|float <= 2 %}
mdi:dishwasher-off
{% else %}
mdi:dishwasher
{% endif %}
}}
It passes the Studio Code Server tests, as well as gives me a green check on File Editor. When I go to Developer Tools, it gives me errors.
Configuration invalid!
Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected '%') for dictionary value @ data['sensors']['dishwasher']['value_template']. Got '{{% if states("sensor.shelly_dishwasher_switch_0_power")|float == 0 %}\n Off\n{% elif states("sensor.shelly_dishwasher_switch_0_power")|float <= 2 %}\n Standby\n{% else %}\n Washing\n{% endif %} }} icon: > {{% if states("sensor.shelly_dishwasher_switch_0_power") == "off" %} mdi:dishwasher-alert-alert {% elif states("sensor.shelly_dishwasher_switch_0_power")|float <= 2 %} mdi:dishwasher-off {% else %} mdi:dishwasher {% endif %} }}\n'
invalid template (TemplateSyntaxError: unexpected '%') for dictionary value @ data['sensors']['dryer']['value_template']. Got '{{% if states("sensor.dryer_switch_0_power")|float == 0 %}\n Off\n{% elif states("sensor.dryer_switch_0_power")|float <= 2 %}\n Standby\n{% else %}\n Drying\n{% endif %} }} icon: > {{% if states("sensor.dryer_switch_0_power") == "off" %} mdi:tumble-dryer-alert {% elif states("sensor.dryer_switch_0_power")|float <= 2 %} mdi:tumble-dryer-off {% else %} mdi:tumble-dryer {% endif %} }}\n'
invalid template (TemplateSyntaxError: unexpected '%') for dictionary value @ data['sensors']['washing_machine']['value_template']. Got '{{% if states("sensor.washer_switch_0_power")|float == 0 %}\n Off\n{% elif states("sensor.washer_switch_0_power")|float <= 2 %}\n Standby\n{% else %}\n Washing\n{% endif %} }} icon: > {{% if states("sensor.washer_switch_0_power") == "off" %} mdi:washing-machine-alert {% elif states("sensor.washer_switch_0_power")|float <= 2 %} mdi:washing-machine-off {% else %} mdi:washing-machine {% endif %} }}\n'. (See ?, line ?).
What can I do to fix this issue? Also, I tried putting the updated code in a sensors.yaml file and did a sensor: !include sensors.yaml, but HA didn’t like that either.
I’m still learning all this YAML coding, so I would really appreciate any help that anyone can provide.