I’m currently working on optimizing my OpenHAB setup, specifically focusing on a rule that monitors firmware updates for my Sonoff devices. I’ve been able to get the rule working for the most part, but I’m running into an issue when trying to determine whether the rule was triggered by an item or a channel. Here’s what I’ve got so far:
I’m using OpenHAB 2.5 Milestone 1 on a Raspberry Pi 3B. The rule in question is designed to check the firmware versions of my Sonoff devices and log the results. It works perfectly when triggered manually, but when it’s triggered by a channel event (like sunrise or sunset), I run into an error. The error message I’m getting is:
2019-07-29 06:54:00.638 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘Sonoff Tasmota Release Version’: cannot invoke method public abstract org.eclipse.smarthome.core.types.State org.eclipse.smarthome.core.items.Item.getState() on null
I understand that the issue is arising from the if-statement where I’m checking triggeringItem.state == ON. It seems that when the rule is triggered by a channel event, triggeringItem is null, which causes the error. I’ve tried various approaches to handle this, including checking if triggeringItem is null before accessing its state, but I’m not quite getting it right.
Here’s the relevant section of my rule:
if (triggeringItem.state == ON) {
logInfo(“sonoff.rules”, "Rule was manually fired: " + triggeringItem + " previousState: " + previousState);
} else {
logInfo(“sonoff.rules”, "Rule fired by Channel: ");
}
I’m really stuck here and would appreciate any guidance on how to properly structure this if-statement to handle both item and channel triggers without errors. I’ve spent hours trying different solutions, and while I’ve learned a lot about OpenHAB rules, I’m still not quite there. Any help would be greatly appreciated!