Help accessing sensor attribute value in climate command template

I have this sensor:

    - name: Thermostat Under Temperature
      unique_id: thermostat_under_temperature
      state_topic: "HVAC/JSONout"
      value_template: >
        {% if "ThermostatUndertemp" in value_json %}
        {% endif %}
      state_class: measurement 

I need to access it to compute my command template but I can’t make it work; I always have an error message saying that state_attr('thermostat_under_temperature','last_updated') is NoneType

  -name: hvac
      temperature_low_command_topic: "HVAC/JSONin/ThermostatUndertemp"
      temperature_low_command_template: >
        {{ value|float - state_attr('thermostat_under_temperature','last_updated') }}

Besides the fact that you cannot get “last_updated” with state_attr(), why do you want to subtract a date to get a temperature?

There’s more than one problem.

1. The sensor’s template can fail to report a value.

For the following template, if ThermostatUndertemp is not in value_json it reports nothing. That means the sensor’s value will be reported as unknown. You need to fix that. I suggest that if ThermostatUndertemp is not in value_json it reports the sensor’s existing value using this.state.

        {% if "ThermostatUndertemp" in value_json %}
        {% endif %}

2. Invalid use of the state_attr() function.

state_attr requires two parameters: an entity_id and the name of an attribute. You supplied it with 'thermostat_under_temperature' which is not an entity_id. In addition, last_updated is not an attribute. It’s a property of an entity’s State Object and can’t be referenced like an attribute.

The correct way to reference the value of the entity’s last_updated property is like this:


However, it will still fail because of the next problem.

3. Invalid subtraction

The following template is attempting to subtract the value of last_updated which is a datetime, from value|float which is a floating point number. That’s simply not possible.

{{ value|float - state_attr('thermostat_under_temperature','last_updated') }}

In order to help you, please explain what is your goal.

Sorry but I am not substracting a date; where do you see that ? how do I get the value of my sensor please ?

“last_updated” is the date and time the sensor was last updated.

thank you, I am trying to access the value of thermostat_under_temperature to sustract it. It is a float number

I updated my sensor like this:

      value_template: >
        {% if "ThermostatUndertemp" in value_json %}
        {% else %}
          this state
        {% endif %}

and my template like this:

{{ value|float - states.sensor.thermostat_over_temperature|float  }}

I also tried

{{ value|float -  states.sensor.thermostat_under_temperature.last_updated|float  }}

but still getting error invalid input None

ok; sorry i did not realize; so how do I get the value ?

It’s this.state not this state

Neither of the two examples you posted are valid templates.

You are still trying to subtract a datetime from a number. I explained in #3 that it’s an invalid subtraction.

This is what your template is trying to do and it makes no mathematical sense:

Number minus Datetime

100 - 2024-01-20T10:07:00-05:00

That’s why I asked what’s your goal because I can’t guess what it is based on that invalid subtraction.

I said in my last reply “I am trying to access the value of thermostat_under_temperature to sustract it. It is a float number”

I do not want the date; How do i access the value please ? the value is a number

How do I get the value please ? the value is a number. I don’t want the date

Getting its value is easy but you want to subtract it from what?

This gets the value of sensor.thermostat_under_temperature.

{{ states('sensor.thermostat_under_temperature') | float(0) }}

Yet that’s exactly what you put in your template in every example you posted. The value of last_updated is a date and time.

You want to perform a subtraction using the value of sensor.thermostat_under_temperature and what else?

Sorry to bother again but your solution does not work either:

{{ states('sensor.thermostat_under_temperature') | float(0) }}

float(0) return 0; float(1) returns 1 so I believe this is the fallback default

If I put just float I get error float got invalid input 'unknown' when rendering template

So I believe I am still not accessing the value

I know 100% I have a value because the value is displayed elsewhere

Copy-paste the following template into the Template Editor and let me know what it reports.

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

If it reports unknown that means one of the following possibilities:

  • sensor.thermostat_under_temperature doesn’t exist
  • It’s misspelled

If it reports some other value that’s not a number then float(0) cannot convert it and will report the default value 0.

I honestly cannot figure; I get unknown but the entity exists and it is the correct name and it is not misspelled and I have the value I need displayed elsewhere so the entity Im trying to fetch is working correctly.

I have double; triple; quadruple checked… loosing my sanity here

Where are you looking?

Go to Developer Tools > States and find sensor.thermostat_under_temperature in the list of entities. What is the sensor’s value shown in the State column?

If it says unknown there then that’s the sensor’s actual value.

OMG that was it ! Thank you !
I don’t understand why but the entity ID was different than the unique_id !!
I assumed they would be the same since the beginning… I will accept your previous answer as solution.

I know this is outside the scope of my question but why is that please; do you know ?
Other entities have same entity_id and unique_id… I don’t understand why is this possible; Can I change them back ?

1 Like

What is the sensor’s actual entity_id?


I renamed it at some point that must be that but it did not update I guess