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.
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?
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.
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
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!
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.
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
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?
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.
If anyone has any suggestion for better code I would love to hear it.
I m only learning.