Help needed! Xiaomi button click > Call IFTTT webhook > Turn Sonoff ON

i want to find a way to turn my sonoff switch on / off with xiaomi’s wireless switch.

i have installed xiaomi’s binding on my openhab, i have found everything i need but how could i make a rule from openhab or even from IFTTT where…

when the button is long pressed, IFTTT captures that (or gets notified) and then call through a webhook to turn sonoff on or off.

the only reason i want that is because Openhab cannot bind sonoff switches (unless you flash a custom firmware from what i know, which is something that i do not wish to do)

Then you won’t be able to use your sonoff switch with OH

yes i know this is why i said that i want to reach my sonoff through IFTTT somehow.
because my OH connects with my xiaomi ecosystem.

i already have some automations with OH and IFTTT like… when my xiaomi door sensor opens… send a notification to my phone through IFTTT . this is an automation made with IFTTT.

the problem is that IFTTT does not recognize switches from OH. if OH could share that event… it would solve my issue

Create a virtual switch that is turned on and off via a rule that reads the button state and use that virtual switch to trigger the IFTTT applet.

thank you. i have thought of that but i wasn’t sure if it is possible to do.
even though i am using openhab for months now, i have only been able to create rules with the rule engine which has UI. i have absolutely no idea how to do what you said. is there anyway that you can guide me?

i looked for this… Design Pattern: Unbound Item (aka Virtual Item)
but it was not helpful

thank you

I do everything through text files but I will try and walk you through the way to do it via Paper UI if hat’s what you’re used to.

I am assuming that you have added the Xiaomi gateway and button to Openhab already.

First create a new item, give it a name and a label e.g. SONOFF_IFTTT, or whatever you want to call it, with the type set to ‘Switch’

Go to the rules engine and create a new rule and give it a name.

Hit + next to When and select ‘a trigger channel fires’ and click Next. Under Channel select your button. In event type “LONG_PRESSED” without the speech marks, this is the trigger that comes from the Xiaomi button. FYI you also get “SHORT_PRESSED” “DOUBLE_PRESSED” and “LONG_RELEASED”.

Click the + next to then and select ‘send a command’ - next. Under Item select the switch you made at the begining. Under command select ON.

Do the above again but this time chose OFF as the command.

Save the rule.

So basically you have configured Openhab to toggle the item SONOFF_IFTTT when the button has been long pressed.

How are connecting Openhap to IFTTT? Assuming it is through the cloud you would need to add this new virtual switch into the config to be sent to the cloud. The you should be able to create an IFTTT applet that is triggered by the Openhab item state chage.

Hope this helps.

the problem is that i don’t know how to create a new virtual item through paper ui. If i go to Inbox and add a new thing it asks me to add a device based on the bindings i have installed. there is no option to add a virtual item. maybe i have to install a binding for that?

hm i think that i can just create a virtual xiaomi door sensor and do the job. i will have to try it

In PaperUI go to configuration - items from the menu on the left. You will then see a list of your items. There is a + button at the top of the page that will allow to to add a new switch.

Configuration has Things. In things i press + and then it gives me a list of all my bindings.
Then i need to choose a binding in order to add a new thing.

i tried to add a xiaomi socket manually as a virtual device, but i can’t turn it on or off using rules.
or make it work manually. i click on power and it looks like it turns on but if i refresh… then it’s off again

You need to be in Configuration > Items. Not Configuration > Things

Sorry, just realised you must have “Simple Linking” enabled so you don’t see an Items option in the menu.

Go to Configuration > System and turn off “Simple Mode” in the “Item Linking” section then click Save. This will give you the Items menu where you can create a new switch. You can then turn the “Simple Mode” back on if you want.

Turning Simple Mode off gives you full control over what items Openhab has and what Thing channels they are attached to. Having it off also allows you to create Items that are not linked to a certain Thing.

you were right! thank you. i created the virtual switch now through items. simple mode was indeed on.

i created a rule, i can’t see the virtual switch on Control page, but i guess this is normal?

i saw that i can expose the virtual switch to openhab cloud which means that ifttt will be able to see it too.

i will try it and let you know if it works.

i am happy to report that my issue is resolved. thanks to your guidance i managed to control my Sonoff device through openhab without the need of flashing! yay!

now i have another question as a bonus.
i want to create a rule through rule engine on PaperUI where it executes a command after 30 minutes.

more specific… right now i have the following…

  1. if switch is pressed
    2a)virtual switch turn on

what i want is…
2b) wait 30 minutes (this is where i am stuck)
2c) virtual switch turn off (this is where i want to end up)

After having a look I’m not sure that is possible using the rule engine. I’m guessing that functionality hasn’t been added yet.

Unless somebody else disagrees I believe the only way for you to be able to have a rule that does what you want would to to have it in a text file. Depending on what hardware your openhab is running on there will be a folder called rules. In here you would create a file called something like my.rules, it can be called whatever you want as long as it ends with .rules. Open this with a Text editor like Notepad and type in the following changing the item name to match what you setup through Paper UI.

var Timer Auto_Off = null   //Timer to control when to turn switch off

rule "Mijia & Aqara Wireless Switch"
    Channel "mihome:sensor_switch:<GwID>:<ID>:button" triggered //Change <GwID> to Gateway ID and <ID> to button ID, should be shown under Thing name in Paper UI
    var actionName = receivedEvent.getEvent()
    switch(actionName) {
        case "SHORT_PRESSED": {
            //Do something when button pressed
            Virtual1.sendCommand(ON)    //Turn on Item "Virtual1" change "Virtual1" to match item name you setup
            Auto_Off = createTimer(now.plusMinutes(30)) [   //Starts a timer for 30 minutes then runs coke within square brackets
                Virtual1.sendCommand(OFF)   //Turn off item after 30 minuter timer finishes
        case "DOUBLE_PRESSED": {
            //Do something when button double pressed
        case "LONG_PRESSED": {
            //Do Something when button long pressed
        case "LONG_RELEASED": {
            //Do something when button long press released

Or as a complete alternative you can setup a timer through IFTTT using Stringify. You can download the app and create a “Flow” that is triggered by IFTTT waits 30 minutes then triggers an IFTTT applet. In IFTTT you would have two applets that trigger from the Openhab item, one turns the sonoff on and one starts the stringify flow. You would then make an applet that would trigger from the Stringify flow ending that would turn off your Sonoff device.

Yes, but now your activities are passing through a Chinese cloud server.
I don’t know how much you value your privacy. But I would not do that personally.

Look at Experimental Next-Gen Rules Engine Documentation 1 of : Introduction (all five pages). You need to create a Script Action and create a Timer from inside that Action to then send the command.

you mean Sonoff’s server or IFTTT?

at this moment i prefer to make my life easy and let someone from China know when i turn on or off my heater.
if privacy is your issue with China, we should also consider America and everyone else.

trust me, i am more concerned about the Google Home that i have which is a live microphone in my room than Sonoff. why do i keep it? because that thing actually makes my life easier on certain things.

@TeDeV_Solutions, thanks to @rlkoshak post I now think I know what you need to do.

Create a new rule that is triggered when your virtual item receives the command ON. Click the + next to “then” to add an action and select “Execute a given script” click next. Give it a name and description so you can remember what it does on the future and type the following into the “Script” box

// Turn off Triggering item after 30 minute timmer
var triggeringItem = ir.getItem(event.itemName);
var ScriptExecution = Java.type("org.eclipse.smarthome.model.script.actions.ScriptExecution");
var DateTime = Java.type("org.joda.time.DateTime");
var runme1 = function(){ events.sendCommand(triggeringItem, OFF); }
var timer1 = ScriptExecution.createTimer(, runme1);

What the above script does is it works out when item caused the rule to run (Your virtual item) and turns it off after a timer of 30 minutes completes.

Don’t forget to add another applet on IFTTT to tuen off the Sonoff device when your virtual item turns off.

thank you for your help man