Xiaomi BLE Sensor - how to disable showing last value when a sensor is not available

Hi,
I want to use Home Assistant (running on Rapsberry PI3) as my home temperature and humidity monitor. I bought 2 XIaomi LYWSD03MMC temperature/humidity sensors, obtained a bind key and flashed the apropriate yaml config to an esp32 dev board:

esphome:
  name: esphome-web-c4b644
  friendly_name: ESP32-xiaomi-teplota

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "*****************************"

ota:


wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "************"
    password: "***********"

captive_portal:

esp32_ble_tracker:
  scan_parameters:
    interval: 300ms # try with 300ms if you don't have LAN module
    window: 300ms # try with 300ms if you don't have LAN module
    active: false

sensor:
  - platform: xiaomi_lywsd03mmc
    mac_address: "A4:C1:**:**:**:**"
    bindkey: "*******************************"
    temperature:
      name: "LYWSD03MMC Temperature"
    humidity:
      name: "LYWSD03MMC Humidity"
    battery_level:
      name: "LYWSD03MMC Battery Level"
  - platform: xiaomi_lywsd03mmc
    mac_address: "A4:C1:**:**:**:**"
    bindkey: "*******************************"
    temperature:
      name: "Izba Teplota"
    humidity:
      name: "Izba Vlhkost"
    battery_level:
      name: "Izba LYWSD03MMC Battery Level"

Everything works fine - I have historical graphs in my HA dashboard and I see the current value of the entity (temperature, humidity, battery) there as well.

But the problem is when a sensor is not available. I reproduced it by removing battery from 1 sensor. I see the last measured temperature and humidity is being kept in the graph - there is a straight line keeps going. Also I see value 25.0 from “3 hours ago”. And also I think it is a bit weird it will show you points wit 5 minutes interval when you hover over the graph line - so you have assumption it works and you just have steady 25.0 degrees all the time.

2023-03-14_13-41

Is there a way how can i configure it in a way it will be understandable from checking the graph the sensor is not available (battery drain or out of reach for instance)? I mean if the temperature or humidity would not fluctuate much, one would say all works but it can be the sensor broke.

Ideally I would like to have interrupt in the graph line for instance. And also I would think displaying “null” or “n/a” or something like that when I check the entity’s value would make sense.

Thanks much for potential ideas!

Hi @stan12,

Have a look at expire_after, to allow you what you want to achieve when the sensor hasn’t been updated for a set amount of time.

Hi @DigiH ,
I assume I would have to install and configure Mosquitto MQTT Broker in HA and configure also Esphome to use it prior to setting the expire_after? Right?

Sorry, yes, my assumption was it was coming in as an MQTT sensor already.

You can maybe use the on_value trigger for the sensors and use for condition.

The condition: under for: should be something validating to true.
Im not sure if the on_value will reset the for timer for every update. you should try it.
But if the timer reset then the action under then: will be called after 5min if no new value has been seen.

on_...:
  if:
    condition:
      for:
        time: 5min
        condition:
          api.connected:
    then:
      - logger.log: API has stayed connected for at least 5 minutes!

The action could be:

id(temperatureid).publish_state("NaN");