I have problems with a rule when i try to get “triggeringItem.name”.
Here is my rule:
rule "Get Item Name"
when
Member of gEneTotal changed
then
logInfo("TEST", "+++++++++ GET triggeringItem.name +++++++ : " + triggeringItem.name)
logInfo("TEST", "+++++++++ GET triggeringItem.state+++++++ : " + triggeringItem.state)
logInfo("TEST", "+++++++++ GET triggeringItem.label+++++++ : " + triggeringItem.label)
end
I remeber that it was possible in OH 2.5.
Since i moved to 3.2.0.M2 - Milestone Build i tried several times and different ways to get the name from the item that triggered the rule.
Here is the output from logging:
2021-10-19 13:07:04.521 [INFO ] [org.openhab.core.model.script.TEST ] - +++++++++ GET triggeringItem.state+++++++ : 121.114
2021-10-19 13:07:04.522 [INFO ] [org.openhab.core.model.script.TEST ] - +++++++++ GET triggeringItem.label+++++++ : Energie Total
I need the triggeritem.name to execute some SQL querys.
Would be very appreciated for help.
This is interesting.
There have been changes in this area for OH3.
Implicit variable triggeringItem now only exists when there is a Member of xx rule trigger (in OH2 it existed under some other circumstances as well).
That’s okay, you do have a Member of type trigger.
And you can prove it works with triggeringItem.state etc.
There is also a new implicit variable triggeringItemName which isn’t limited to Member of rule triggers. This one is just a string, the name only.
So you can try triggeringItemName in your rule, just as a sanity test.
You might also try the less commonly used .getName() synonym.
logInfo("TEST", "+++++++++ GET triggeringItemName +++++++ : " + triggeringItemName)
logInfo("TEST", "+++++++++ GET triggeringItem.getName() +++++++ : " + triggeringItem.getName())
logInfo("TEST", "+++++++++ GET triggeringItem.name +++++++ : " + triggeringItem.name)
but of course triggeringItem.name should be working.
It’s just possible a bug was introduced with the new change, but if so it is a very weird kind of bug.
From what you’ve shown us your logInfo() for .name neither throws an error nor produces any output, I’ve no idea how that could happen. Sure your edited rules file has been saved and read correctly? Sure you are looking at complete log entries?
Hi Rossko57,
this is a very weird behavior.
I tested also
rule "Get Item Name"
when
Member of gEneTotal changed or
Member of gEneTotal received update
then
val membername = triggeringItem.name.split("_").get(1) //Schlaf, TV, Trock, etc.
logInfo("TEST", "+++++++++ GET membername+++++++ : " + membername)
logInfo("TEST", "+++++++++ GET triggeringItemName+++++++ : " + triggeringItemName)
logInfo("TEST", "+++++++++ GET triggeringItem.state+++++++ : " + triggeringItem.name)
logInfo("TEST", "+++++++++ GET triggeringItem.state+++++++ : " + triggeringItem.state)
logInfo("TEST", "+++++++++ GET triggeringItem.label+++++++ : " + triggeringItem.label)
end
and the output is the following:
2021-10-19 15:24:44.170 [INFO ] [org.openhab.core.model.script.TEST ] - +++++++++ GET membername+++++++ : Desk
2021-10-19 15:24:44.173 [INFO ] [org.openhab.core.model.script.TEST ] - +++++++++ GET triggeringItem.state+++++++ : 121.114
2021-10-19 15:24:44.174 [INFO ] [org.openhab.core.model.script.TEST ] - +++++++++ GET triggeringItem.label+++++++ : Energie Total
As you can see from the logs there is no output or error for:
triggeringItemName
triggeringItem.name
the rest works like expected.
The strange thing is that i can get a part of the itemname with split
I looked at an old backup from OH2.5 and i am pretty sure it worked before.
I had a similar rule.
I haven’t changed something on the logging but get this faults:
2022-01-02 16:35:30.090 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘12_light-8’ failed: cannot invoke method public abstract java.lang.String org.openhab.core.items.Item.getName() on null in 12_light
when I am using “+ triggeringItem.name” within a logInfo() call in a rule. If I use the instead “triggeringItemName” everything works fine.
Well, it would be easy to replace those strings, but I want to understand whats going wrong, especially because I do have a lot more problems with my rules in OH3.
That is an announced breaking change between OH 2 and OH 3. triggeringItem only exists for rules triggered by a Member of trigger. All other Item triggered rules have triggeringItemName only.