[SOLVED] Xiaomi wireless switch + lamp basic definitions

Hello world,
I am very new to OpenHAB. currently using my windows10 laptop to test it, but soon the RPi will come.

I am trying to program my button to toggle the lamp On/Off.

FirstTry.rules:

rule "Toggle Celing Lamp"
when
    ButtonTest.state changed
then
    if (BP01.state == ON)
    {
        if (LivingRoom_LightCeil.state == ON)
        {
            LivingRoom_LightCeil.sendCommand(OFF)
        }
        if (ButtonTest.state == OFF)
        {
            LivingRoom_LightCeil.sendCommand(ON)
        }
    }
end

default.items:

Switch    LivingRoom_LightCeil  "Luster"          <light>         (LivingRoom, gLight)      ["Lighting"]     {channel="miio:generic:04572C43:power"}  
Switch    ButtonTest            "Pokusny vypinac" <wallswitch>    (LivingRoom, gSwitch)                      {channel="mihome:sensor_switch:158d000201706b:button"} 

both - button and celing light are paired through paperui and in events.log i can see, that both are working.

anyway, nothing happened and the events.log is showing only this:

2018-09-22 13:44:41.531 [vent.ChannelTriggeredEvent] - mihome:sensor_switch:158d000201706b:button triggered SHORT_PRESSED

I am using latest OH for windows. What i am doing wrong?

Thank you for answers,
Michal, Slovakia

Change the above bit to:

rule "Toggle Celing Lamp" 

when

       Item ButtonTest changed
then

Should be: Item ButtonTest changed from OFF to ON

But it’s a toggle button so a press just needs the lamp to change to the state which it currently isn’t, irrespective of whether the current state is ON or OFF so the ‘from OFF to ON’ is neither needed or desirable.

In the rule, what is the item BP01?

Hi,

I use the Xiaomi Aqara Smart Switch 2 Button

in my rules I don’t refer to the item but directly to the channel

i.e. My channel is mihome:86sw2:xxxxxxxxxxxxxd:ch1

rule "Lam on when I press button"

when
        Channel "mihome:86sw2:xxxxxxxxxxxxxd:ch1" triggered
then
     lampxxx.sendCommand(ON)}
end

i have not been able to get it to work with a item
Maybe someone else has experience with linking xiaomi switches with a Item

But the above example works for me

I have 3 separate switches that operate that way without problems

First try a simple rule like above and if that is working you can always extend it

The Xiaomi binding does not support channel triggers
You need to link the channel to an item and use that item for the trigger

I use the "Xiaomi Mi Smart Home Binding" on OH 2.3

Have no trouble to use the channel trigger in a rule

I prefer to use it thru a item, but I couldn’t get it to work, this was on OH 2.1

Maybe I did something wrong or it was a bug.

sorry, mistake. it was taken from other script, now it is replaced by “ButtonTest”

rule "Toggle Ceiling Lamp"
when
    ButtonTest.state changed
then
    if (ButtonTest.state == ON)
    {
        if (LivingRoom_LightCeil.state == ON)
        {
            LivingRoom_LightCeil.sendCommand(OFF)
        }
        if (ButtonTest.state == OFF)
        {
            LivingRoom_LightCeil.sendCommand(ON)
        }
    }
end

still nothing works


It doesn’t work because you did not change

 ButtonTest.state changed

to

Item ButtonTest changed

Like this?

rule "Toggle Ceiling Lamp"
when
    Item ButtonTest changed
then
    if (ButtonTest.state == ON)
    {
        if (LivingRoom_LightCeil.state == ON)
        {
            LivingRoom_LightCeil.sendCommand(OFF)
        }
        if (ButtonTest.state == OFF)
        {
            LivingRoom_LightCeil.sendCommand(ON)
        }
    }
end

No, does not work :confused:

Your logic is flawed:

rule "Toggle Ceiling Lamp"
when
    Item ButtonTest changed
then
    if (ButtonTest.state == NULL) return; //DO NOTHING IF NULL
    if (LivingRoom_LightCeil.state == NULL) return; //DO NOTHING IF NULL
        if (LivingRoom_LightCeil.state == OFF) {
            LivingRoom_LightCeil.sendCommand(ON)
        }
        else {
            LivingRoom_LightCeil.sendCommand(OFF)
        }
end

so back to the basics


how would the simple ON command looks like?

when I switched on the Ceiling lamp manually through PaperUI, log is like this:

Item 'LivingRoom_LightCeil' received command ON
LivingRoom_LightCeil changed from OFF to ON

EDIT:
This works:

rule "toggle"
when
    Channel "mihome:sensor_switch:158d000201706b:button" triggered
then
    var actionName = receivedEvent.getEvent()
    switch(actionName) {
        case "SHORT_PRESSED": {
            LivingRoom_LightCeil.sendCommand(ON)
        }
        case "DOUBLE_PRESSED": {
            LivingRoom_LightCeil.sendCommand(OFF)
        }
    }
end

Now how to toggle SHORT_PRESS to ON and OFF?

rule "toggle"
when
    Channel "mihome:sensor_switch:158d000201706b:button" triggered
then
    var actionName = receivedEvent.getEvent()
    switch(actionName) {
        case "SHORT_PRESSED": {
            if (LivingRoom_LightCeil.state == OFF) {
                LivingRoom_LightCeil.sendCommand(ON)
            }
            else {
                LivingRoom_LightCeil.sendCommand(OFF)
            }
        }
        case "DOUBLE_PRESSED": {
            LivingRoom_LightCeil.sendCommand(OFF)
        }
    }
end
1 Like

Woooow, works, thank you very much, Vincent!!!

Please mark the thread as solved, thank you