Report as “good” instead of “unknown” for MQTT binary sensor

Is there a way to have the battery report as “good” instead of “unknown” in Lovelace?
Here is a sample code:

  - platform: mqtt
    name: "Main Bathroom Window sensor battery"
    state_topic: "home/sensor10_battery"
    device_class: battery
    qos: 1

I just want to make it if there is nothing sent (since it only sends if there is a low battery) that it states Good or OK.

This is a result of changes made to MQTT in 2022.2 - states for binary sensors used to default to off, now default to unknown:

Screen Shot 2022-03-04 at 12.27.17

I’m still looking for a fix - so if you find one please post it.

Oh, got it. That makes sense, since I swear I thought it used to say good or ok, and my leak sensors said dry instead of unknown.

Thank you.

You can make a template sensor from it that you can set on your own preferences.

Yeah not happening for the 48 binary sensors I have.

If it comes to it I’ll write an automation that checks all of them for unknown state on startup and publishes an OFF message. At least it will be only one object to manage.

Any chance you could assist me with that? Is it the “assumed state” option?

Put something like this in your configuration.yaml and reboot.
It simply sets a sensor to on, if the real sensor is on and else it will set the sensor to off regardless of what the real sensor report.
The icon_template section can be omitted.

sensor some_name_section:
  - platform: template
        friendly_name: "sensor1"
        value_template: >-
          {% if is_state('sensor.real_sensor','on')  %}
          {% else %}
          {% endif %}
        icon_template: >-
          {% if is_state('sensor.real_sensor','on')  %}
          {% else %}
          {% endif %}

I’ll try this shortly. Thanks.

I am having this issue since the 2022.2 update and find it quite annoying. I have several 433mhz door/window sensors that are connected via an sonoff rf bridge (tasmotized). Ever since this update my sensors are moving to an “unknow” state after restarting HA or after a extended period of time. Is there an official work around for this? Is there a Tasmota command to force it to “retain” sensor states? I would prefer that these sensors display their last “know” state instead of changing to unknown.

interesting. Only my battery level does this (because it doesn’t broadcast “good”. How do you have your sensors setup? I believe I have it retain last state.

I use the demux option.

d = { '9F799A':['sensor1','ON','true'],

I believe the true retains it.

Sensors reported through the bridges are “stateless” if your setup is anything like mine.

My bridges just report the code received, which is then decoded by a python script into the state topic to be published. Thus no setting on the bridge can have an effect.

My short term fix is to publish on “off” state for every binary sensor in “unknown” at startup. I’ll post my automation once the weekend is over.

My preferred long term solution is to have an assumed state option on any MQTT binary sensor or switch, but I don’t hold out much hope for that happening.


This is exactly what I want! That would be great. I did find a python script that might do it, but I’ll wait for your info.

1 Like

use strategy 2 :

Works perfect.

I use strategy 2 - when you say “works perfect” do you mean your sensors don’t come up as “unknown” after reboot?

Yes. You just need to set them as retained.

This doesn’t work if it is battery sensor or leak detector.

Ok - the problem occurs when a sensor only has one state - “on” - and no off state…

Sensors that publish both an on and off are fine.

I am going to have to think of another strategy to handle these.

1 Like

Are you using off_delay?

Or are you saying that option has ceased to work correctly after the recent introduction of an unknown state for MQTT sensors?

I get what you guys mean with the “unknown” state at Home Assistant reboot, I have the same.
And in my automations I did a test of the button going from “OFF” to “ON” but the button is only a pushbutton and will never send an OFF signal to RF.

Result after reboot HA the state is OFF, when I push the button it goes to “ON” and because I set an off_delay of 1 after one second the state will go to OFF.
Issue: the first time after reboot the button will not work as it goes from “Unknown” to “ON” and not “OFF” to “ON”

Example of 1 of my buttons:

  - platform: mqtt
    state_topic: "home/sonoffRemoteB1"
    name: 'Sonoff Remote Button 1'
    off_delay: 1

Python script:

d = {   'code8E7218':['sonoffRemoteB1','ON','false']

p = data.get('payload')

if p is not None:
  if p in d.keys():
    service_data = {'topic':'home/{}'.format(d[p][0]), 'payload':'{}'.format(d[p][1]), 'qos':0, 'retain':'{}'.format(d[p][2])}
    service_data = {'topic':'home/unknown1', 'payload':'{}'.format(p), 'qos':0, 'retain':'false'}
    logger.warning('<rfbridge_demux1> Received unknown RF command: {}'.format(p))'mqtt', 'publish', service_data, False)

The Solution to get an “OFF” state in stead of “Unknown” after reboot of Home Assistant:
Make a script to set the state of the devices to “OFF”

  alias: Startup RF Devices
  - service: mqtt.publish
      topic: home/sonoffRemoteB1
      payload: 'OFF'
  mode: single

add an automation that runs after startup of Home Assistant:

- id: '1549662264057'
  alias: Home Assistant ON
  - event: start
    platform: homeassistant
  - service: script.s
    data: {}

Ofcourse you can add the MQTT publish directly in the startup-automation, but I like to keep it organised.

Hope this helps you guys.


Thanks. Will try this.

edit: This worked. Set state to OFF! Thanks.