I was also struggling with state/retain after restarts reading this topic.
I am running all TASMOTA and to obtain status after HA/MQTT restart, created a small pyscript (=custom component) to pick up the tele commands of TASMOTA.
So after 5 minutes (default TASMOTA tele timing) all status messages are received and switches set accordingly in HA.
@linuxbak:/var/lib/docker/volumes/ha-storage/_data$ cat pyscript/tasmota_tele.py
@mqtt_trigger("mc35/tele/#")
def tasmota_tele(topic=None, payload=None, payload_obj=None):
#log.info(f"tasmota_tele: got action {topic} id {payload_obj} {type(payload_obj)}")
#log.info(payload)
if payload_obj is not None:
#log.info(payload_obj)
#log.info(payload_obj.get("POWER","No Power found in json"))
t = topic.split("/")
tt = t[2].lower()
if tt not in ["vera","masu1","masu2"]:
entp = payload_obj.get("POWER","").lower()
if entp != "":
ents = state.get("switch.%s"%tt)
if entp == ents:
log.info("XX %s: %s == %s" % (tt, entp, ents) )
else:
log.info("XX %s: %s <> %s" % (tt, entp, ents) )
state.set("switch.%s"%tt, entp)
#else:
# log.info("No json payload")
The mqtt devices are defined as below. The name must be the same as the mqtt device name. Still checking out the patch that allows a friendly name on mqtt switches for the UI…
homeassistant:
customize:
################################################
## Node Anchors
################################################
switch.node_anchors:
switch: &tasmota_sw
platform: mqtt
qos: 1
payload_on: "ON"
payload_off: "OFF"
payload_available: "Online"
payload_not_available: "Offline"
retain: false
switch:
- <<: *tasmota_sw
name: "bw201"
state_topic: "mc35/stat/bw201/POWER"
command_topic: "mc35/cmnd/bw201/POWER"
availability_topic: "mc35/tele/bw201/LWT"
- <<: *tasmota_sw
name: "bw202"
state_topic: "mc35/stat/bw202/POWER"
command_topic: "mc35/cmnd/bw202/POWER"
availability_topic: "mc35/tele/bw202/LWT"