What is the workaround/replacement for ‘triggeringitem’ being deprecated?
I have a few key rules that used triggeringitem in OH2.5. Here is a simplified example.
rule "Speak notify"
when
Item KitchenMotion changed or
Member of gFrontDoors changed to OPEN or
Member of gBackDoors changed to OPEN or
Member of gGaragedoors changed to OPEN or
Item HouseSideExit changed to OPEN
then
val msg = triggeringItem.label
Speak.sendCommand(msg)
end
Is there an OH3 sanctioned way of getting the triggering Item?
rule “workaround”
when
Item a1 received command or
Item a2 received command or
Member of g1 received command
then
logInfo(“testing”, “name: {}”, triggeringItemName)
var list = ScriptServiceUtil.getItemRegistry.getItems(triggeringItemName);
// reality check does an item with that name exist?
if (list == newArrayList) {
logWarn("testing", "Item name lookup failed: '{}''", triggeringItemName);
return;
}
var triggering_item = list.get(0);
logInfo("testing", "label: {}", triggering_item.label)
logInfo("testing", "state: {}", triggering_item.state.toString)
end
The following code is the only way I have figured out how to replicate the functionally of triggeringItem prior to OH 3.0.
I suppose you could omit the reality check since it should always find triggeringItemName (famous last words). I really don’t like using import, it screams this code too will break some day.
Is there a better way to do this?
// Place import at top of rule file
import org.openhab.core.model.script.ScriptServiceUtil
rule "workaround"
when
Item a1 changed to OPEN or
Item a2 changed to OPEN or
Member of g1 changed to OPEN
then
logInfo("testing", "name: {}", triggeringItemName)
var list = ScriptServiceUtil.getItemRegistry.getItems(triggeringItemName);
// reality check does an item with that name exist?
if (list == newArrayList) {
logWarn("testing", "Item name lookup failed: '{}''", triggeringItemName);
return;
}
var triggering_item = list.get(0);
logInfo("testing", "label: {}", triggering_item.label)
logInfo("testing", "state: {}", triggering_item.state.toString)
end
If you’re using this, import org.openhab.core.model.script.ScriptServiceUtil, those rules still have to be defined in a .rules file, and not the UI, right?
I’ve tried some of that, but it throws errors. It feels like it is trying to evaluate the import inside of the THEN section of the rule, whereas with the .rules file, you place the import outside of the rule.
1. The method or field import is undefined; line 1, column 0, length 6
2. The method or field ScriptServiceUtil is undefined; line 3, column 164, length 17
Thanks for the great workaround. I have a slightly different use case with an additional “Time cron” trigger. I need to identify which trigger has fired the rule.
DSL rules in OH3:
// Place import at top of rule file
import org.openhab.core.model.script.ScriptServiceUtil
rule "workaround"
when
System started or // currently not working with OH3
Time cron "0/20 * * * * ? *" or
Item ALLG_EXEC3 changed to ON or
Item ALLG_EXEC4 changed to ON or
Member of gEXEC123 changed to ON
then
logInfo("testing", "name: {}", triggeringItemName)
var list = if (triggeringItemName !== null) ScriptServiceUtil.getItemRegistry.getItems(triggeringItemName) else null
if (list === null) logInfo("testing", "trigger: {}", "Time cron")
else {
// reality check does an item with that name exist?
if (list == newArrayList) {
logWarn("testing", "Item name lookup failed: '{}''", triggeringItemName);
return;
}
var triggering_item = list.get(0);
logInfo("testing", "label: {}", triggering_item.label)
logInfo("testing", "state: {}", triggering_item.state.toString)
}
end
When starting the rule, the expected result is (Time cron every 20 seconds):
After changing a trigger Item once (e.g. ALLG_EXEC4) there is this result. The first three lines show the information about the triggered Item. This is as expected. But the other lines are triggered by “Time cron” (every 20 seconds) again and should show this like above.
It seems to me that “triggeringItemName” holds the last parameter even there is another Time cron trigger event. How can I overcome this problem?
Thanks.