Sensor - display last known value instead of 'unavailable'


I have created an ultrasonic sensor which is mounted into a sewer cover and measures the amount of capacity left in the sewer tank. This sensor is based on ESPhome. This sensor gets power supply only a couple hours a day and is offline for the rest of the day. When offline, it shows up as ‘unavailable’ and displays no data on the history graph. Is there a way to make HA display the last know value instead of ‘unavailable’ (e.g. the measurement value from when the sensor has been online for the last time)?

Thank you!

you probably need to set up a template sensor for this that only updates if the value is not unavailable…

Thanks! Would this be something like if not ‘unavailable’ then equals to the other sensor’s value?

…or you could use Input Number to store the sensor readings. This would be preserved over a reboot.

Did you find the way ?
I try tto do the same thing :
I have 4 sensor entities to get ink levels and a 5th to get status of my printer. When my printer is on, I have the ink levels like this :

When my printer is off :

I’d like to get the last ink levels with normal color drop icon when printer is off
and the update ink levels with personalised ink color (black, yellow, magenta, cyan) drop icon when printer is on

I looked around templates and icon_color with custom UI but without success
Thanks for you help

I found a solution :slight_smile:

          friendly_name: Encre noire
          value_template: >
            {% if states('sensor.encore_noire_V1') == "unavailable" %}
            {% else %}
            {% endif %}

Sorry, I’m a newbie, can you explain what you did in the example above. I have a Shelly H+T sensor that only updates once an hour and i want to display the last known temperature until it becomes available again

Tried to match your example to my sensor, but the value of the template is still unknown.
I have tried == "unavailable" as well as == "unknown".

  - platform: template
          value_template: >
            {% if states('sensor.heizung_terrasse_target_temperatur') == "unknown" %}
            {% else %}
            {% endif %}

MIght someone give another example?

1 Like

Exactly the same thing happened here. I added the template and it display the values correctly however as soon as the sensor becomes unavailable the template values revert to “unknown”

- platform: template
          value_template: >
            {% if states('sensor.shelly_temp_humidity_temperature') == "unavailable" %}
            {% else %}
            {% endif %}
  - platform: template
          value_template: >
            {% if states('sensor.shelly_temp_humidity_humidity') == "unavailable" %}
            {% else %}
            {% endif %}
  - platform: template
          value_template: >
            {% if states('sensor.shelly_temp_humidity_battery') == "unavailable" %}
            {% else %}
            {% endif %}

I dont think that you can ref a template sensor within itself.

any other ideas?

Define a filter sensor with lower and upper bounds with a range filter. This will not display NaNs.

- platform: filter
  name: "filtered zappi house"
  entity_id: sensor.zappi_house
    - filter: range
      lower_bound: -20000
      upper_bound: 20000

I hade same problem and I have the solution only if you are connected to the cloud. So if your shelly is connected to the cloud you should create REST sensor which will pull data from the cloud.

  • platform: rest
    name: temp_cloud_db # temperature reading from shelly cloud
    unit_of_measurement: ‘°C’ #added unit of measurement, °C in my case
    resource: # my shelly cloud server
    method: POST
    payload: ‘auth_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’ #you sholud retrieve your from the app
    scan_interval: 86400
    User-Agent: Home Assistant
    Content-Type: application/x-www-form-urlencoded
    value_template: ‘{{ }}’

With this I have new sensor in HA which retrieve temperature readings from the cloud.
Next step is creating TEMPLATE sensor for temperature which will take temperature from your shelly or in other case from cloud so You will never have NaN displayed.

  • platform: template #TEMPLATE temperature sensor
    friendly_name: “Temperatura dnevni boravak kombinirano”
    unit_of_measurement: ‘°C’
    value_template: >-
    {% if states(‘sensor.shelly_shht_1_58xxxf_temperature’) != ‘unavailable’ %}
    {% else %}
    {% endif %}´´´

I hope this will help You.

Be aware that formating is messed.


1 Like

After updating to 2020.12.0, it stopped working, before it kept the value when the sensor was not available, now it shows unknown.

I’m facing a very similar issue. ESP32 board with BMP280 temperature + pressure sensor, turning on for 30 seconds and then going to deep sleep, running EspHome but communicating with HomeAssistant via MQTT. This “unavailable” is poor usability, especially on the phone where history graph is tough to hit for last recorded values.

There is an older thread about similar problem where it seems to have been fixed with value templates: Binary Sensor last known state when unavailable

I’m completely new to HomeAssistant so I’d be grateful for some pointers where should I click or which yaml files should I edit. As for now, I’m running an auto-generated Lovelace dashboard.


Yeah just noticed this, how annoying. Such a basic feature and it’s completely missing.

1 Like

I have a similar scenario looking for a solution: A Tasmota ESP8266 reporting the readings of HC-SR04 ultrasonic distance sensor via MQTT before going into DeepSleepTime.

On going into deep sleep, Tasmota obviously sends a MQTT LWT (Last Will Testament) indicating offline, which causes a value of unavailable. Here my typical readings:

I failed to find an easy solution. I tried:

Not working: Outlier filter

  - platform: filter
    name: "sensor.zisterne_echolot_sr04_distance.filtered"
    entity_id: sensor.zisterne_echolot_sr04_distance
      - filter: outlier
        window_size: 5
        radius: 80

Not working: Declaring a template sensor

  - platform: template
        friendly_name: "Zisterne w/o unavailable"
        value_template: >
          {% if not states('sensor.zisterne_echolot_sr04_distance') in ['unavailable', 'unknown', 'none'] %}
            {{ states('sensor.zisterne_echolot_sr04_distance') | float }}
          {% else %}
            {{ states('sensor_zisterne_echolot_sr04_distance_template') | float }}
          {% endif %}
        unit_of_measurement: 'cm'

Both didn’t lead to a success.

Can anybody give me a hints for a simple solution?

Small side comment: I feel that I’m looking for sth. very trivial but my current two, failed approaches already feel quite clumsy & boiler-plate code.

To anyone still having this issue, I was able to solve it using the instructions in this thread:

This does not solve the root issue and unfortunately is also not an option for my Tasmota setup: Sorry – I was wrong.

If I get it right, the proposal in the referred thread is to reconfigure the MQTT sensor, so that HASS it no longer reacts on offline / online messages.

coming from a battery powered tasmota and some smart power meter i simply created my own sensor that fetches the latest mqtt value:

# --- power meter
- platform: mqtt
  state_topic: tele/tasmota_55D63F/SENSOR
  name: "Stromzaehler Total"
  unique_id: a30a8bd5-682e-449a-b49e-12e1a58a238e
  value_template: '{{ value_json["Wohnung"]["power_total"] }}'
  unit_of_measurement: "kWh"
- platform: mqtt
  state_topic: tele/tasmota_55D63F/SENSOR
  name: "Stromzaehler Curr"
  unique_id: 0d619efa-5f63-45a3-b226-e7d4e932cd3f
  value_template: '{{ value_json["Wohnung"]["power_curr"] }}'
  unit_of_measurement: "W"
1 Like