Remove unknown state of a sensor

Hi everyone, I have a temperature sensor that occasionally becomes unavailable for a few seconds and then reconnects.
I would like to filter the unavailable status so that when I disconnect, the last value sent by the sensor appears instead of “unknown”. I tried with a sensor template but I can’t. How could I do?

template:
   - sensor:
      - name: 'temperatura_interna'
        state: >-
            {% if (states('sensor.temperatura_interna_nofiltro')) == 'unknown' %}
              {{ states('sensor.temperatura_interna') }}
            {% else %}
              {{ states('sensor.temperatura_interna_nofiltro') }}
            {% endif %}
        unit_of_measurement: "°C"
        device_class: temperature

imo template sensor doesn’t turn unavailable if not commanded to do so
See documentation of the template, especially ‘availability’ part

Simply, pass your sensor state to template state and it should work.

I would expect that unknown state works the same way

I tried to do such a thing (I think) but it doesn’t work in the sense that when the state becomes unavalaible with the code I wrote the sensor value disappears because in the condition I didn’t write anything, how could I tell it to leave the value as it is 'is?

Yes the availability template is not waht you want. It will actually mark the sensor as unavailable.

Try this:

template:
   - sensor:
       - name: 'Temperatura Interna Filtered'
         state: >
           {% if states('sensor.temperatura_interna_nofiltro') not in ['unavailable', 'unknown'] %}
             {{ states('sensor.temperatura_interna') }}
           {% else %}
             {{ this.state }}
           {% endif %}
         unit_of_measurement: "°C"
         device_class: temperature
1 Like

Hi
I tried, but when the media_player entity is on I see the state (eg. Channel), when the media_player entity is “unknow” the template state is “null”

{% if states('media_player.lg_webos_smart_tv') not in ['unavailable', 'unknown'] %}
  {{ state_attr('media_player.lg_webos_smart_tv', 'media_title') }}
{% else %}
  {{ this.state }}
{% endif %}

The problem could be that:
media_title attribute is present only when the tv is on, when the tv is off this attribute disappears.

My idea was to create a sensor that tells me the current tv channel, using the media_title attribute of the media_player.lg_webos_smart_tv entity (and I managed to get that), but set a custom state (e.g. off) when the tv is off .

template:
  sensor:
    - name: "Canale TV"
      unique_id: "canale_tv"
      state: "{{ state_attr('media_player.lg_webos_smart_tv', 'media_title') }}"
      icon: "mdi:television-classic"