After migrating to OpenHAB 4, I encountered an unexpected challenge with my Blockly rules. One of my rules, which worked seamlessly in OH3.4.4, suddenly stopped triggering. The rule was designed to respond to state changes in a virtual presence switch item using the ‘contextual info’ block. This issue was perplexing, especially since the logic was straightforward with IF/ELSE IF conditions. To troubleshoot, I added an ELSE clause to log when neither ON nor OFF states were matched, which revealed the problem: the rule wasn’t recognizing the states correctly.
Through some research, I stumbled upon a helpful thread that suggested using a ‘create text’ block to resolve the issue. I implemented this solution, and it worked like a charm! This experience made me realize the importance of understanding how Blockly interprets states versus string comparisons. While it was a bit frustrating to adjust all my rules, it also provided a valuable learning opportunity.
I wanted to share my journey in case others are facing similar issues. If you’re transitioning to OH4 and rely on Blockly, keep an eye out for state comparisons and consider the ‘create text’ workaround. It might save you some time and headaches!
Here’s a snippet of my adjusted rule for reference:
markdown
if (String(event.itemState) == ‘ON’) {
// Actions for ON state
} else if (event.itemState == ‘OFF’) {
// Actions for OFF state
} else {
console.info(‘Neither ON nor OFF’);
}
This adjustment ensured my rule functioned as intended. Happy coding and smooth transitions to all OpenHAB enthusiasts out there!