Template sensor in configuration.yaml

This is part of the climate.thermostat.yaml form my thermostat, copied from dev.tools/states. I want to create a sensor to calculate difference between “temperature” and “current_temperature”
The template below. But it show “unavailable”?

Thermostat`

 hvac_modes:
  - "off"
  - cool
  - heat
min_temp: 10
max_temp: 37.2
fan_modes:
  - "on"
  - auto
  - diffuse
preset_modes:
  - none
  - away
  - hold
current_temperature: 23.9
temperature: 25
target_temp_high: null
target_temp_low: null
current_humidity: 47
fan_mode: auto
hvac_action: idle

Template


name: "HVAC Set temperature and ambient temperature diff"
        unique_id: '55xxxxxxxxxxxxxxf'
        unit_of_measurement: "°C"
        state: >
          {% set set_temp = states('climate.thermostat','temperature') | float %}
          {% set ambient_temp = states('climate.thermostat','current_temperature') | float %}
          {{ ((set_temp - ambient_temp)) | round(1, default=0) }}

You need to use state_attr() to get attributes, not states().

Also, you should have defaults on the float filters and/or add an availability. Since those attributes are normally already floats, you can probably just leave the filters off if you add an availability:

name: "HVAC Set temperature and ambient temperature diff"
unique_id: '55xxxxxxxxxxxxxxf'
unit_of_measurement: "°C"
state: >
  {% set set_temp = state_attr('climate.thermostat','temperature') %}
  {% set ambient_temp = state_attr('climate.thermostat','current_temperature') %}
  {{ ((set_temp - ambient_temp)) | round(1, default=0) }}
availability: |
  {{ state_attr('climate.thermostat','current_temperature') | is_number and 
  state_attr('climate.thermostat','temperature') | is_number }}

by the way it I input sensor.thermostat_temperature in to dev_tools/states it returns current temperature.
What would be syntax to extract value for the ‘temperature’ from

climate.thermostat

?

ok. Let me try

deleted posted by mistake

This appease to be working. Thank you.

state: >
  {% set set_temp = state_attr('climate.thermostat','temperature') | float(0) %}
  {% set ambient_temp = state_attr('climate.thermostat','current_temperature') | float(0) %}
  {{ (set_temp - ambient_temp) | round(1, default=0) }}
availability: >
  {{ state_attr('climate.thermostat','current_temperature') is number and 
     state_attr('climate.thermostat','temperature') is number }}