Template sensor stopped working after upgrade to 0.74.1

Hi,

this is my template which works befor 0.74.1:

- platform: template
sensors:
low_batteries:
  friendly_name: Low batteries
  value_template: >-
    {%- for state in states if state.attributes.battery_level or state.attributes.battery -%}
      {%- set battery = state.attributes.battery_level or state.attributes.battery -%}
      {%- if battery == 'High' -%}
        {%- set battery = 100 -%}
      {%- endif -%}
      {%- if battery > 2.2 -%}
        {%- set battery = 100 -%}
      {%- endif -%}
      {%- if battery | int < 30 -%}
        {{state.attributes.friendly_name}}
      {%- endif -%}
    {%- endfor -%}

Now I get this error:

2018-08-01 09:59:03 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.low_batteries fails
Traceback (most recent call last):
  File "/usr/src/app/homeassistant/helpers/entity.py", line 196, in async_update_ha_state
yield from self.async_device_update()
  File "/usr/src/app/homeassistant/helpers/entity.py", line 317, in async_device_update
yield from self.async_update()
  File "/usr/local/lib/python3.6/asyncio/coroutines.py", line 212, in coro
res = func(*args, **kw)
  File "/usr/src/app/homeassistant/components/sensor/template.py", line 183, in async_update
self._state = self._template.async_render()
  File "/usr/src/app/homeassistant/helpers/template.py", line 132, in async_render
return self._compiled.render(kwargs).strip()
  File "/usr/local/lib/python3.6/site-packages/jinja2/asyncsupport.py", line 76, in render
return original_render(self, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 1008, in render
return self.environment.handle_exception(exc_info, True)
  File "/usr/local/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
raise value.with_traceback(tb)
  File "<template>", line 6, in top-level template code
TypeError: '>' not supported between instances of 'str' and 'float'

What could I do? :slight_smile:

Also you appear to perform a few checks against battery, it seems sometime it’s a string, sometime it’s a number. You probably need to convert the format for each of your checks when you compare it to a number, this one is missing:
{%- if battery > 2.2 -%} should become {%- if battery | int > 2.2 -%}

Let me know how it goes

sorry I forgot to mention that I already tried that:

- platform: template
sensors:
low_batteries:
  friendly_name: Low batteries
  value_template: >-
    {%- for state in states if state.attributes.battery_level or state.attributes.battery -%}
      {%- set battery = state.attributes.battery_level or state.attributes.battery -%}
      {%- if battery == 'High' -%}
        {%- set battery = 100 -%}
      {%- endif -%}
      {%- if battery | int > 2.2 -%}
        {%- set battery = 100 -%}
      {%- endif -%}
      {%- if battery | int < 30 -%}
        {{state.attributes.friendly_name}}
      {%- endif -%}
    {%- endfor -%}

the error is gone, but the values >2.2 are still not set to 100. Gor the value “High” it is set to 100.

What could I else do?

mmm. my bad, can you try this:

 {%- if battery | float > 2.2 -%}

2.2 is obviously a float, not an int

great, with “float” it is working. thanks a lot!

but I recognized another problem: All sensor with state “Low” are not set to an integer and they are missing in the output.

  {%- if battery | int < 30 -%}
    {{state.attributes.friendly_name}}
  {%- endif -%}

how can I include non-integer states in this code part, too? or should I just set them to an int < 100 for example:

  {%- if battery != 'High' -%}
    {%- set battery = 1 -%}
  {%- endif -%}

How about this?

  {%- if battery != 'Low' -%}
    {%- set battery = 29 -%}
  {%- endif -%}

does not work, i think its because the value “High” or “Low” ist not an int which is interpreted by this output:

{%- if battery | int < 30 -%}
    {{state.attributes.friendly_name}}
{%- endif -%}

is it possible to set all “non-int-values” to a defined int value? for example

{%- if battery = "not a int value" -%}
    {%- set battery = 100 -%}
{%- endif -%}

in that case use if and elif else:

    {%- for state in states if state.attributes.battery_level or state.attributes.battery -%}
      {%- set battery = state.attributes.battery_level or state.attributes.battery -%}
      {%- if battery | float > 2.2 -%}
        {%- set battery = 100 -%}
      {%- elif battery | float < 30 -%}
        {{state.attributes.friendly_name}}
      {%- else -%}
        {%- set battery = 100 -%}
      {%- endif -%}
    {%- endfor -%}
1 Like

thanks for the hint, but this gives not the correct output, too. but with your help I got it working with this:

 {%- for state in states if state.attributes.battery_level or state.attributes.battery -%}
      {%- set battery = state.attributes.battery_level or state.attributes.battery -%}
      {%- if battery | float > 2.2 -%}
        {%- set battery = 100 -%}
      {%- elif battery == 'High' -%}
        {%- set battery = 100 -%}
      {%- else -%}
        {{state.attributes.friendly_name}}
      {%- endif -%}
    {%- endfor -%}

Thanks a lot @lolouk44 :slight_smile:

Anyway I think this could be reached easier because I dont need to set the battery variable to 100, but nevermind, its working :slight_smile:

1 Like