in esphome i can do it by publish_state(NaN) and sets the sensor to unavaliable and thats for xiamoi devices talking to a esp32 as a bluetooth server
but in home assistant i wanna make i guess template sensor it poles ever 1 min to see an esp32 thermometer i made with a bme280 that its connected or if the value is still valid… so if the bme280 breaks as i have had where it doesnt communicate anymore or if the esp32 say gets unplugged i like the template sensor to report unavaliable… and not report constant 15.1c as an example… is there a way to do that with the avaliablity: attribute you add in some sensors
right now only way it displays unavaliable is if i have that sensor unplugged or removed from devices and reboot home assistant to get it to go unavaliable but i want it to do it live
here is what i have for hallway sensor that reads an esp32 device with the bme280 now if that sensor goes offline or if the bme breaks in esp i want it to report unavliable… above them remmed out marks… the bluetooth server will set it to NAN if xiaomi temperature breaks or has dead battery… but i wanna now do this in HA with esp32 device itself
im doing this for the Climate. sensor for furnace i found that the actual temp i guess goes unavliable… but the climate.current_temperature doesnt change to n/a right away unless u reboot… and even when i make an automation for the esp32 if its unavaliable and set temperature to 0 -100 or force it with unavaliable it doesnt actually set the current temp to n/a
i tried to use the Python Set State script but i cant seem to alter the state
it doesnt like my entity sensor
You can compare the value from the last_reported property to a time offset from now(). The following will become unavailable if the hallway temperature sensor returns a non-numeric state or if it’s last report from the sensor was more than 10 minutes ago.
- name: "Hallway Temperature Display"
state: "{{ states('sensor.hallway_temp_sensor_temperature') }}"
availability: |
{% set ent = 'sensor.hallway_temp_sensor_temperature' %}
{{ is_number(states(ent)) and ( now() - timedelta(minutes=10) < states[ent].last_reported) }}
icon: "mdi:thermometer"
unit_of_measurement: "°C"
device_class: temperature
While I do not recommend using that script… the errors that stand out are as follows:
The template is not surrounded in quotes.
The template is placed to provide the value for an entity_id key, but will not return an entity ID.
-100 is not a valid state for a climate entity. If the goal is to replace an attribute’s value, you need to use the attribute ID as the key, not state.
@Didgeridrew didnt help so i set it to 1 min
i rebooted HA and the temp was like 10.9 i removed the esp32 hallway_temp_sensor_temperature sensor and it instantlly goes unavaliabled the hallway_temp_sensor_temperature which it was doing before
but it wont force
climate.moms_furnace current_temperature to unavaliable
as it wont auto update… and when i guess Display temperature it wont tell climate.moms_furnace.current_temperature to set to unavaliable
it still states 10.9 even after removing the esp32
so thats why i was trying to use the python set_state script to
set
climate.moms_furnace.current_temperature to unavaliable
or
this one i figured from that one link
{%- set result = state_attr('climate.moms_furnace', 'current_temperature') %}
{%- set result = 'unavaliable' %}
{{ result }}
but dont know how to set that unless you do it like this?
- name: "Hallway Temperature Display"
state: "{{ states('sensor.hallway_temp_sensor_temperature') }}"
availability: |
{% set ent = 'sensor.hallway_temp_sensor_temperature' %}
{{ is_number(states(ent)) and ( now() - timedelta(minutes=10) < states[ent].last_reported) }}
{%- set result = state_attr('climate.moms_furnace', 'current_temperature') %}
{%- set result = 'null' %}
{{ result }}
icon: "mdi:thermometer"
unit_of_measurement: "°C"
device_class: temperature
so if i run
{% set ent = 'sensor.hallway_temp_sensor_temperature' %}
{{ is_number(states(ent)) and ( now() - timedelta(minutes=10) < states[ent].last_reported) }}
under the template page under developer… it returns a True or False it doesnt return a number or a null as current_temperature for climate needs the word “null” to report as unavaliable as thats what you get when you do a reboot
so i wanna pass a “null” to climate sensor current temperature
if i guess becomes False…
so
True pass the number
Fale pass the null
to climate.moms_furnace to current_temperature attribute
@Didgeridrew
ok i think i got it to work differently from what that other link was and yours tell me if this is coded right seems ok but currently testing it… it does the True False and sets the attritube i think its all right? currently testing
- name: "Hallway Temperature Display"
state: "{{ states('sensor.hallway_temp_sensor_temperature') }}"
availability: |
{% set ent = 'sensor.hallway_temp_sensor_temperature' %}
{{ is_number(states(ent)) and ( now() - timedelta(minutes=1) < states[ent].last_reported) }}
{%- if ent == 'True' %}
{%- else %}
{%- set ent = 'False' %}
{%- set result = state_attr('climate.moms_furnace', 'current_temperature') %}
{%- set result = 'null' %}
{{ result }}
{%- endif %}
I don’t even understand what you are trying to do with that…
The first statement sets the value of ent to an entity ID string “sensor.hallway_temp_sensor_temperature’”… it will never be “True”… but that doesn’t matter because there’s no value to return for the if statement…
Then in the else clause that will always run, you assign a value to result, then overwrite that value with the string “null”.
so when the temperature changes like 10.5 to 11 the climate current_temperature updates
but if the temperature is unavaliable it doesnt update current_temperature to null unless you Do a reboot and then it sets current_temperature to null it will not do it when the sensor goes offline
so far i got to force a null with your true and false code.
the climate. uses
target_sensor: sensor.Hallway_Temperature_Display
so far i got it to force null when the set ent is false from what you wrote
but when i do True
it doesnt like it
so it doesnt like this code
- name: "Hallway Temperature Display"
state: "{{ states('sensor.hallway_temp_sensor_temperature') }}"
availability: |
{% set ent = 'sensor.hallway_temp_sensor_temperature' %}
{{ is_number(states(ent)) and ( now() - timedelta(minutes=1) < states[ent].last_reported) }}
{%- if ent == 'True' %}
{%- set result = state_attr('climate.moms_furnace', 'current_temperature') %}
{%- set result = {{states('sensor.hallway_temp_sensor_temperature') }} %}
{{ result }}
{%- else %}
{%- set ent = 'False' %}
{%- set result = state_attr('climate.moms_furnace', 'current_temperature') %}
{%- set result = 'null' %}
{{ result }}
{%- endif %}
so the code i got to work is if its False if the sensor isnt there it sends null but if its true the sensor is there i was unable to set climate current_temperature from sensor.hallway_temp_sensor_temperature the actual valuse i figured if i left it blank like this it would work but it didnt
{% set ent = 'sensor.hallway_temp_sensor_temperature' %}
{{ is_number(states(ent)) and ( now() - timedelta(minutes=1) < states[ent].last_reported) }}
{%- if ent == 'True' %}
{%- else %}
{%- set ent = 'False' %}
{%- set result = state_attr('climate.moms_furnace', 'current_temperature') %}
{%- set result = 'null' %}
{{ result }}
{%- endif %}
ill explain better
Climate Current temperature . needs a Value or Null
it gets it from Hallway _temperature_display
Climate Sensor Does not Update on its own
so Hallway_Temperature Dispay is
If sensor.hallway_temp_sensor_temperature
is True set the value to - name: “Hallway Temperature Display” or
set 'climate.moms_furnace', 'current_temperature' depends how it updates
if False
set 'climate.moms_furnace', 'current_temperature'
to “null”
so i tried to pass the value
but it didnt like the code in the template design page
{%- set result = state_attr('climate.moms_furnace', 'current_temperature') %}
{%- set result = {{states('sensor.hallway_temp_sensor_temperature') }} %}
{{ result }}
so when my tempate sensor gets a unavliablility from the sensor it needs to pass that unavaliablity or a null to another sensor
pass the value or pass the null
reason i tried in the automation to use HA Python script Set State
to set the state to unavliable or null i guess from the Template Sensor going unavaliable
reason i said if the template sensor goes unavailiabe it doesnt update the Climate Sensor it keeps it at the same value as it was… it doesnt pass a null or unavaliable
if the temp is 10 degrees and the template sensor goes unavaliable it doesnt send Climate. current_temperature a null it keeps it at 10 degrees… and there is no automation to mod it so with ur code i got it to if the template sensor goes False (unavalible) it will set the climate.current_temperature to null but it wont pass the actual value anymore
so you can see in the picture when i got ur code to work with what i added… if the unavaliable it pass’s null to climate… now i trying if its true just pass the value
so
template sensor “Hallway Temperature Display”
gets value from sensor.hallway_temp_sensor_temperature
now i want
template sensor “Hallway Temperature Display”
if hallway_temp_sensor_temperature it has a Value pass it along normaly
if hallway_temp_sensor_temperature is unavaliable pass “null” to climate.current_temperature
now it works partly it will send the temperature a null if its unavalible. but when its avaliable its still showing the template sensor as unavliable it didnt like the If statement for True