This doesn’t work anymore for me.
My code (in template.yaml) is:
- name: "tmpl_weather_wind_dir"
unique_id: "tmpl_weather_wind_dir"
state_class: measurement
icon: mdi:sign-direction
state: >
{% set direction = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] %}
{% set degree = states('sensor.weather_wind_bearing')|float %}
{{ degree }}
{{ direction[((degree+11.25)/22.5)|int] }}
availability: "{{ states('weather.home') not in [ 'unknown', 'unavailable'] }}"
Using value_template instead of state gave me an error, preventing restart.
With this code the system restarts, but the value of the sensor remains ‘unknown’.
And the logs show these errors:
Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:173
Integration: Template (documentation, issues)
First occurred: 13:11:40 (1 occurrences)
Last logged: 13:11:40
TemplateError('ValueError: Template error: float got invalid input 'unknown' when rendering template '{% set direction = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] %} {% set degree = states('sensor.weather_wind_bearing')|float %} {{ degree }} {{ direction[((degree+11.25)/22.5)|int] }}' but no default was specified') while processing template 'Template("{% set direction = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] %} {% set degree = states('sensor.weather_wind_bearing')|float %} {{ degree }} {{ direction[((degree+11.25)/22.5)|int] }}")' for attribute '_attr_native_value' in entity 'sensor.tmpl_weather_wind_dir'
Logger: homeassistant.helpers.event
Source: helpers/template.py:411
First occurred: 13:11:40 (1 occurrences)
Last logged: 13:11:40
Error while processing template: Template("{% set direction = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] %} {% set degree = states('sensor.weather_wind_bearing')|float %} {{ degree }} {{ direction[((degree+11.25)/22.5)|int] }}")
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1615, in forgiving_float_filter
return float(value)
ValueError: could not convert string to float: 'unknown'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 409, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1842, in _render_with_context
return template.render(**kwargs)
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1301, in render
self.environment.handle_exception()
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 936, in handle_exception
raise rewrite_traceback_stack(source=source)
File "<template>", line 1, in top-level template code
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1618, in forgiving_float_filter
raise_no_default("float", value)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1331, in raise_no_default
raise ValueError(
ValueError: Template error: float got invalid input 'unknown' when rendering template '{% set direction = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] %} {% set degree = states('sensor.weather_wind_bearing')|float %} {{ degree }} {{ direction[((degree+11.25)/22.5)|int] }}' but no default was specified
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 525, in async_render_to_info
render_info._result = self.async_render(variables, strict=strict, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 411, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: float got invalid input 'unknown' when rendering template '{% set direction = ['N','NNE','NE','ENE','E','ESE','SE','SSE','S','SSW','SW','WSW','W','WNW','NW','NNW','N'] %} {% set degree = states('sensor.weather_wind_bearing')|float %} {{ degree }} {{ direction[((degree+11.25)/22.5)|int] }}' but no default was specified