mqtt:
sensor:
- name: Thermostat Under Temperature
unique_id: thermostat_under_temperature
state_topic: "HVAC/JSONout"
value_template: >
{% if "ThermostatUndertemp" in value_json %}
{{value_json.ThermostatUndertemp|float}}
{% 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
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 %}
{{value_json.ThermostatUndertemp|float}}
{% 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.
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
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 ?