Mqtt switch status polling

I am playing with tasmota firmware on a few sonoff mini devices, and they work great. But, if I remove power to the sonoff, the HA UI doesn’t see it go offline (no way to send out a mqtt update on powerdown). So if the lights were “ON” before, and are obviously not on anymore, the HA UI keeps showing “ON.”

Some devices I would like to be only connected to power some times, and not others. So I need a way to have HA poll the switch and trigger it to update the MQTT STATE topic (I assume this is what I’m after) and then set some sort of expiry for the mqtt update so that the switch will show “unavailable” or “offline” in the HA UI.

If I had my dream option, i could customize the text that shows up on the HA as well, but I haven’t figured that option out yet either.

Any suggestions?

There are existing mechanisms to report when an MQTT client has disconnected (not involving polling).

Each MQTT-based entity supports an availability_topic.

The MQTT topic subscribed to receive availability (online/offline) updates

In addition, there’s this:

1 Like

If I remember rightly the Sonoff Mini does not support MQTT discovery due to its small flash memory size meaning that you need to define it in your yaml. The following is an example from my config which you might like to compare and this includes the availability topic refered to by @123 Taras:

 - platform: mqtt
  name: "Ensuite Radiator"
  state_topic: "sonoff05/stat/RESULT"
  value_template: "{{ value_json.POWER }}"
  command_topic: "sonoff05/cmnd/POWER"
  payload_on: "ON"
  payload_off: "OFF"
  availability_topic: "sonoff05/tele/LWT"
  payload_available: "Online"
  payload_not_available: "Offline"
  qos: 1
  retain: false 

I saw the LWT stuff, but I didn’t see anything in the tasmota firmware to set it up, and I thought it was the responsibility of the device to trigger.

I will play around with it and see if that’s what I’m looking for. Thank you.

Joanh - the full tasmota firmware (applied via wire, not OTA) does support auto discovery afaik. I did see some auto-discovery stuff pop up in my broker, but I am not having problems with getting the device into the yaml, when it’s online and powered it works great. Just trying to solve out the puzzle of how to inform the user (via the UI) that the device is “missing” and either broken, or powered off.

If you use an MQTT client (like MQTT Explorer) you can easily inspect the discovery payload that Tasmota published for the switch’s discovery topic. Look for topics that begin with homeassistant/switch. Once you find the discovery topic for your switch, inspect its payload for information regarding availability.

Ah thanks for letting me know … I have just checked and it has 1024kb of memory, this means that I flashed mine with tasmota-lite when I could have used tasmota.bin.

I believe you can only do tasmota-lite updates through the web gui due to needing 512kb for the existing bin, and the remaining 512kb for the incoming file (which tasmota-lite fits in). I tried the full tasmota.bin OTA update and it failed, so i just slapped it on with the FTDI. The pads aren’t too bad to get at if you have a fine tip on your iron.

I am using MQTT Explorer actually. great program. I was very stumped on the MQTT stuff until i found that. HA’s inability to subscribe to a topic (other than #) in the config page.

I will take a look for the discovery packets and see what I can glean from it. I’m not sure I’ll know where to go from there but if I post up the data, maybe you guys can help.

Cheers

Ok I was going to go do chores, but this is more fun so I’ll just look into this discovery thing now. lol

Powered up the tasmota (“testmini2”) and captured the discovery broadcast.

Topic: tasmota/discovery/600194FBF38D
config=
{
  "ip": "192.168.0.60",
  "dn": "Tasmota",
  "fn": [
    "Tasmota",
    null,
    null,
    null,
    null,
    null,
    null,
    null
  ],
  "hn": "testmini2",
  "mac": "600194FBF38D",
  "md": "Sonoff Basic",
  "ofln": "Offline",
  "onln": "Online",
  "state": [
    "OFF",
    "ON",
    "TOGGLE",
    "HOLD"
  ],
  "sw": "8.5.1",
  "t": "testmini2",
  "ft": "%prefix%/%topic%/",
  "tp": [
    "cmnd",
    "stat",
    "tele"
  ],
  "rl": [
    1,
    0,
    0,
    0,
    0,
    0,
    0,
    0
  ],
  "swc": [
    -1,
    -1,
    -1,
    -1,
    -1,
    -1,
    -1,
    -1
  ],
  "btn": [
    0,
    0,
    0,
    0
  ],
  "so": {
    "11": 0,
    "13": 0,
    "17": 0,
    "20": 0,
    "30": 0,
    "68": 0,
    "73": 0,
    "80": 0
  },
  "lk": 1,
  "lt_st": 0,
  "ver": 1
}

Also

sensor=
{
  "sn": {
    "Time": "2020-10-07T00:34:54"
  },
  "ver": 1
}

Nothing obvious stands out to me… As far as I can tell the sonoff doesn’t rebroadcast it’s state unless queried. It does broadcast a LWT message in the “tele/testmini2” topic. "LWT = ‘online’ "

I assume you know that what you posted is unrelated to Home Assistant’s MQTT Discovery mechanism. It’s both the wrong MQTT topic and payload format.

Everything you need to know about it is here: MQTT Discovery

What you should be inspecting are the topics that begin with homeassistant/switch/

No It’s not obvious to me. That is the only thing that comes up related to discovery in the MQTT Explorer.

I don’t have anything “homeassistant/switch”

The only thing I get from homeassistant topic is the “status = online” when the docker starts up.

It will be after you read the documentation for MQTT Discovery.

Then your switch, running Tasmota, isn’t configured to support Home Assistant’s MQTT Discovery. See: Tasmota - Automatic Discovery

The Sonoff mini does support discovery. The only thing is : if you flash it OTA from the sonoff firmware, you need to flash it with tasmota-lite.bin due to it small remaining memory. But once flashed with tasmota-lite.bin, there is nothing to prevent you from upgraading to tasmota.bin, which supports discovery. It is just an extra OTA flash to do. Once upgraded to tasmota.bin, just enter in the tasmota console

setoption19 1

to have it discovered, including availability topic.

This is a discovery topic for a sonoff mini (configured with setoption30 1)

homeassistant/light/A9BBEF_LI_1/config

{"name":"lichtbuitenvoordeur","stat_t":"lichtbuitenvoordeur/tele/STATE","avty_t":"lichtbuitenvoordeur/tele/LWT","pl_avail":"Online","pl_not_avail":"Offline","cmd_t":"lichtbuitenvoordeur/cmnd/POWER","val_tpl":"{{value_json.POWER}}","pl_off":"OFF","pl_on":"ON","uniq_id":"A9BBEF_LI_1","dev":{"ids":["A9BBEF"]}}

Usually sending status 5 to the tasmota topic helps you update the real status, i got an automatization for that in every 1 min. The UNAVAILABLE attribute is not related to this.

payload: 5
topic: cmnd/sonoffs/status