ESP Relay / Sonoff default state when cycling power

Hi All,

I’ve a few ESP8266 devices that I’ve flashed with ESP Easy, which works really well. Just need some help with regard to power cycling. Would it be possible to set the relay to “on” once power has been cycled ie switching a physical light switch on without it needing to fetch the switch state from HASS?
I assume one would do this using rules on the MQTT device itself, if so how?

Thanks

If you’re interested in using different firmware, I have just added EEPROM support for relay state. If you’re using a Sonoff give it a try.

Thanks so much, I’ll implement it on the Sonoffs.
Just need to try make plan with this other relay I have - http://www.electrodragon.com/product/wifi-iot-relay-board-based-esp8266/

Any ideas?

Try this: https://github.com/arendst/Sonoff-MQTT-OTA-Arduino

It has everything you need and supports the Electrodragon. Even has OTA !

On ESP Easy you can set the boot state of the GPIO. Sonoff relay uses GPIO12(D6).

Hi all,

Thanks alot for the replies. I’ve gone with piotr’s suggestion.
I think all the firmwares have an issue though… If you power cycle off the device and back on, the relay switches on, as needed, but after a few seconds, due to the status in HASS, the device switches off. Is there any way to ignore this during power cycling? Does the retain flag in HASS compromise the state?

Thanks

Hi all,

I may have found the issue. Once I connect to the MQTT broker and restart the light switches on momentarily then off. The mosquitto.db has the device listed with an OFF flag. Is there any way to default this to ON, or am I looking int he right place?

You can make a rule I believe in the ESPeasy Sonoff.

something like:

On System#Boot do
gpio,12,1 // like this switch gpio 12 on it will be sended to HA also
endon

also electrodragon behave goofy if you have “send boot state” enabled and retain state in mqtt.

I suggest using the espeasy rules to set state on boot, rules are incredibly flexible and can do many(!!) things when applied creatively.

If using the Electrodragon with ESPEasy and Home Assistant via MQTT, the “Send Boot state” must be unchecked otherwise the button will send its state (high) on boot and toggle back based on the “Retained flag” on the MQTT broker.

More help with Sonoff here Using ESPEasy with Home Assistant via MQTT

@xbmcnut i have made a esp8266 socket with the help of your blog. if i turn on the socket from HA and reboot HA it doesn’t remember the state of the switch is there any way i can fix this please help me.?

What version of ESP Easy are you using. Do you have the global “retain flag” option set in ESP Easy and does your HA yaml code also have the MQTT retain option added?

im on version esp easy 120. i can not find retain flag option

this is how i set it in HA

 - platform: mqtt
    name: "Socket"
    command_topic: "socket/gpio/2"
    state_topic: "socket/relay/Switch"
    payload_on: "0"
    payload_off: "1"
    qos: 1
    retain: true

Im on version 147 RC8 here is the link:
http://www.letscontrolit.com/downloads/ESPEasy_R147_RC8.zip

@oriolism tried 147 i tried configesp it says incorrect password.

1 Like

If you upgrade with the OTA option on Esp Easy, it will keep your password otherwise the default password is configesp.

Yep, as per the blog, the retain flag option was added in version R121 and above under Tools > Advanced. Without that ticked, the connecting client (HA, phone app etc.), won’t know that last know state so will report the device as off. You can simply upload R147 using the web GUI under Tools > Firmware > Upload.

1 Like

i re flashed 147 and it worked.thanks guys i upgraded now status are working after reboot…next want to make with a 4 channel relay with buttons to control it :slight_smile:

@xbmcnut @oriolism hello is it possible to do a long press in esp easy rule ? im making a switch board with 4 channel relay and 4 switch but i want one of the button to act as master switch when long pressed everything ON long pressed again everything OFF is it possible?

so far my rule is this.

on button1#button1 do
if [relay1#relay1] =0
GPIO,16,1
else
GPIO,16,0
endif
endon

on button2#button2 do
if [relay2#relay2] =0
GPIO,14,1
else
GPIO,14,0
endif
endon

on button3#button3 do
if [relay3#relay3] =0
GPIO,13,1
else
GPIO,13,0
endif
endon

on button4#button4 do
if [relay4#relay4] =0
GPIO,2,1
else
GPIO,2,0
endif
endon

Thank you

Take a look here https://www.letscontrolit.com/forum/viewtopic.php?t=2637 although I see you’ve asked your question there. Did the info in that thread not assist? I’m not a rules guru so the folks at the ESPEasy forum might be a better bet?

1 Like