Sensor - display last known value instead of 'unavailable'

Tags: #<Tag:0x00007f326a2516a8>

Hi!

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!
Ros

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:

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

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
    sensors:
       heizung_terrasse_target_temperatur_template:
          value_template: >
            {% if states('sensor.heizung_terrasse_target_temperatur') == "unknown" %}
              {{states('sensor.heizung_terrasse_target_temperatur_template')}}
            {% else %}
            {{states('sensor.heizung_terrasse_target_temperatur')}}
            {% 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
    sensors:
       shelly_temp:
          value_template: >
            {% if states('sensor.shelly_temp_humidity_temperature') == "unavailable" %}
              {{states('shelly_temp')}}
            {% else %}
            {{states('sensor.shelly_temp_humidity_temperature')}}
            {% endif %}
            
  - platform: template
    sensors:
       shelly_humidity:
          value_template: >
            {% if states('sensor.shelly_temp_humidity_humidity') == "unavailable" %}
              {{states('shelly_humidity')}}
            {% else %}
            {{states('sensor.shelly_temp_humidity_humidity')}}
            {% endif %}
  - platform: template
    sensors:
       shelly_battery:
          value_template: >
            {% if states('sensor.shelly_temp_humidity_battery') == "unavailable" %}
              {{states('shelly_battery')}}
            {% else %}
            {{states('sensor.shelly_temp_humidity_battery')}}
            {% 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.

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

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: https://shelly-5-eu.shelly.cloud/device/status # my shelly cloud server
    method: POST
    payload: ‘auth_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’ #you sholud retrieve your from the app
    scan_interval: 86400
    headers:
    User-Agent: Home Assistant
    Content-Type: application/x-www-form-urlencoded
    value_template: ‘{{ value_json.data.device_status.tmp.value }}’

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
    sensors:
    temp_db_combined:
    friendly_name: “Temperatura dnevni boravak kombinirano”
    unit_of_measurement: ‘°C’
    value_template: >-
    {% if states(‘sensor.shelly_shht_1_58xxxf_temperature’) != ‘unavailable’ %}
    {{states(‘sensor.shelly_shht_1_58xxxf_temperature’)}}
    {% else %}
    {{states(‘sensor.temp_cloud_db’)}}
    {% endif %}´´´

I hope this will help You.

Be aware that formating is messed.

Regards.

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.