Hi everyone,
I’ve been diving into the world of smart home automation with OpenHAB, and I wanted to share my experience with integrating Xiaomi button switches. It’s been a learning curve, but I’m thrilled with how it all came together!
Initially, I set up my Xiaomi button switches to trigger events through channels, and it worked like a charm. Here’s a snippet of the rule I used:
rule “Xiaomi Button Switch 1”
when
Channel “mihome:sensor_switch:158d00012345678:button” triggered
then
var actionName = receivedEvent.getEvent()
switch(actionName) {
case “SHORT_PRESSED”: {
logInfo(“RULES”, “Xiaomi 1 triggered SHORT_PRESSED”)
end
}
}
end
But I wanted to take it a step further by integrating it with items instead of channels. I faced some challenges along the way, especially with the receivedEvent not being recognized in my second rule. Here’s what I tried:
rule “Test Item 1”
when
item test_item_1 received update
then
var actionName = receivedEvent.getEvent()
logInfo(“RULES”, "test_item_1 received the following: " + actionName)
end
Unfortunately, I encountered an error: The name 'receivedEvent' cannot be resolved to an item or type. After some research and troubleshooting, I realized that receivedEvent isn’t compatible with item triggers in the same way it works with channels. I needed a different approach.
Here’s what worked for me: Instead of using receivedEvent, I utilized the event variable directly. I modified my rule to capture the event type when the item is updated. Here’s the corrected version:
rule “Test Item 1”
when
item test_item_1 received update
then
var String actionName = event.getEvent().toString()
logInfo(“RULES”, "test_item_1 received the following event: " + actionName)
end
This change allowed me to successfully capture and log the event type whenever test_item_1 is updated. It was a bit frustrating at first, but figuring it out gave me a great sense of accomplishment!
I’d love to hear from others who have successfully integrated Xiaomi devices with OpenHAB. What tips or tricks have you discovered along the way? Let’s keep the learning and sharing going!
Cheers,
[Your Name]