org.openhab.core.items.Item not working in 3.1

Hello,
i tried to switch from OH 2.7 to 3.1 and habe a lot of troubles with bindings and self created things and items

I get follow error message when i try to find an item by name or state:

Script execution of rule with UID ‘my-1’ failed: cannot invoke method public abstract java.lang.String org.openhab.core.items.Item.getName() on null in my

I use the my.items file for creating items but i have to use channel items which are created on the gui.

How can i catch the triggeringItem by name or state?

Here’s some code details:

Thing and channels:

UID: http:url:PowerStrip1
label: PowerStrip1
thingTypeUID: http:url
configuration:
  authMode: BASIC
  ignoreSSLErrors: true
  baseURL: http://192.168.22.81/?cmd=511
  delay: 0
  stateMethod: GET
  refresh: 5
  commandMethod: GET
  contentType: application/json
  timeout: 3000
  bufferSize: 2048
channels:
  - id: PowerStrip_1_Socket_1_Status
    channelTypeUID: http:number
    label: PowerStrip_1_Socket_1_Status
    description: null
    configuration:
      mode: READONLY
      stateTransformation: JSONPATH:$.data.switch[0]
  - id: PowerStrip_1_Socket_1_Watt_Org
    channelTypeUID: http:number
    label: PowerStrip_1_Socket_1_Watt_Org
    description: null
    configuration:
      mode: READONLY
      stateTransformation: JSONPATH:$.data.watt[0]

Items:

PowerStrip1_Socket_1_Status
String . Point
PowerStrip1_Socket_1_Status

PowerStrip_1_Socket_1_Watt_Org
Number . Point
PowerStrip_1_Socket_1_Watt_Org

Items in my.items:

Number PowerStrip_1_Socket_1_Watt “Verbrauch1_1”
Switch PowerStrip_1_Socket_1 “Steckdose1_1” (Steckdosen)

my.rules:

rule "PowerStrip_Switch"
    when
	Item PowerStrip_1_Socket_1 received command or
   	Item PowerStrip_1_Socket_2 received command
    then     
	val strip = triggeringItem.name.split("_").get(1) 
   	val ip = transform("MAP", "steckdose.map", strip)
	val port = triggeringItem.name.split("_").get(3)
	val cmd = if(receivedCommand == ON) "1" else "0"
    	val encoded = URLEncoder::encode('{"port":'+port+', "state":'+cmd+'}', 'UTF-8')
	sendHttpGetRequest("http://"+ip+"/?cmd=200&json="+encoded)
	logWarn("Steckdose", "Steckdose " + strip + " Port" +  port  + "umgeschaltet auf: " + cmd )

end
rule "PowerStripWatt"
	when
		Item PowerStrip_1_Socket_1_Watt_Org changed or
		Item PowerStrip_1_Socket_2_Watt_Org changed
	then
	val watt = (triggeringItem.state as Number)/1000
	//val java.text.DecimalFormat df = new java.text.DecimalFormat("#.#")
	//val Number watt = Double.valueOf(df.format((triggeringItem.state as Number)/1000))
	
	val gItemName2 = triggeringItem.name.replace("_Org","")
	//logWarn("Wattänderung.test", triggeringItem.name.toString()  )
	//logWarn("Wattänderung.test", gItemName2  )
    if(triggeringItem.state == "0")
	{
	postUpdate(gItemName2,0)
	}
	else
	{
	val CompleteWatt = String::format("%.1f", watt)
	 postUpdate(gItemName2,CompleteWatt)
	}
	
	
end

and error messages:

2021-07-04 11:23:56.086 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘my-1’ failed: cannot invoke method public abstract java.lang.String org.openhab.core.items.Item.getName() on null in my

2021-07-04 11:24:15.431 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘my-6’ failed: cannot invoke method public abstract org.openhab.core.types.State org.openhab.core.items.Item.getState() on null in my

triggeringItem has changed in OH3.

In OH2,it was generally available.
In OH3, it is only available if you trigger from ‘Member of

However a new implicit variable triggeringItemName is available

1 Like

Thanks for help. now this scripts are running fine again.

So, this means any logic also must be a Member Of:

rule "lampsCtrl"
when 
    Member of gOutWeather changed
    or 
    Item localCurrentTemperature changed
then 

if gOutWeather triggered, triggeringItem would be set.
If localCurrentTemperature triggered, triggeringItem is null, or otherwise unusable.

Do I have this right?