Online/Offline availability_topic config for MQTT Switch

From version 0.50 MQTT Switch: add availability_topic for online/offline status, but i can not make it work.
I use this configuration for ESP8266 base switch:

- platform: mqtt
  name: 'Office Light'
  state_topic: 'office/light1/status'
  command_topic: 'office/light1/switch'
  availability_topic: "office/light1/available"
  payload_on: "ON"
  payload_off: "OFF"
  payload_available: "online"
  payload_not_available: "offline"

On the ESP 8266 i send birth message and lwt message. But the switch always show “unavailable”

make sure your device is sub/pub to the correct topics. Have you checked the logs or subbed to the topic via command line to see if the messages are being sent?

Thank you. It is my fault, send wrong topic to the MQTT server.

Hi,

I’ve try this but you can share how do this in esp8266.

Thank you for help.

Can you share how you have done the MQTT topics on the ESP, I want to implement the same on my ESP Switches too.

Thanks. @dzungpv

This is the config, you must send available signal periodically:

  • platform: mqtt
    name: ‘Office Light’
    state_topic: ‘office/light1/status’
    command_topic: ‘office/light1/switch’
    availability_topic: “office/light1/available”
    payload_on: “ON”
    payload_off: “OFF”
    payload_available: “online”
    payload_not_available: “offline”

this helps immensely @dzungpv

I’m having problem setting up my MQTT Switch. I can see it in HA front end, but it does not execute. I think the problem is with the correct command_topic. Where can I find the correct path?

This is how my config looks like:

  - platform: mqtt
    name: "LED"
    command_topic: "cmnd/sonoffLED/"
    state_topic: "stat/sonoffLED/"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true

and this is the info of the Sonoff:

MQTT Host 192.168.1.233
MQTT Port 1883
MQTT Client &
Fallback Topic DVES_E724DD
MQTT User pi
MQTT Topic sonoffLED
MQTT Group Topic sonoffs
MQTT Full Topic cmnd/sonoffLED/

I had problem with availability too. But I found bug on my side in ESP32 code.

OFFLINE[]=“offlne”
BUGGY: publish(…, OFFLINE, sizeof(OFFLINE), … ) // willl send payload with \0 at the end
OK: publish(…, OFFLINE, sizeof(OFFLINE) -1, … ) // without the \0

I hope it will help somebody, who also will find this topic :slight_smile:

1 Like

Take a look at: https://github.com/arendst/Sonoff-Tasmota/wiki/Home-Assistant
Your topics are missing something at the end, probably should be:

    command_topic: "cmnd/sonoffLED/POWER"
    state_topic: "stat/sonoffLED/POWER"

Or maybe POWER1 or SWITCH1, depending on your model.

1 Like