Hi, unfortunately I am not able to fix my template sensor (noob here!). When I reboot HA, the sensor is not ready (the mqtt value is not pubblished yet), so I get a render error.
I read to use state_attr so it will render to None if no attributes → error
I tried to use the float(default) → error
I tried to give a value float(0) → error
I tried other things with the same result, so… HELP!
I have a json that contains:
{"info": {"timestamp": "2022-02-15T13:41:56+01:00", "rpi_model": "RPi Zero W r1.1", "ifaces": "e,w,b", "host_name": "pizero1", "fqdn": "pizero1", "ux_release": "bullseye", "ux_version": "5.10.92+", "up_time": "4 days, 17:38", "last_update": "2022-02-15T05:11:52+01:00", "fs_total_gb": 32, "fs_free_prcnt": 8, "networking": {"wlan0": {"IP": "192.168.xxxxx", "mac": "xxxxxxxxxx"}}, "drives": {"root": {"size_gb": 32, "used_prcnt": 8, "device": "/dev/root", "mount_pt": "/"}}, "memory": {"size_mb": "429.633", "free_mb": "288.867"}, "cpu": {"hardware": "BCM2835", "model": "ARMv6-compatible processor rev 7 (v6l)", "number_cores": 2, "bogo_mips": "797.66", "serial": "00000000d101c7f6", "load_1min_prcnt": 10.5, "load_5min_prcnt": 6.0, "load_15min_prcnt": 4.0}, "throttle": ["throttled = 0x0", "Not throttled"], "temperature_c": 30.9, "temp_gpu_c": 30.9, "temp_cpu_c": 29.9, "reporter": "ISP-RPi-mqtt-daemon v1.6.0", "report_interval": 5}}
the sensor I made:
- sensor:
- name: "Memoria libera PiZero1"
unique_id: "sensor.rpi_monitor_pizero1.free_mem"
icon: mdi:memory
unit_of_measurement: "MB"
state: >
{{ state_attr('sensor.rpi_monitor_pizero1', 'memory').free_mb | float(default) }}
and the error on HA start:
2022-02-14 23:46:04 ERROR (MainThread) [homeassistant.helpers.event] Error while processing template: Template("{{ state_attr('sensor.rpi_monitor_pizero1', 'memory').free_mb | float(default) }}")
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 407, in async_render
render_result = _render_with_context(self.template, compiled, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1814, in _render_with_context
return template.render(**kwargs)
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1291, in render
self.environment.handle_exception()
File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 925, 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 1584, in forgiving_float_filter
return float(value)
jinja2.exceptions.UndefinedError: 'None' has no attribute 'free_mb'
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 523, in async_render_to_info
render_info._result = self.async_render(variables, strict=strict, **kwargs)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 409, in async_render
raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'None' has no attribute 'free_mb'
2022-02-14 23:46:04 ERROR (MainThread) [homeassistant.components.template.template_entity] TemplateError('UndefinedError: 'None' has no attribute 'free_mb'') while processing template 'Template("{{ state_attr('sensor.rpi_monitor_pizero1', 'memory').free_mb | float(default) }}")' for attribute '_attr_native_value' in entity 'sensor.memoria_libera_pizero1'
how to change the state not to have an error on HA start?


could you please share some info how to use it? Thanks