Temperature sensor template log TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'

Hello, i have some errors in logs caused by the temperature sensor template.

Template looks like this

  • platform: template
    unit_of_measurement: “°C”
    value_template: >-
    {% if (states.sensor.czujka2_temperature.state|float) != 0.0 and (states.sensor.czujka2_temperature.state|float) != 85 %}
    {{ (states.sensor.czujka2_temperature.state|float) }}
    {% else %}
    {{ as_timestamp(now()) - as_timestamp(states.czujka2_filtrowana.last_changed|float) }}
    {% endif %}
    scan_interval: 30

The reason i’ve made the template for the sensors was to eliminate reads 85c and 0c while voltage pikes.

Template is reading value from ds18b20 sensor on 1 wire and if the value is not 85c or 0c show real value is shown and if it’s wrong its presenting the last state from recorder.

The solution works but its annoying to everyday observe the same errors in logs.

Tried may changes but still errors in logs.

2019-10-20 11:47:35 ERROR (MainThread) [homeassistant.helpers.entity] Update for sensor.czujka2_filtrowana fails
Traceback (most recent call last):
File “/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity.py”, line 268, in async_update_ha_state
await self.async_device_update()
File “/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity.py”, line 446, in async_device_update
await self.async_update()
File “/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/template/sensor.py”, line 248, in async_update
self._state = self._template.async_render()
File “/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/template.py”, line 215, in async_render
return compiled.render(kwargs).strip()
File “/srv/homeassistant/lib/python3.7/site-packages/jinja2/asyncsupport.py”, line 76, in render
return original_render(self, *args, **kwargs)
File “/srv/homeassistant/lib/python3.7/site-packages/jinja2/environment.py”, line 1008, in render
return self.environment.handle_exception(exc_info, True)
File “/srv/homeassistant/lib/python3.7/site-packages/jinja2/environment.py”, line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File “/srv/homeassistant/lib/python3.7/site-packages/jinja2/_compat.py”, line 37, in reraise
raise value.with_traceback(tb)
File “”, line 1, in top-level template code
TypeError: unsupported operand type(s) for -: ‘float’ and ‘NoneType’

Looks like you need to remove the |float in your else statement. Also a good idea to test any templates using the Templates dev tool before actually adding them to a sensor.

A little explanation on what @Tediore said. last_changed will either be None or a datetime object. Neither of those objects can be converted to a float.

The method as_timestamp() handles both of those objects. So just remove the |float after the fact.

Also, please take the time to learn how to properly format your posts. Follow step 11.

1 Like

Thanks petro. I forgot to include this in my original reply too: State object documentation

1 Like