OLD Example dosen’t work on 3.3.0 firmware.
New one here https://github.com/JAMESBOWLER/SONOFF_DIY
Sonoff DIY Example
To setup I created rules to control. Openhabian on RPI 3+
Product Page Link
Documentation/Tools Github
Steps
- Wire power and Install header pin
- Update Firmware using eWelink APP as it won’t work with shipped firmware 3.0.0
- DIY will connect to wifi ssid:sonoffDiy password:20170618sn Youtube Demo video
I created wifi with spare router I had lying around and mDNS is required - Use DIY mode tool to change SSID to same as pi found here Github
- Setup power on state
- Set up DHCP reservation using router (192.168.1.50)
- Create Items Sitemap and Rules
You can flash 3rd party firmware using this tool to void warranty.
I use the openhab config files edited through VScode
Item
Switch sonoff_one "Lounge Light" <switch> (gLounge) [ "Lighting" ]
Sitemap
sitemap sonoff label="sonoff" {
Frame label="Lounge" {
Switch item=sonoff_one icon="light"
}
}
Rules
rule "Sonoff control Rule"
when
Item sonoff_one received update
then
try {
var CommandState = sonoff_one.state.toString.toLowerCase // Change Sonoff_one to the item in your .items file e.g. Switch sonoff_one "Lounge Light" <switch> (gLounge) [ "Switchable" ]
val String Ip = '192.168.1.50' // Ip of Sonoff
val String Deviceid = '100086f3e5' // Device ID - Works without it
// Shouldn't need to change anything below
var CommandURL = 'http://' + Ip +':8081/zeroconf/switch'
var String CommandData = '{ "sequence": "1560835774", "deviceid": "' + Deviceid + '", "encrypt": false, "iv": "1234567890123456", "selfApikey": "123", "data": "{\\"switch\\": \\"' + CommandState + '\\"}"}'
var String UpdateResult
var Number Attempts = 0
var CommandExec = 'curl@@-sSH@@"Accept: application/json"@@-H@@"Content-Type: application/json"@@-X@@POST@@-d@@' + CommandData + '@@' + CommandURL
do {
Attempts += 1
UpdateResult = executeCommandLine(CommandExec, 2000)
if (UpdateResult.contains('"error":0')) { logInfo("Sonoff ID:"+ Deviceid +" Control", "IP: " + Ip + " Turned: " + CommandState) } // Success
} while (!UpdateResult.contains('"error":0') && Attempts < 5) // Attempt to send 5 times
if (Attempts == 5) {
logError("Sonoff write", "5 failures updating Sonoff")
}
if (UpdateResult.contains('"error":400')) {
logError("Sonoff ID:"+ Deviceid +" Error", "The operation failed and the request was formatted incorrectly. The request body is not a valid JSON format")
}
if (UpdateResult.contains('"error":401')) {
logError("Sonoff Error", "The operation failed and the request was unauthorized. Device information encryption is enabled on the device, but the request is not encrypted")
}
if (UpdateResult.contains('"error":404')) {
logError("Sonoff Error", "The operation failed and the device does not exist. The device does not support the requested deviceid")
}
if (UpdateResult.contains('"error":422')) {
logError("Sonoff Error", "The operation failed and the request parameters are invalid. For example, the device does not support setting specific device information")
}
}
catch(Throwable t) {
logError("Sonoff write", "Error was caught: {}", t)
}
end
Google intergration
- I have setup a https://myopenhab.org/ account
- Installed cloud connecter installed and verified it is online.
- https://www.openhab.org/addons/integrations/openhabcloud/
- Setup Google assistant https://www.openhab.org/docs/ecosystem/google-assistant/
You may need to tell google “Hey Google sync all my devices”
Now in the google routine for bedtime you can add the light to the sequence.
Layout of Data
Cons
- If you switch it too fast using sitemap it light may be out of sync. I working on making it more reliable.