Sonoff-HomeAssistant (Alternative firmware for Sonoff Switches for use with mqtt/HA)

So the issue for me was the Wireless / AP I have now added in the below lines giving the unit a static ip and uploaded the code… have rebooted about 20 times in different locations around the house and been successful every time. not sure if this is clean code, by all means correct me :slight_smile:

#define WIFI_SSID       "****************"                           // wifi ssid
#define WIFI_PASS       "****************"                          // wifi password

**IPAddress ip(192, 168, *, *);**
**IPAddress gateway(192, 168, *, *);**
**IPAddress subnet(255, 255, 255, *);**

btn_timer.attach(0.05, button);
mqttClient.set_callback(callback);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
**WiFi.config(ip, gateway, subnet);**
ArduinoOTA.onStart([]() {

Nice One!

i will try that, can you just lock the IP for the Sonoff on the Modem? does that work?

Cheers Rob.

Hey Guys,

So i’ve set my IP’s on the Sonoff to static and will test that out see how I go.

The other thing I have noticed is if I reboot my HA and the Sonoff is ON, When HA all boots up and MQTT reconnects
I do not see the correct State of the Switch.

The only way I have found to workaround this is to add this code to the timedTasks()

    if(digitalRead(RELAY) == LOW)  {
   mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "off").set_retain().set_qos(1));
    Serial.println("Relay is OFF, Letting HA Know");
  } else {
   mqttClient.publish(MQTT::Publish(MQTT_TOPIC"/stat", "on").set_retain().set_qos(1));
    Serial.println("Relay is ON, Letting HA Know");
  }

It’s reporting back correctly now, seems strange that no one else has had this issue?

Cheers Rob.

2 Likes

FYI – the S20 SmartSocket US plug is back in stock (just ordered 5 :slight_smile: )

https://www.itead.cc/smart-socket-us.html

Hey Guys,

Another small quirk with the firmware for those that use the physical button on the sonoff.

I have a couple hooked up to my bedside lamps and I flick them on thru HA web interface. When I go to bed I press the physical button to turn them off. All good I thought until you loose wifi or if the MTQQ broker crashes then then after a reboot and reconnection to wifi and the broker then it pushes the last retained message which in my case is ON.

So the state is saved in the EEPROM but if you have “retain: true” which is the default on your configuration then it pushes that.

For the sonoffs that I don’t use the physical button I have no issues.

Cheers Rob.

Yeah my documentation isn’t clear. Either use the retain: true or set the HA configs to QOS 0.

2 Likes

That is great, thanks for the confirmation.

Mine are working great now, no need to change anything else.

Thanks for sharing all your hard work.

Rob.

Just finished off the code for the Sonoff 4CH. Been busy with my 3D printer and got off track. Should have it up over the weekend sometime.

Question: Can I push the same new firmware to this Sonoff https://www.aliexpress.com/item/90-250v-3500w-Sonoff-Pow-WiFi-Switch-With-Power-Consumption-Measurement/32780887076.html?spm=2114.01010208.3.1.lVNk8c and can I then also see the measured power consumption in Hass?

Yes I have support for that in Github already. Don’t buy from Aliexpress, buy direct from ITEAD and it’s cheaper. https://www.itead.cc/smart-home.html

You can get it for even cheaper on aliexpress https://www.aliexpress.com/item/WiFi-Wireless-Phone-APP-Remote-Control-Temperature-Humidity-Control/32795133626.html
edit: typical, they went and boosted the price… here’s a new link for 10$ https://www.aliexpress.com/item/16A-Smart-Home-WiFi-Smart-Remote-Control-Temperature-amp-Humidity-Thermostat-Module-For-Sonoff-Pow-NEW/32793542691.html

Just wanted to say thank you for the info in this thread. This was very simple to flash the Sonoffs, and they are working 100% perfectly with HA. I am a novice with the soldering iron but I got 6 of these up and running very quickly. An extremely cheap and solid solution!

That’s great and quite frankly what I set out to achieve when I first posted. It’s good to know that novices can pick this up fairly quickly and get a cheap working solution up and running fast.

1 Like

As I have a sperate Access points to the firewall it seems to point at an issue with the AP’s. i am going to be adding some more sonoff today so will try adding it at the fw,

Cheers

Rich

Thanks for the new link. ordered 3 :).

These are pretty neat. I’ve been messing mostly with the S20 outlet and the Sonoff Basic’s using Tasmota. I have them tied in fairly seamlessly with the rest of my Z-wave stuff, they seem to react as fast or faster.

There’s a couple US vendors on eBay that will sell the S20’s preflashed with a firmware you want for under $20, which I would have done before if I hadn’t drained several hours learning to flash them (first I reversed the RX/TX pin, then I kept forgetting to put them into flashing/programming mode).

will this work

yup it can be flashed. I purchased one and flashed Tasmota on it. The issue with the Tough is the design is meant for EU boxes and horizontal mounting. It does way US and does have mounting holes that line up.

The back won’t fit a single gang box,the set screw are really close the the box. Maybe a plastic box would be wider.

Would it be possible to dim with this switch? Does it detect longpress?

I don’t know, probably not.

Interested too in finding one