Mqtt Sensors out of sync after power loss

I have a Wemos D1 mini flashed with Tasmota fw connected to a simple reed switch to act as a door sensor.

On Pi Hassio side I use:

#binary sensors

  • platform: mqtt
    name: “Wemos1”
    state_topic: “stat/Wemos1/POWER”
    payload_open: “ON”
    payload_close: “OFF”
    availability_topic: “tele/Wemos1/LWT”
    payload_available: “Online”
    payload_unavailable: “Offline”
    device_class: door
    retain: true

The sensor is working fine, even if I restart HA, sensor status is always at sync with HA front end.

The problem is if the sensor changes state when there is no power on the wemos side,
status is not updated on HA frontend when sensor comes back online again.

Any ideas how to setup the wemos sensors on HA so that they are always at sync?
I believe something like this is crusial if I want to use HA for security purposes.

Thanks.

ideally you should have it run a status check after a power cycle and publish that message, or when HA starts have it ask the device its current status.

That’s the principle I’m looking, just dont know how to implement it on code.
Hassio runs on PI model 3 with mosquitto broker. Wemos D1 mini runs on Tasmota.
Any suggestions?

You could drop Tasmota and go with ESPEasy. It allows you to run rules at boot up. Then you just poll the pin and publish the message. I only have 1 device with tasmota ( a sonoff as a fan switch) but for all my Wemos D1 devices I use ESPEasy because it’s just better. So much more it can do.

I tried ESP easy mega-20180504 just today in a Wemos D1 module.

So far I can see it as a sensor in HA front end with the below code and it is working as it should.

  • platform: mqtt
    name: “WemosD1”
    state_topic: “WemosD1/Reed/status”
    payload_on: “0”
    payload_off: “1”
    device_class: door
    retain: true

In ESP easy I set it up as a normal switch in Devices, and I have enabled in Tools advanced the MQTT Retain Msg. The reed switch is connected in GPIO-14 D5. I have also enabled the rules. (nothing there though yet)

With these settings so far, ESPeasy behaves like tasmota on HA. State out of sync on power loss.

Anything else I can do so HA and Wemos are always at sync?

Thanks

if you go into advanced you can look check a box for ‘Rules’. Then you get a new menu option. inside that you can just have a routine that runs on bootup.

look atESPEasy rules

Also you need to make sure that your topics are the same across devices. something as simple as a capital letter or a / can mess it up. With the retain flag it should be subscribing after power cycle and getting the last status, which makes it sound like a bad topic somewhere. If you can work the command line watch your MQTT messages and see what’s being pub/sub’d to the broker.

So any suggestions of actual code to write on espeasy rules that will work?

Anyone with an actual solution of code either on Tasmota or on ESPeasy fw that has worked for him?
We want a sensor to be able to send its current status after the sensor restarts or after it connects to mqtt.
Pretty fundamental for any kind of on/off sensor.

Thank you

If you use esphomeyaml it will automatically make sure all states are correctly sent after each restart/reconnect and that they’re always persisted :slight_smile:

1 Like
on System#Boot do
  if [doorSwitch#doorState] = 1
    publish WemosD1/Reed/status,0
  else
    publish WemosD1/Reed/status,1
  endif
endon

fill in your topics and states as needed

1 Like

forsquirel, you nailed it. Thank you very very much !!!

I was testing possible solution for days without any luck but with your guidance I got the result I wanted.
Yes, / and capital letters or not, are enough to mess your result completely.
Took me a lot of reading, trial and error to get here. But you were right.

Using a Wemos D1 Mini and a plain reed switch and HA.
For others, the working code that I end up using in ESP Easy Rules is:

on MQTT#Connected do
if [Reed#Status] = 1
publish %sysname%/Reed/Status,1
else
publish %sysname%/Reed/Status,0
endif
endon

Reed and Status are the Switch Name and Value Name accordingly under Devices in ESP Easy mega-20180504 fw.

%sysname% is the Unit Name under Config in ESP Easy fw. Used a variable so no matter how I name the rest of my door/window sensors in the fw I can always use the same rule.

Using MQTT#Connected covers all bases. No matter if power/WiFi or HA is down when Wemos module connects to MQTT, HA will always show the correct sensor status!

In the sensors section in HA I used:

  • platform: mqtt
    name: “Wemos1”
    state_topic: “Wemos1/Reed/Status”
    payload_on: “1”
    payload_off: “0”
    device_class: door
    retain: true

At last everything works like a charm !

Now up to my next task, to find a way for HA to know when wemos sensor is online or offline.

That’s done as well… Look into ‘Last Will and Testament’ messages.

https://www.hivemq.com/blog/mqtt-essentials-part-9-last-will-and-testament

When a device is offline the LWT message gets published. Essentially all you need to do is have a sensor that reads the current message and if it ever hits your LWT (Which you can make whatever you want such as, Disconnected, Offline, Oh My! I’m dead! and so on) then HA will react to the sensor update and there you go.

mta:

when a wemos D1 with ESPEasy boots up it connects to the broker and throws out a LWT message such as ha/motionOne/status/LWT Connected which tells the broker that it has indeed connected to the system. Now should it go offline the last LWT message gets published, but published by the broker since the client told the broker the LWT message upon connect. So no when your device goes offline or unresponsive you’ll see ha/motionOne/status/LWT Connection Lost and there for knowing there’s an issue. hope this makes sense. i’m tired, really tired and need sleep. but good look on your en devours

Very very nice :smiley:
Based on that:

I had created a sensor in the past

  • platform: mqtt
    name: “Wemos1 Power”
    state_topic: “tele/Wemos1/LWT”
    payload_on: “Online”
    payload_off: “Offline”
    device_class: plug
    retain: true

that was working fine with Tasmota on Wemos.

Now based on your instructions I found the topic and values that were missing from my code,
so I created this sensor with ESPEasy Wemos on HA:

  • platform: mqtt
    name: “Wemos1 Power”
    state_topic: “Wemos1/status/LWT”
    payload_on: “Connected”
    payload_off: “Connection Lost”
    device_class: plug
    retain: true

which works just great !

Keep in mind that I am a hardware guy. I have no background or any knowledge on programming.
I am learning all these the slow and painfull way.

forsquirel you 're awesome. Thank you very much again for the guidance and your patience.

Now, I have a door sensor that states open or closed on HA, and I have a separate power sensor that states if the first sensor is actually online or offline. Both perform and behave excellent.

If I wanted the door sensor to state open or close but when it is offline to state as unavailable on HA,
which way would be best to do that? Availability topic and values on the door sensor (tried that but only works for me if door sensor is offline when HA restart) or some automation that gets triggered by the power sensor and passes the value to the door sensor?

Found it the hard way.
In case anyone else is interested:

Wemos D1 mini flashed with ESPEasy Mega

Sensor code for HA side:

  • platform: mqtt
    name: “Wemos1”
    state_topic: “Wemos1/Reed/Status”
    payload_on: “1”
    payload_off: “0”
    availability_topic: “Wemos1/status/LWT”
    payload_available: “Connected”
    payload_not_available: “Connection Lost”
    device_class: door
    retain: true

With this availability topic and those payload values if the sensor is available/online shows open/close state accordingly as normal behaviour. If the sensor is not available/offline shows as unavailable in HA and mdi:icon gets greyed. :wink:

If anyone has any suggestion for better code I would love to hear it.
I m only learning.

Thanks, but I’m still learning myself and some of it is still a guessing game for me. There’s so many different firmwares and devices that just trying to keep up can get overwhelming.

Glad you got it working!