Value unknown for sensor

i have some sensors and somtime the give value unknown

i want them to display as a graph bit this is messing the sensors up

example

nest_heat_temp:
entity_id: sensor.living_room_thermostat_hvac_state
value_template: >-
{% if is_state(‘sensor.living_room_thermostat_hvac_state’, ‘heating’) %}
{{ float(state_attr(‘climate.living_room’, ‘current_temperature’)) }}
{% else %}
0
{% endif %}

A few comments…

First, always properly format YAML code following instructions at the top of the page.

Next, for this template sensor you don’t need to use the entity_id config option. It can figure it out from the template. (In fact, since you did use it, but only specified one of the two entities used by the template sensor, it probably won’t update properly.)

Next, if you want the value to be graphed, you need to add the unit_of_measurement config option.

Lastly, it’s probably getting a value of unknown when climate.living_room isn’t ready yet (e.g., at startup.) I.e., the value returned by the state_attr function is probably ‘unknown’. Since you’re using the float function that will pass through the string when it can’t be converted, so ‘unknown’ is passing through. However, if you use the float filter, it will return 0.0 when the input cannot be converted to a float (such as when it is ‘unknown’.)

So, I’d suggest:

nest_heat_temp:
  value_template: >
    {% if is_state('sensor.living_room_thermostat_hvac_state', 'heating') %}
      {{ state_attr('climate.living_room', 'current_temperature')|float }}
    {% else %}
      0
    {% endif %}
  unit_of_measurement: °F

I did use the code button after selecting the code, it seems like the intents are messing things up, next time ill do this right.

Thanks for the quick reply, tomorrow ill test your ideas, its a confirmation for me that there is a lot to learn :wink:

No problem. For some reason some people have issues using that “preformatted text” button. Not sure why. As I was just telling someone else, the other way is to precede the code with a line that contains just three “back tick” characters, and add a line just like it after the code. So:

```
abc:
  xyz: 123
```

That should also properly format the code (instead of using the “preformatted text” button.)

@pnbruckner your advice is working flawless. i immediately changed the rest of my sensor that were bad designed :+1:

one note for some reason i need to define the entity_id’s of both sensors involved otherwise the sensor isnt updating, other sensors are working perfect without the entity_id’s as you suggested. but its working now, im a happy man.

Many thanks :smiley: