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

for all this you have Home Assistant …

1 Like

Indeed an active server. pi3 is a very low cost purchase and low cost running cost device where you can run Home Assistant. Be sure to put a certificate when you want to access it from outside your own network.

Using Tasker you can also make shortcuts on your home screen of your phone to for instance quickly turn on/off your tv or other things.

default firmware does not send to Chinese Server

  • or did I miss where China bought Amazon AWS?

Guaranteed New in Box (and not returns) from Itead directly
https://www.itead.cc/smart-home.html

  • plenty bought from elsewhere are registered by another user.
    for the $0.20 that maybe saved, why lose peace of mind.

One of the reasons I wrote this firmware is because I don’t want Sonoff switches accessing the cloud anywhere in the world other than my own internal MQTT broker. That’s also the reason why I have HA running. I don’t want to have to rely on the Cloud to use my Home Automation system. I really cannot see the benefits of doing so but I can think of a couple of reasons why it shouldn’t :smiley:

1 Like

Currently working on this. So far it’s working with -

Sound Level
Light Level
Dust Level
Temperature
Humidity

I’m in the process of adding Room Presence (Microwave) and Notification (via LEDS) as well. All on MQTT and under $30.

I’ll keep you posted.

5 Likes

Hi @KmanOz

First of all thanks a lot for your great share. This is awesome.

I just started with my first sonOff today which is a SonOFF POW 16A version. Re-flashing the default code with yours and wiring those up were pretty much straight forward. However, I am facing an weird issue after doing all this. The problem is I can’t switch on/off the SonOff via MQTT publish message. The press button works fine. And mosquitto_sub is also registering the messages (stat and power ) message fine.

Here is a sample of the messages…

....
home/sonoff/powerstrip1/stat on
home/sonoff/powerstrip1/power {"Power": 0.00, "Voltage": 251.64}
home/sonoff/powerstrip1/power {"Power": 0.86, "Voltage": 251.29}
....

For your information, I used the “ESPsonoff_POW-v1.01.ino” code. Do you have any suggestion for me?

home/sonoff/powerstrip1/stat is the status of the switch. You do not publish on and off to the /stat topic to control the switch. That is where the switch publishes it’s status so HA knows where it’s at. You need to publish on & off to home/sonoff/powerstrip1.

1 Like

Thanks a lot for the quick reply. Yeah I was just writing that I understood the issue as I was looking into your code. It works flawlessly… Thanks again for your great work.

1 Like

Hey Guys,

Just got a Sonoff POW 16A and flashed the firmware, How do you go about debugging with these given we cant run live power and the serial at the same time?

Thanks Rob.

What’s the issue?

Hi KmanOz,

When it is on the Serial Interface, Everything works perfectly with stock firmware (ESPsonoff_POW-v1.01)
It connects to WiFi, I can toggle the switch both in HomeAssistant and Via the Button and with debug = true
I can see everything is correct on the serial monitor on Arduino IDE.

When I plug it into Mains, It doesn’t connect to wifi the blue LED just blinks, then after some time it goes stable (still not connected - cant ping) then the led goes out. The Push button doesn’t function at all. I’ve tried in two different locations (different routers)

Thanks Rob.

If it’s working with DC from your computer FTDI then I don’t think it’s going to be a coding issue. Do you have an Oscilloscope? Can you check voltage rails and how noisy they are? Remember it’s a mini switchmode supply with a capacitor dropper. You could have a dodgy capacitor in the power supply. I haven’t bothered writing any Telnet debug routines because basically they just work.

I have been testing a SonoffSC environmental room sensor. Notice a few posts above this one that the noise level in the room is supposedly around 36%. The room is dead quiet but the little switchmode power supply I’m using is dirty and noisy and it shows up in the readings. I had another supply here that was even worse at around 50%. Sometimes that switching noise or AC (50Hz) noise on the voltage rails can swamp the ESP chip and it stops it from functioning correctly especially if it has a suspect capacitor etc

Hi KmanOz,

I tend to agree with you, I’ll take it to the workbench at work and do some further investigation. Thanks for the information and suggestions

Thanks again Rob.

No worries Rob. Good luck. Just be careful. Basically the whole thing is live so don’t get zapped :smiley:

I’m trying to get a Sonoff Switch to work with a newly created Home Assistant vit Hassbian 1.23

My HUE lamps work in HA, so far so well.

MQTT is enabled and appears to work.

I have added the following to my configuration.yaml:

mqtt:
  Broker: 192.168.1.155

   #configuration  Sonoff Switch
switch:
  Platform: mqtt
  Name: "living room"
  State_topic: "home / sonoff / living_room / 1 / stat"
  Command_topic: "home / sonoff / living_room / 1"
  Qos: 1
  Payload_on: "on"
  Payload_off: "off"
  Retain: true

On my Sonooff I have flashed ESPsonoff-v1.01p with the correct information.

  **** USE THIS Firmware for: Original Sonoff, Sonoff SV, Sonoff Touch, Sonoff S20 Smart Socket ****

* /

#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Ticker.h>

#define BUTTON 0 // (Do not Change for Original Sonoff, Sonoff SV, Sonoff Touch, Sonoff S20 Socket)
#define RELAY 12 // (Do not Change for Original Sonoff, Sonoff SV, Sonoff Touch, Sonoff S20 Socket)
#define LED 13 // (Do not Change for Original Sonoff, Sonoff SV, Sonoff Touch, Sonoff S20 Socket)

#define MQTT_CLIENT "Sonoff_Living_Room_v1.01p" //mqtt client_id (Must be unique for each Sonoff)
#define MQTT_SERVER "192.168.1.155" // mqtt server
#define MQTT_PORT 1883 // mqtt port
#define MQTT_TOPIC "home / sonoff / living_room / 1" //mqtt topic (Must be unique for each Sonoff)
#define MQTT_USER "haadmin" // mqtt user
#define MQTT_PASS "*************" // mqtt password

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

#define VERSION "\ n \ n ------------------ Sonoff Powerpoint v1.01p ------------------"

When I press the button on the Sonoff Switch, the relay switches on and off.

But in HA States, does not the new switch see?

How can I check that Sonoff Switchen communicates with MQTT?

There is a missing link somewhere but where?

It maybe something to do with all the spaces you have

#define MQTT_TOPIC “home / sonoff / living_room / 1”

should be…
#define MQTT_TOPIC “home/sonoff/living_room/1”

just a thought :slight_smile:

I do not know why these spaces occur, they are not in configuration.yaml, but only in my post here where the code is copied from Arduino IDE.

Here it is again without spaces, and it looks like it’s flashed.

I’m a beginner in Home Assistant.
Will a switch appear in HA States solely on the basis of the configuration.yaml content, or will it be visible only when there is communication to the device?

  ==============================================================================
  Changes in v1.01
  
    - Relay state now stored in EEPROM and will power up with last relay state
  ==============================================================================

  **** USE THIS Firmware for: Original Sonoff, Sonoff SV, Sonoff Touch, Sonoff S20 Smart Socket ****

*/

#include <EEPROM.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Ticker.h>

#define BUTTON          0                                    // (Don't Change for Original Sonoff, Sonoff SV, Sonoff Touch, Sonoff S20 Socket)
#define RELAY           12                                   // (Don't Change for Original Sonoff, Sonoff SV, Sonoff Touch, Sonoff S20 Socket)
#define LED             13                                   // (Don't Change for Original Sonoff, Sonoff SV, Sonoff Touch, Sonoff S20 Socket)

#define MQTT_CLIENT     "Sonoff_Living_Room_v1.01p"          // mqtt client_id (Must be unique for each Sonoff)
#define MQTT_SERVER     "192.168.1.155"                      // mqtt server
#define MQTT_PORT       1883                                 // mqtt port
#define MQTT_TOPIC      "home/sonoff/living_room/1"          // mqtt topic (Must be unique for each Sonoff)
#define MQTT_USER       "haadmin"                            // mqtt user
#define MQTT_PASS       "***************"                     // mqtt password

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

#define VERSION    "\n\n------------------  Sonoff Powerpoint v1.01p  ------------------"
Platform: mqtt
  Name: "living room"
  State_topic: "home / sonoff / living_room / 1 / stat"
  Command_topic: "home / sonoff / living_room / 1"
  Qos: 1
  Payload_on: "on"
  Payload_off: "off"
  Retain: true

I don’t think you can have these capitalised and no spaces either, however maybe that’s forum formatting again :frowning:

Your broker setup may have a few things missing? And no capitals :wink:

mqtt:
  broker: 192.168.1.7
  port: 1883
  client_id: home-assistant
  username: !secret mqtt_usern
  password: !secret mqtt_pw
  keepalive: 60
  protocol: 3.1.1

To check the sonoff communcates with mqtt subscribe to the topic in a terminal window (ssh)…

mosquitto_sub -u your_user_name -P your_pw -t home/sonoff/living_room/1/stat/#

Only based on configuration.yaml. It does not need to communicate to appear. If the switch flashes 4 times fast, then it has connected to your broker. That’s the point of the status LED. Just copy the configuration.yaml from the github exactly as it is. Your issue is capitalization and syntax. YAML is very picky.

1 Like