Why check attributes exist doesn't work for me in template template

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 :frowning:

Thanks in advance.

try this line instead:

{% if state_attr('sensor.sonoff_a48005c2b7','temperature') != none %}
1 Like

Thanks a lot @jchh for your reply.

I applied as you suggested and also I found now that I made a mistake because I wrote the “temperatura_camera_cust” without prefix “sensor.”.

Now I applied this conf:

- platform: template
  sensors:
    temperatura_camera_cust:
      device_class: temperature
      friendly_name: Temperatura della camera
      value_template: >
        {% if state_attr("sensor.sonoff_a48005c2b7", "temperature") != none %}
          {{ state_attr("sensor.sonoff_a48005c2b7", "temperature")|float /100 }}
        {% else %}
          {{ states( "sensor.temperatura_camera_cust" )|float  }}
        {% endif %}
      unit_of_measurement: °C
      icon_template: mdi:thermometer

And I am checking if it is working fine :slight_smile: but I am optimist.

1 Like