Mqtt "sync" question

Covid, a drawer full of electronic bits and pieces, time to play. On an ESP I setup a rotary encoder hooked it up to my garage door and with ui-level I can now tell how far up or down my door is graphically. The problem is power failure. The door is half up so the dog can go in or out then the power fails and I put the door down. Now mqtt thinks the door is still half up and when power comes on the display shows the door half up when it is down. Trying to reason out a good way to re-sync everything. Let's say the count goes from 0 to 100. Mqtt now thinks the count is 50 when it should be 0. About the only thing I have is to setup another mqtt node with the same topic and send a 0 via a push button. Any other thoughts?

Is it not possible to "ask" the rotary for current value?

Otherwise having sensors for "fully open" and "closed" connected to inputs of the ESP (assuming ESP32) you could use those states to set the rotary value once connection is back. If open set to 100, if closed set to 0, otherwise keep old value? Not fool prof if you happen to move the door partly up or down while power is off

When the power goes off upon power on the ESP resets and the value is set to zero. Spent quite a bit of time thinking about that one but there is no place for me to store a value that will survive a power reset. At least that I have found. I had considered the switch route which has the advantage that every time the door hits full closed say, I'm assured that things line up. I do have the parts to make that work. Just have to modify the ESP code. I'll mark that as alternative number one.

Well, maybe this could be something

Mine is the ESP8266 NODE MCU , not sure that will work but I'll dig a little deeper.

Otherwise buy an ESP32, they are not that expensive

One thing I wonder about is why the ESP is losing power. Is'nt it permantly supplied with power? If so, why not build a small ups? I did that myself with some few components and standard rechargeable batteries

Any controller of this type will surely suffer the same issue. That is why many door controls will fail open or closed so that they reset to a known position. Also why any commercial powered door will have additional sensors to prevent it from chopping your dog in 1/2! Or, more mundanely, burning the motor out if the controller gets out of step and tries to continue to shut the door when it is already shut.

So a disconnected electronic control isn't enough to safely control a door I don't think. Add a simple sensor to it at least. Perhaps an IR gate sensor that detects whether it has a clear path to a reflector on the other side of the opening. This would have the benefit of being certain if the door is (partially) open and if something is obstructing the path of the door.

You can certainly save to permanent memory on an ESP8266 as well.

This "system" isn't meant to control anything. It is only an indication system. All of the safety function, features present in the factory door opener are there and operational.
Looked at saving to memory and I did find a method to save to memory. However I would need to save every count during open and close which makes me wonder if the memory can take that much stress. Or if I might drop counts during the memory write. But... I do have time to play with it and see if that works

Oops, I misunderstood and read "rotary encoder" as a controller! What can I say, it's been a loooong week :frowning:

Maybe set up a sound detector and listen for the dog barking - then you would know that the door was closed?! :rofl:

You certainly shouldn't save every value of the encoder, that will kill your ESP quickly, especially if you ended up with a cheap clone.

Just put a small delay in after a change in values, if you get another value inside that delay, reset the delay. On expiry of the delay, save the value. Alternatively, have a periodic check of the value and only save if it has changed (every 30sec maybe?)

The way the encoder works is there is no output if the encoder doesn't move. So if the door stays in one position there would never be a memory write. However if I don't save after every count I'm afraid over time the two (indicator and door) would drift apart. ( the encoder actually clocks itself)

Instead of saving in the ESP, you could save & build the logic for this on the Node-RED (NR) side. From the ESP, just send the simple output via MQTT to NR. In NR start a 5s timer that is killed/restarted for each received "tick". Once there is no more incoming "ticks", door movement has stopped, the timer fires and a function saves the resulting counter persistent (eventually publish it to a decicated MQTT broker topic with the retained flag set)
In NR you could easily add "buttons" to reset or sync the counter with the actual state of the door (open/closed). When and if NR or your computer would need a restart it would allways receive the latest known counter value that hopefully should reflect the real world

But regardless, if you move the door while power failure, you will be out of sync. To have some kind of "semi-automatic sync" you could add those magnetic contacts for closed and open door and connect them to the ESP and simply send the state changes to NR. When NR receieves those messages, the counter could be set to either value, in this way keeping the counter inside "reasonable" boundaries 0 to 100 or whatever fits

Just a simple "simulator" below

I assume that the "ticks" also have a sign telling the direction, in my test below I have assumed either +1 for opening and -1 when closing for each "tick". You can simulate opening and closing using the inject buttons. After 5s and no more ticks, the door position value is published to the MQTT broker with the retained flag set. I have also added buttons to be used to synchronize the state, either when fully closed or fully open door. (If you would have some sensors for those states you could simply add some nodes capturing & inject those values into the buttons to achieve automatic sync)

[{"id":"d9622d3d.49391","type":"inject","z":"bf12ff9c.cd354","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":220,"y":190,"wires":[["e2aaaae3.c80f88"]]},{"id":"df3ec878.1616c8","type":"inject","z":"bf12ff9c.cd354","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"-1","payloadType":"num","x":220,"y":250,"wires":[["e2aaaae3.c80f88"]]},{"id":"e2aaaae3.c80f88","type":"function","z":"bf12ff9c.cd354","name":"","func":"let door_pos = parseInt(flow.get('door_pos'))||0;\ndoor_pos = door_pos + msg.payload;\nmsg.payload = door_pos;\nflow.set('door_pos', door_pos)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":410,"y":220,"wires":[["e9f2bf08.2e8b8","32d38a11.0542d6"]]},{"id":"e9f2bf08.2e8b8","type":"trigger","z":"bf12ff9c.cd354","name":"","op1":"","op2":"","op1type":"nul","op2type":"payl","duration":"5","extend":true,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":590,"y":220,"wires":[["5c61d0ca.3d4f","b07637b0.0bef58"]]},{"id":"5c61d0ca.3d4f","type":"debug","z":"bf12ff9c.cd354","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":780,"y":220,"wires":[]},{"id":"b07637b0.0bef58","type":"mqtt out","z":"bf12ff9c.cd354","name":"","topic":"door_pos","qos":"","retain":"true","broker":"75eba16c.094f9","x":770,"y":270,"wires":[]},{"id":"5a412fd1.b77d1","type":"mqtt in","z":"bf12ff9c.cd354","name":"","topic":"door_pos","qos":"2","datatype":"auto","broker":"75eba16c.094f9","x":230,"y":100,"wires":[["25adc72b.22f948"]]},{"id":"25adc72b.22f948","type":"change","z":"bf12ff9c.cd354","name":"","rules":[{"t":"set","p":"door_pos","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":100,"wires":[[]]},{"id":"32d38a11.0542d6","type":"ui_level","z":"bf12ff9c.cd354","group":"b04b0c14.28f88","order":1,"width":0,"height":0,"name":"","label":"Garage Door","colorHi":"#0fe600","colorWarn":"#fbff00","colorNormal":"#f40101","colorOff":"#fc0303","min":0,"max":100,"segWarn":"","segHigh":"","unit":"%","layout":"sv","channelA":"","channelB":"","decimals":0,"animations":"soft","shape":2,"colorschema":"valuedriven","textoptions":"default","colorText":"#eeeeee","fontLabel":"","fontValue":"","fontSmall":"","colorFromTheme":true,"textAnimations":false,"hideValue":false,"tickmode":"off","peakmode":false,"property":"payload","peaktime":3000,"x":590,"y":270,"wires":[]},{"id":"8a1e9f1e.d0be7","type":"ui_button","z":"bf12ff9c.cd354","name":"","group":"b04b0c14.28f88","order":5,"width":0,"height":0,"passthru":false,"label":"Garage Door is CLOSED","tooltip":"","color":"","bgcolor":"","icon":"","payload":"0","payloadType":"num","topic":"","x":280,"y":350,"wires":[["825fd5c7.14f648"]]},{"id":"7842264e.08e6e8","type":"ui_button","z":"bf12ff9c.cd354","name":"","group":"b04b0c14.28f88","order":6,"width":0,"height":0,"passthru":false,"label":"Garage Door is OPEN","tooltip":"","color":"","bgcolor":"","icon":"","payload":"100","payloadType":"num","topic":"","x":270,"y":410,"wires":[["825fd5c7.14f648"]]},{"id":"825fd5c7.14f648","type":"change","z":"bf12ff9c.cd354","name":"","rules":[{"t":"set","p":"door_pos","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":380,"wires":[["32d38a11.0542d6"]]},{"id":"75eba16c.094f9","type":"mqtt-broker","name":"","broker":"","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"online","birthQos":"0","birthPayload":"BULB-1/LWT","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"offline","willQos":"0","willPayload":"BULB-1/LWT"},{"id":"b04b0c14.28f88","type":"ui_group","name":"Garage Door","tab":"5757ea10.5f6df4","order":1,"disp":false,"width":"6","collapse":false},{"id":"5757ea10.5f6df4","type":"ui_tab","name":"Garage Door","icon":"dashboard","disabled":false,"hidden":false}]

So after much thought I settled on putting a button on the esp. When the button is pushed it sends a 0 and resets the esp.
I feel I was over thinking the problem. So if there is a power failure I put the door down , push the button on the esp and life is good again.

1 Like

Simple is usually best :smiley:

1 Like

Hi gerry

I'm coming in late here, but my suggestion is to get an esp32 with a LiPo battery (refer, so it stays alive during power outages.This will even track if the door is moved manually during the power outage.
Battery backup is a standard way for industrial incremental encoders to be made into pseudo-absolute encoders.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.