Problem with my rule?

Hi all,

I was wondering if you’d be able to help? I’m currently using zigbee2mqtt and have several devices which work really well. Most recently I’ve purchased an Aqara smart switch and can see the status updates within OH.

To the issue at hand, the smart switch has 3 options; single click, double click and hold. I’ve written a rule on single click which works but takes up to 2 seconds for it to action once the button is pressed.Agree its not a massive delay, but not sure if you’d be able to review my rule?

rule "Room Smart Switch"


  Item Room_Smart_Switch_Click received update


val vRoomSmartSwitchLog = "Room Smart Switch"

if (Room_Smart_Switch_Click.state == "single") {

  if (Room_Main_Light.state == OFF) {

    Room_Main_Light.sendCommand(ON) // Toggle on

    logInfo(vRoomSmartSwitchLog, vRoomSmartSwitchLog + " single press (ON)")

  } else if (Room_Main_Light.state == ON) {

    Room_Main_Light.sendCommand(OFF) // Toggle off

    logInfo(vRoomSmartSwitchLog, vRoomSmartSwitchLog + " single press (OFF)")


} else if (Room_Smart_Switch_Click.state == "double") {

  logInfo(vRoomSmartSwitchLog, vRoomSmartSwitchLog + " double press")



Many thanks in advanced :slight_smile:


Post the event log (use code fences) around the time when this occurs so we can see what is happening.

1 Like

Thanks for the quick response @jswim788!

Do you mean from frontail? Or other logs?


frontail will probably do it. Need to see the events.log. See for info on the logs. I don’t use frontail, but presumably it is showing you exactly these logs which should be what we need to see.

Here you go:

==> /var/log/openhab2/openhab.log <==
2020-07-14 20:27:17.077 [INFO ] [me.model.script.Room Smart Switch] - Room Smart Switch single press (ON)

==> /var/log/openhab2/events.log <==
2020-07-14 20:27:17.137 [nt.ItemStatePredictedEvent] - Room_Main_Light predicted to become ON
2020-07-14 20:27:17.168 [vent.ItemStateChangedEvent] - Room_Main_Light changed from OFF to ON
2020-07-14 20:27:19.686 [vent.ItemStateChangedEvent] - Room_Smart_Switch_Link_Quality changed from 70 to 78
2020-07-14 20:27:19.728 [ome.event.ItemCommandEvent] - Item 'Room_Main_Light' received command OFF

==> /var/log/openhab2/openhab.log <==
2020-07-14 20:27:19.737 [INFO ] [me.model.script.Room Smart Switch] - Room Smart Switch single press (OFF)

==> /var/log/openhab2/events.log <==
2020-07-14 20:27:19.790 [nt.ItemStatePredictedEvent] - Room_Main_Light predicted to become OFF
2020-07-14 20:27:19.813 [vent.ItemStateChangedEvent] - Room_Main_Light changed from ON to OFF

Thank you!

EDIT: Looking at the logs doesn’t seem to be that long. However, just feels like it takes 2 seconds or so for the rule to trigger…

It’s possible that the switch itself has some delay - it has to give you a chance to double tap or hold it if that’s what you want. Or the zigbee2mqtt has a delay?

20:27:17.077 We can see the log from your rule running. By the time of the log, it should have already sent command ON

20:27:17.137 For some reason, you have omitted the events.log of the Item receiving the command from the rule. But we know that it happened, because we can see the effect, autoupdate’s prediction event.

20:27:19.686 Two seconds later, we get an update for some mystery Item. Guessing, is this a channel from the same device that you just commanded? Something derived from the devices response to the command? We need a bit more info on your Items/channels/Things involved here really.
But on the face of it that looks like a 2-second round trip outside of openHAB

2020-07-14 20:27:19.728 A few milliseconds after that, we get a new run of the rule producing an OFF command this time, looks like you pressed the button again. Is that so?

Thank you @jswim788 and @rossko57!

For reference the smart switch that I have is the following Xiaomi Aqara Smart Switch

20:27:17.137 I always see the autoupdate prediction even if I use my light directly from OH. Is this wrong? Saying that, I do see that it turned ON at:

2020-07-14 20:27:17.168 [vent.ItemStateChangedEvent] - Room_Main_Light changed from OFF to ON

20:27:19.686 Yes this is another channel that record the quality of the item.

2020-07-14 20:27:19.728 I pressed the button again to show that it switched off.


Thing topic RoomSmartSwitch "Room Smart Switch" {


        Type string : click "Click" [ stateTopic="zigbee2mqtt/RoomSmartSwitch/click"]

        Type string : action "Action" [ stateTopic="zigbee2mqtt/RoomSmartSwitch/action"]

        Type number : linkquality "Link Quality" [ stateTopic="zigbee2mqtt/RoomSmartSwitch/linkquality" ]

        Type number : battery "Battery" [ stateTopic="zigbee2mqtt/RoomSmartSwitch/battery" ]

        Type number : voltage "Voltage" [ stateTopic="zigbee2mqtt/RoomSmartSwitch/voltage" ]



String          Room_Smart_Switch_Click          "Click"                 <switch>        (gRoom)                   { channel="mqtt:topic:oh:RoomSmartSwitch:click" }

String          Room_Smart_Switch_Action         "Action"                <switch>        (gRoom)                   { channel="mqtt:topic:oh:RoomSmartSwitch:action" }

Number          Room_Smart_Switch_Link_Quality   "Link Quality [%d]"     <network>       (gRoom)                   { channel="mqtt:topic:oh:RoomSmartSwitch:linkquality" }

Number          Room_Smart_Switch_Battery        "Battery [%.1f %%]"     <batterylevel>  (gRoom)                   { channel="mqtt:topic:oh:RoomSmartSwitch:battery" }

Number          Room_Smart_Switch_Voltage        "Voltage [%d mV]"       <energy>        (gRoom)                   { channel="mqtt:topic:oh:RoomSmartSwitch:voltage" }

Thanks as always

note: These only the ‘Link Quality’, ‘Battery’ and ‘Voltage’ items are used on the sitemap. Currently only using the ‘Click’ in rules and would like to use ‘Action’ too (hold)

@rossko57 has a nice write up on autoupdate here.

I will guess that you see a delay because the device needs the time to decide if you are going to press it again for a double click or hold it for a long click. But it could be something else.

Do you have any other device or sensor that you can also press at the same time to see what it does? Any Z-Wave switches? Anything that will cause a report in the log. I suppose you could be at the console (are you running on a Linux box?), type in ‘date’, and press your button and the enter key at the same time, or as close as you can get. Record the date/time on the screen and compare with what you see in the log file.

1 Like

Good advice. At present, we do not know if you are looking for

Delay for pushbutton event getting to openHAB
Delay in processing in openHAB
Delay in getting command to device
Delay in device response arriving back at openHAB

Thank you @jswim788 for the information, that is most helpful :slight_smile:

I do have other Xiaomi door contacts, these seem pretty instant to update. I don’t experience any lag with them…sadly do not have any Z-Wave switches :frowning: Yes, I’m running openhabian on a raspberry pi 3.

@rossko57 My gut it telling me there seems to be a slight delay in the push button event interacting with openHAB. This could be a delay in zigbee2mqtt, saying this the pushbutton is within 2 meters max of the sniffer.

At the same time I wanted to eliminate any issues with my rule that I’ve setup. Can either of you see any issues with it? Or where it could be improved?

Thank you as always :slight_smile:

I don’t see anything obvious in the rule that would be causing a delay. It looks straightforward. No timers are used.

You might add code to cover the case of the Light state being UNDEF or NULL - what do you want your switch to do if the Light state is unknown? Log it? Always switch on? Always switch off? And you might add an else to log the Click state if it doesn’t match one of the 2 you are checking for to see if anything else ever comes in. Very minor points.

1 Like

Yes, agreed - I really ought to cover case of the light state being undefined/unknown and will do that :slight_smile:

I really do think this issue is a lag from zigbee2mqtt. I will probably create a post on that forum and see if anyone can highlight anything :slight_smile:

Thank you all :slight_smile: