Binary sensor: keep last status when sensor is unavailable

I’ve spent quite a few hours searching for a solution for this one, but somehow I can’t wrap my head around getting it to work.

I’ve got an ESP8266 running esphome connected to a binary sensor. It passes status information to HA via MQTT. That part works.

I’m running the ESP off of a battery though and utilizing deep sleep to save power. Therefor, the sensor turns “unavailable” in HA as soon as the ESP goes to sleep.

Is there any way to have the sensor in HA display the last known status until the next MQTT update is sent?

I tried using a template binary sensor but as far as I know, you can’t toggle those and I have not managed to have the template one mirror the real ones status.

To give a broader picture of what I’m trying to achieve: I want to create cheap window sensors that last quite a while. Since those don’t need to be updated too frequently, I’m letting them do their thing for 20 seconds and then put them in deep sleep for 10 minutes.

Any help would be greatly appreciated.

Can you please paste the related configuration.yaml code for the binary sensor?
My simple thought (I am no expert) would be to remove the availability topic from the sensor if you have it set.

I have a binary sensor for a door that is a 433mhz transmitter.
This runs on batteries as well, and it only uses the state_topic in HA.
Since there is no availability topic listed it just updates when it gets a signal.
I expect this is what you are looking to do.

Example sensor:

binary_sensor:
  - platform: mqtt
    name: sensor_name
    state_topic: example/state/topic
    payload_on: payload_value_on
    payload_off: payload_value_off
    device_class: icon_class

I believe you only need the payload sensors if they are not “on” and “off” in the MQTT message.
If you sensor sends “on” and “off” you can even skip the two payload values.

1 Like

I have actually not defined the binary_sensor in the configuration.yaml, but rather used the MQTT integration shipped with HA. I’ll try defining it, too.

What would I have to set for state_topic? I’m assuming, if the name is sensor_name, it would be sensor_name/state/topic?

install an MQTT client on your PC or phone and subscribe to your broker.
That is the best way to find the topic.
If you don’t have many sensors yet, you can just subscribe to # to get all messages.

I haven’t used the MQTT integration in HA so I don’t know what it looks like.
I would think you can modify it there - but I’d need to see what the screen looks like.

DeadEnd

1 Like

Mate, you’re a lifesaver.

Just as you said, I got the state_topic with MQTT.fx, added another binary sensor in the configuration.yaml, set a name and the device_class and it’s working.

I always expected the entries in configuration.yaml to be redundant when using the internal MQTT Integration, but I was wrong.

Thank you very much!

1 Like

I’ll be honest - I don’t know if it is redundant or not… lol. Like I said I haven’t used the integration.
In my integration screen the mqtt is listed like this:

image

This is because I have mqtt: defined in my configuration.yaml (with confidential details in secrets.yaml):

mqtt:
  broker: !secret mqtt_broker
  port: !secret mqtt_port

So all my mqtt sensors are setup in configuration.yaml file.
The only things I am currently doing via integration is mobile_app…
Mainly because I don’t know how to set them manually - :slight_smile:.

DeadEnd

That’s interesting. I guess there must be some kind of reference from an MQTT sensor defined in configuration.yaml to whatever is picked up by the MQTT integration then, because I have no reference to MQTT except for the one in the sensor in my configuration.yaml.

Still, it works. And I guess that is the only fact that counts. :smile: