Read sensor state

I defined a sensor and I’m simply trying to work with the sensor value. But whenever I try to read it it’s unknown or not existing.

sensor:
  - platform: template
    sensors:
      woonkamer_target_temp_diff:
        value_template: >-
          {% set current_temp = states.climate.woonkamer.attributes.current_temperature|int %}
          {% set target_temp  = states.climate.woonkamer.attributes.temperature|int %}
          {{ (target_temp - current_temp)|int }}

sBKoaLb0DC

The state is -2.

Now how do I display or use this in a template?

{{ states.sensor.sensor.woonkamer_target_temp.state }}

UndefinedError: ‘None’ has no attribute ‘woonkamer_target_temp’

{{states('sensor.woonkamer_target_temp')}}

unknown

Trying all variants Google will come up with still give invalid results.

Have you checked the entity_id in the Devtools?

I would asume the sensor is called “sensor.woonkamer_target_temp_diff”.

{{states('sensor.woonkamer_target_temp_diff')}}
1 Like

I’m an idiot, not sure how I missed this.

There is a typo in
{{ states.sensor.sensor.woonkamer_target_temp.state }}

sensor is double, should be {{ states.sensor.woonkamer_target_temp.state }}

and @dennis84de has a point that you might be looking for sensor.woonkamer_target_temp_diff

You can try them out in Templates of Developer tools.

And you sure you want to use |int and not |float ?

1 Like