Hi all.
I am news in this world and I am trying to do the best reading and reading posts and documentations.
I am writing because I really don’t understandr why my template sensor configuration is not working as expected and I would like your help.
I have one sensor device that send some attributes every X seconds and in that attributes there is one that I am trying to use in my additional sensor template to have the temperature.
So I just simple created a sensor with device_class: temperature and in value_template I put the device conf to have a value from device:
{{ state_attr( "sensor.sonoff_a48005c2b7","temperature")|float/100 }}
After that I discovered that sometime the device is not sending in the attributes list the “temperature” attribute. It is not set to none but is not present in the list.
So, I updated the sensor template (after a lot of tentative, read some blogs here) to test the presence of the attribute like this:
- platform: template
sensors:
temperatura_camera_cust:
device_class: temperature
friendly_name: Temperatura della camera
value_template: >
{% if 'temperature' not in states.sensor.sonoff_a48005c2b7.attributes %}
{{ states( "temperatura_camera_cust" )|float }}
{% elif state_attr("sensor.sonoff_a48005c2b7", "temperature") == none %}
{{ states( "temperatura_camera_cust" )|float }}
{% elif state_attr("sensor.sonoff_a48005c2b7", "temperature") == "unknown" %}
{{ states( "temperatura_camera_cust" )|float }}
{% elif state_attr( "sensor.sonoff_a48005c2b7","temperature")|float(default=0) == 0 %}
{{ states( "temperatura_camera_cust" )|float }}
{% else %}
{{ state_attr( "sensor.sonoff_a48005c2b7","temperature")|float/100 }}
{% endif %}
unit_of_measurement: °C
icon_template: mdi:thermometer
But seems that the first “if” condition is not working properly because I continue to have the state of the custom sensor as “unknow” and the error on the log:
2023-07-03 10:06:57.549 ERROR (MainThread) [homeassistant.helpers.template_entity] TemplateError('ValueError: Template error: float got invalid input 'unknown' when rendering template '{% if 'temperature' not in states.sensor.sonoff_a48005c2b7.attributes %}
{{ states( "temperatura_camera_cust" )|float }}
{% elif state_attr("sensor.sonoff_a48005c2b7", "temperature") == none %}
{{ states( "temperatura_camera_cust" )|float }}
{% elif state_attr("sensor.sonoff_a48005c2b7", "temperature") == "unknown" %}
{{ states( "temperatura_camera_cust" )|float }}
{% elif state_attr( "sensor.sonoff_a48005c2b7","temperature")|float(default=0) == 0 %}
{{ states( "temperatura_camera_cust" )|float }}
{% else %}
{{ state_attr( "sensor.sonoff_a48005c2b7","temperature")|float/100 }}
{% endif %}' but no default was specified') while processing template 'Template<template=({% if 'temperature' not in states.sensor.sonoff_a48005c2b7.attributes %}
{{ states( "temperatura_camera_cust" )|float }}
{% elif state_attr("sensor.sonoff_a48005c2b7", "temperature") == none %}
{{ states( "temperatura_camera_cust" )|float }}
{% elif state_attr("sensor.sonoff_a48005c2b7", "temperature") == "unknown" %}
{{ states( "temperatura_camera_cust" )|float }}
{% elif state_attr( "sensor.sonoff_a48005c2b7","temperature")|float(default=0) == 0 %}
{{ states( "temperatura_camera_cust" )|float }}
{% else %}
{{ state_attr( "sensor.sonoff_a48005c2b7","temperature")|float/100 }}
{% endif %}) renders=32>' for attribute '_attr_native_value' in entity 'sensor.temperatura_camera_cust'
In the states page, as below, sometime the attribute there is, sometime no.
So, please can someone help me to understand where is my error … I’ve been trying for several days without success
Thanks in advance.