i have made a template sensor that determines the water temperature and outputs the result of shower. Here is my code:
- platform: template
sensors:
shower:
friendly_name: "Shower State"
value_template: >
{% if states.sensor.water_temp_below.state|float >= '40.0' and states.sensor.water_temp_above.state|float <= '45.0' %}
Two or more can Shower
{% elif states.sensor.water_temp_below.state|float >= '35.0' and states.sensor.water_temp_above.state|float <= '40.0' %}
Shower Quickly
{% elif states.sensor.water_temp_below.state|float >= '25.0' and states.sensor.water_temp_above.state|float <= '30.0' %}
No Shower
{% else %}
Turn On Hot Water
{% endif %}
I get the following errors:
Traceback (most recent call last):
File "/usr/src/app/homeassistant/helpers/entity.py", line 220, in async_update_ha_state
await self.async_device_update()
File "/usr/src/app/homeassistant/helpers/entity.py", line 375, in async_device_update
await self.async_update()
File "/usr/src/app/homeassistant/components/template/sensor.py", line 191, in async_update
self._state = self._template.async_render()
File "/usr/src/app/homeassistant/helpers/template.py", line 140, in async_render
return self._compiled.render(kwargs).strip()
File "/usr/local/lib/python3.7/site-packages/jinja2/asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.7/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
File "<template>", line 1, in top-level template code
TypeError: '>=' not supported between instances of 'float' and 'str'
- platform: template
sensors:
shower:
friendly_name: "Shower State"
value_template: >
{% if states.sensor.water_temp_below.state|float >= 40.0 and states.sensor.water_temp_above.state|float <= 45.0 %}
Two or more can Shower
{% elif states.sensor.water_temp_below.state|float >= 35.0 and states.sensor.water_temp_above.state|float <= 40.0 %}
Shower Quickly
{% elif states.sensor.water_temp_below.state|float >= 25.0 and states.sensor.water_temp_above.state|float <= 30.0 %}
No Shower
{% else %}
Turn On Hot Water
{% endif %}
If you do this it will be incorrect because if the temperature falls below 25 the template will report nothing.
{% set below = states('sensor.water_temp_below')|float %}
{% set above = states('sensor.water_temp_above')|float %}
{% if below >= 40 and above <= 45 %} Two or more can Shower
{% elif below >= 35 and above <= 40 %} Shower Quickly
{% elif below >= 25 and above <= 30 %} No Shower
{% endif %}
If you do this, the template will report âNo Showerâ if the temperature falls below 35.
{% set below = states('sensor.water_temp_below')|float %}
{% set above = states('sensor.water_temp_above')|float %}
{% if below >= 40 and above <= 45 %} Two or more can Shower
{% elif below >= 35 and above <= 40 %} Shower Quickly
{% else %} No Shower
{% endif %}
In the first example, if the temperature falls below 25, the template reports nothing.
In the second example, if the temperature falls below 35, the template reports âNo Showerâ.
{% set below = states('sensor.water_temp_below')|float %}
{% set above = states('sensor.water_temp_above')|float %}
{% if below >= 40 and above <= 45 %} Two or more can Shower
{% elif below >= 35 and above <= 40 %} Shower Quickly
{% elif below >= 25 and above <= 30 %} No Shower
{% endif %}
throws an error saying:
019-05-13 13:46:44 ERROR (SyncWorker_1) [homeassistant.util.yaml] while scanning for the next token
found character '%' that cannot start any token
in "/config/configuration.yaml", line 566, column 13
2019-05-13 13:46:44 ERROR (MainThread) [homeassistant.bootstrap] Error loading /config/configuration.yaml: while scanning for the next token
found character '%' that cannot start any token
in "/config/configuration.yaml", line 566, column 13
Like I said, the first example is incorrect because it will report nothing when the temperature falls below 25. In other words, if below = 22, it wonât report âNo Showerâ, it will report nothing.
As for error message you are receiving, I canât comment on it because I am testing the template in the Template Editor and I am not getting that error.
Itâs telling you the line number in the file and the character in the file. One of the easiest errors to debug. Go to that line and character and see what syntax errors are there.