How do i make a TemplateSensor Go Unavaliable when the sensor goes offline

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

    - name: "Hallway Temperature Display"
      #      state: "{{ states('sensor.bluetooth_server_3_hallway_temperature_xiaomi') }}"
      #      availability: "{{ is_number(states('sensor.bluetooth_server_3_hallway_temperature_xiaomi')) }}"
      state: "{{ states('sensor.hallway_temp_sensor_temperature') }}"
      availability: "{{ is_number(states('sensor.hallway_temp_sensor_temperature')) }}"
      icon: "mdi:thermometer"
      unit_of_measurement: "°C"
      device_class: temperature

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

sequence:
  - action: python_script.set_state
    metadata: {}
    data:
      entity_id: {{state_attr('climate.moms_furnace', 'current_temperature')}}
      state: -100
alias: "!!set"
description: ""

i trying to force the state to be -100 or unavaliable but it wont let me set state that specific attribute

i looked at this website but i dont know how to code it to set result for my specific issues
https://haade.fr/en/blog/change-the-result-of-one-entity-state-attribute-command-to-another-in-home-assistant-with-templates

i can do this in template but i have no idea how to do it in an automation

{%- set result = state_attr('climate.moms_furnace', 'current_temperature') %}
{%- set result = 'unavaliable' %}
{{ result }}

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:

  1. The template is not surrounded in quotes.
  2. The template is placed to provide the value for an entity_id key, but will not return an entity ID.
  3. -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 it kinda works… if its true it doesnt pass the value to the current_temperature it keeps it null

what i trying to do is change CLIMATE. Current_Temperature
cuz there is a bug in it as it cant update on its own

so i trying to Update Climate. current_temperature to a Value or 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

How are the template sensor and climate entity linked?

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 %}

Again, everything you added to the availability template I provided in nonsense…

I don’t know why you keep talking about setting things to “null”… an availability template should always be designed to return true or false.

And you still have not explained how this template sensor is connected to the climate entity.

Climate Sensor u can not use True or Fale

I

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”

What does any of that have to do with a template sensor’s availability?

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 }}

it doesnt
i asked for help on how to do it and if i need it in my template sensor since it cant be done in an automation

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


guess not working right the temperature is avaliablty but its showing unavaliable so thats messed

You can’t pass states from one entity to another. An entities state is immutable. You can only create helper entities on top of existing entities.

I suggest you post your whole problem instead of the current path you’re trying to go down. This sounds very similar to the XY problem.

with my disexlia i guess not explaining it right

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