ITEAD Sonoff switches and sockets - cheap ESP8266 Wifi+MQTT hardware

@AndrewZ I’ve enhanced your rule a little bit so I can upgrade all modules at once. That is indeed a great addition! Thank you :wink: I’ll leave it to you to add it to the wiki. :thumbsup:

val String filename = "sonoff.rules"
val sonoff_device_ids = newArrayList(
    "sonoff-CBF6BE",
    "sonoff-96504C",
    ...
)

rule "Sonoff Maintenance"
when
    Item Sonoff_Action received command
then 
    logInfo(filename, "Sonoff Maintenance on all devices: " + receivedCommand)
    for (String device_id : sonoff_device_ids) {
        switch (receivedCommand) {
            case "restart" :
                publish("broker", "cmnd/" + device_id + "/restart", "1") 
            case "queryFW" :
                publish("broker", "cmnd/" + device_id + "/status", "2")
            case "upgrade" : {
                publish("broker", "cmnd/" + device_id + "/otaurl", "http://sonoff.maddox.co.uk/tasmota/sonoff.ino.bin")
                publish("broker", "cmnd/" + device_id + "/upgrade", "1")
            }
        }
    }
    Sonoff_Action.postUpdate(NULL)
end

Hehe, now all my modules are in perfect TELEPERIOD sync :smile:

2 Likes

Great improvement, please add it to wiki!

In fact I have 2 more items to receive and display some responses:

String	UpgradeStatus1 "Sonoff #1 upgrade progress [%s]" { mqtt="<[broker:stat/sonoff1/UPGRADE:state:default]" }
String	Sonoff1info "Sonoff #1 status message [%s]" { mqtt="<[broker:tele/sonoff1/INFO1:state:JSONPATH($.*)], <[broker:stat/sonoff1/STATUS2:state:JSONPATH($.*)], <[broker:stat/sonoff1/RESULT:state:JSONPATH($.*)]" }

Not ideal and there is probably room for improvement :wink:

2 Likes

@AndrewZ I’ve updated the wiki article

Looking at the results again, why did we / I decide to go with the ArrayList? We could just have used the groups topic “sonoffs”! :smiley: The list has the benefit, that a user could just as well only address one or a few modules. You think I should change it? :wink:

Exactly - array is more visual and flexible, we can simply comment/uncomment necessary modules in a rule.
I suppose you can just add a comment in wiki saying that this approach is shown as an alternative to ‘native’ grouptopic based scenario.

So, thanks for this excellent entry to the Sonoff Switches. I bought some Sonoff POWs exactly to monitor washing machine and dish washer states.
I have already flashed the arendst’s Sonoff-Tasmota firmware, got it up and running and it’s connected to the mosquitto-broker I installed on my OH2 openHABian.

So far, so good, but now I’m a bit lost on how to proceed next. How do I tell the POW to send power consumption, states and so on and how do I configure items in OH2 on this? I didn’t find some examples on this - or am I just blind! :wink:

Generic approach: use your mqtt client of choice to subscribe to all the topics; see what is coming from the device you working with; add an item in OH with mqtt binding, use the topic discovered earlier in the binding configuration.
You can use the RSSI example published by @ThomDietrich in wiki as a reference.

Hello!

I have a question regarding a hardware modification of the Sonoff Touch. Does GPIO0 has to be connected to the ground all the time during flash process, or just until it enters a programming mode? And does connecting it to the ground means connecting that pin (GPIO0) to the earlier soldered pin header GND pin, or some other ground source?

Best regards,
Davor

Hi … a newbie here. Not sure whether the topic has been covered elsewhere. I have a Sonoff device with RF. Will the flashing impact the RF functionality?

Thanks
Nil

Hi Andrew,
thanks for the help. As I’m new to the whole MQTT-stuff I’m a bit lost at how this will work and how it works within OH2. I can toggle my Sonoff Pow, but I don’t get the other information:

So, what I did was basically:

  1. flashed my Sonoff Pow:
  2. installed mosquitto as MQTT-broker on my OH2-Pi
  3. activated mqtt-binding and jsonpath in addons.cfg (binding=mqtt1, ..., transformation=jsonpath, ...)
  4. configured the mqtt in OH2:
#
# Define your MQTT broker connections here for use in the MQTT Binding or MQTT
# Persistence bundles. Replace <broker> with an ID you choose.
#

# URL to the MQTT broker, e.g. tcp://localhost:1883 or ssl://localhost:8883
#<broker>.url=tcp://<host>:1883
mqttoh2.url=tcp://localhost:1883

# Optional. Client id (max 23 chars) to use when connecting to the broker.
# If not provided a default one is generated.
mqttoh2.clientId=OH2MQTT

# Optional. User id to authenticate with the broker.
mqttoh2.user=m2m

# Optional. Password to authenticate with the broker.
mqttoh2.pwd=password
  1. configured the Sonoff Pow to use the mosquitto Broker
  2. defined the items as in the Sonoff-Tasmoto Wiki:
Switch Sensoren_Status_SonoffDiWa "Spülmaschine Status" <light> (SensorenLicht)   		{ mqtt=">[mqttoh2:cmnd/sonoff_DiWa/power:command:*:default],<[mqttoh2:stat/sonoff_DiWa/POWER:state:default]" }
Number Sensoren_Leist_SonoffDiWa "Spülmaschine Verbrauch [%.1f W]" (SensorenLicht)   	{ mqtt="<[mqttoh2:tele/sonoff_DiWa/ENERGY:state:JSONPATH($.Power)]" }

mosquitto.log

1486464107: New connection from 192.168.xx.xx on port 1883.
1486464107: New client connected from 192.168.xx.xx as OH2MQTT (c1, k15, u'm2m').
1486464140: Saving in-memory database to /var/lib/mosquitto/mosquitto.db.
...

openhab.log

2017-02-07 13:15:13.566 [INFO ] [t.mqtt.internal.MqttBrokerConnection] - Starting MQTT broker connection 'mqttoh2'
...
2017-02-07 13:20:41.229 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'sonoff.items'
2017-02-07 13:20:41.260 [WARN ] [.core.transform.TransformationHelper] - Cannot get service reference for transformation service of type JSONPATH
...

events.log

2017-02-07 13:23:44.652 [ItemCommandEvent          ] - Item 'Sensoren_Status_SonoffDiWa' received command ON
2017-02-07 13:23:44.657 [ItemStateChangedEvent     ] - Sensoren_Status_SonoffDiWa changed from NULL to ON

The switch toggles the Sonoff, but I don’t get updates on the power-consumption. Did I miss something?

Thanks!

You’re on the right way.
Make sure your Sonoff device is ether configured to send periodic updates or you’re asking it to report the data - see wiki.
I suggest to forget about OH for a moment and check what is reported by using the mqtt client:

mosquitto_sub  -v -t 'tele/#' -t 'stat/#'

Hopefully you will see the json then you will just need to parse it properly.

2 Likes

Ah! Now the fog is going to disappear slowly! :wink: Thanks!

In my user_config.h I found the line

// -- MQTT - Telemetry ----------------------------
#define TELE_PERIOD            300               // [TelePeriod] Telemetry (0 = disable, 10 - 3600 seconds)

after a bit of wiki-reading (GitHub - arendst/Tasmota: Alternative firmware for ESP8266 and ESP32 based devices with easy configuration using webUI, OTA updates, automation using timers or rules, expandability and entirely local control over MQTT, HTTP, Serial or KNX. Full documentation at), I found this

TelePeriod      |         | Show current telemetry period in seconds
TelePeriod      | 0 | off | Disable telemetry messages
TelePeriod      | 1       | Reset telemetry period to ```user_config.h``` value
TelePeriod      | <secs>  | Set telemetry period between 2 and 3600 seconds

So I just figured, I could type in the “console” of the web-Frontend of the Sonoff Pow: TelePeriod 20, and it worked:

Now it works! (I figure, it worked also before in 5min. interval, but as I didn’t had anything connected, the power stayed at 0! ;))

Thanks!

2 Likes

Great you figured everything out! Also @binderth great work in documenting your solution progress. Others might benefit from that.

From what I’m reading the main insights you got are:

  1. Always use an MQTT client to get clarity about what an MQTT device is doing
  2. Stay calm :slight_smile:

Would you please check the first post here and the wiki articles here and here for missing information? We can probably add it easily now :wink:

@AndrewZ Thanks again. I’ll just add “sonoffs” to the ArrayList.

Yes, I kinda hate it, if somebody just tells “everything finde now” :wink:
So I just document my progress (so I can easily find it later myself also!)

If everything works fine, I’ll check for potential additional information.

1 Like

@ThomDietrich what you can do is to put in the comment something like this:

//… add your grouptopic or individual modules here

@davorf I’ve added the answer to your question in the wiki: https://github.com/arendst/Sonoff-Tasmota/wiki/Hardware-Preparation

@NilGhosh all you need to know can be found here: https://github.com/arendst/Sonoff-Tasmota/wiki/Hardware-Preparation#sonoff-rf

@AndrewZ too late :smiley:

To save you some soldering, this worked for me! :wink:

:thumbsup: also check out ITEAD Sonoff switches and sockets - cheap ESP8266 Wifi+MQTT hardware :smile:

As I now flashed my second Sonoff Pow, I now have some strange behaviour: the both of them no play Ping-Pong:

mosquitto_sub -v -t 'tele/#' -t 'stat/#' gives me this:

tele/sonoff_DiWa/STATE {"Time":"2017-02-07T20:13:42", "Uptime":1, "POWER":"ON", "Wifi":{"AP":1, "SSID":"PL.12", "RSSI":56}}
tele/sonoff_DiWa/ENERGY {"Time":"2017-02-07T20:13:42", "Yesterday":"0.000", "Today":"0.002", "Period":0, "Power":0, "Factor":"0.00", "Voltage":247, "Current":"0.000"}
tele/sonoff_WaMa/LWT Online
tele/sonoff_DiWa/LWT Online
tele/sonoff_WaMa/LWT Online
tele/sonoff_DiWa/LWT Online
tele/sonoff_WaMa/LWT Online
tele/sonoff_DiWa/LWT Online
tele/sonoff_WaMa/LWT Online
tele/sonoff_WaMa/STATE {"Time":"2017-02-07T20:13:52", "Uptime":0, "POWER":"OFF", "Wifi":{"AP":1, "SSID":"PL.12", "RSSI":68}}
tele/sonoff_WaMa/ENERGY {"Time":"2017-02-07T20:13:52", "Yesterday":"0.000", "Today":"0.000", "Period":0, "Power":0, "Factor":"0.00", "Voltage":0, "Current":"0.000"}
tele/sonoff_DiWa/LWT Online
tele/sonoff_WaMa/LWT Online
tele/sonoff_DiWa/LWT Online
...

is there a way to tell them to stop this? :wink:

Didn’t see that before…

hmm. I’ll post a issue on github on this.