How to hold last pin state on power failure ? Easy ESP OH2?


i am operating relay remotely, but there are many times power fails and the GPIO’s go to their default states. I want it to get to last state it was before power failure.

What should i do ?

To send the last state after (re)connection is a mqtt basic funktion.

Never worked for me. My Lights keep off once the power is gone and again back online. If there’s any configuration please do let me know how can i confirm this works ?

I dont have a configuration example, but a lot will depend on how you configured your mqtt traffic.
Title suggests you have an ESP getting mqtt commands and operating the relay.

Suppose the power fails and your micro makes a restart when it comes back on again and the first thing it does is to send its state to openhab… then ofcourse that Last State is gone.

Suppose everything is in full working order and Openhab has just send the state ‘ON’ to your peripheral relay. Even if your relay gets a powerfailure… Openhab will keep that state (unless you defined an expire).
Now if you have openhab send regular updates to the periphery, your relays will be set to the proper state again.

Another possibility is to have Openhab keep track if a specific object has gone off line and when it comes back onliine, to send the last state.

i am not sure if there are other mqtt related ways, but my first example is what I do… unless I want the peripheral object to be the autonomous master and just use openhab as an interface.

A completely non MQTT related way is to have yr state stored in non volatile memory (if you are using a microcontroller) although I think the ESP has no powerdown surviving memory, and pull it from there on start-up