Enhancing Sonoff TH16 Functionality and Domoticz Integration

In my previous blogpost, the Sonoff worked, but was lacking a manual override. The switch could only be triggered by Domoticz. Since it also has a physical push button (connected to GPIO0 (D3)), it can be switched by hand. All that needs to be done is:

  1. Create a new switch device in the Sonoff
  2. Enable 'Rules' in the Tools / advanced settings
  3. Create a rule
  4. Change the On/Off commands in the switch parameters in Domoticz

Create a new switch

The new switch name is used in the rules, so pick something obvious. In my case I used 'PushButton'. Since the button should also update the switch-state in Domoticz, the orignal IDX of the existing switch is used. The rest of the settings are shown in the following image.

Enable rules in the Sonoff Device

The ESP Easy software hides some advanced stuff that can be enabled under the Tools -> Advanced setting. Enable the rules option here.

Create a rule

Using rules enables you to automate certain things within the Sonoff itself. Like what to do when a button is pressed. Since there's a dependency with a remote trigger (through Domoticz) the script / rule needs to incorporate that. Basically, the script is gonna be triggered though Domoticz, and the script makes sure that everything falls in place.

The actual script (copy&past-friendly) is listed below.

on switch_on do
gpio 12,1
gpio 13,0
inputswitchstate 0,1

on switch_off do
gpio 12,0
gpio 13,1
inputswitchstate 0,0

on PushButton#Switch do
if [PushButton#Switch]=1

Note that the device name (PushButton) is referenced in the code above. The 'on' lines are 'events' that are used in the Domoticz part of the configuration. These events (switch_on and switch_off) are gonna be used.

Changing The Domoticz Switch Configuration

All that needs to change in the (virtual) Switch configuration is the value of the On/Off commands.


From this point on, the Sonoff TH16 can be manually operated and the state is reflected in Domoticz. Handy for when you don't have access to the Domoticz interface.

Posted on January 1, 2018 and filed under Hardware, Programming, Raspberry Pi, Tips'n Tricks, Domotica.