Template sensor issues

Hi

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'

Can anyone help please?

solved it by removing the quotes i.e.

  - 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 %}
1 Like

You may wish to use variables in your templates to help make them more compact and legible.

For example:

  - platform: template
    sensors:
      shower:
        friendly_name: "Shower State"
        value_template: >-
           {% 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
           {% else %} Turn On Hot Water
           {% endif %}
2 Likes

@123
I see this is better thanks for that. I realize that i do not need the last “else” part i.e. {% else %} Turn On Hot Water.

How do I do that?

@123
If I remove {% else %} Turn On Hot Water it throws an error saying :

while scanning for the next token found character '%' that cannot start any token

Can you help please?

Thanks

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 %}

right I see, let me try that thanks for your help :slight_smile:

EDIT:

@123 did you mean if the temperature below falls bellow 35 it will report ‘No Shower’ ?

Is the last else statement looking at only the condition one above it ? i.e {% elif below >= 35 and above <= 40 %} Shower Quickly

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’.

@123 the first example i.e.

       {% 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.

Thanks for that I think I figured that out but there are no syntax errors there.

Oh there is, but you just don’t see it. You should post it here if you’re having trouble locating it.

I have posted it if you scroll up

which line is 566? If it’s the first line, also post line 565