Get converted item state from other item

Hello,

i use json and http cache to read informations from powerstrip:

Number PowerStrip_1_Port_1_Watt_Org {http="<[PowerStrip1:2000:JSONPATH($.data.watt[0])]" }
Number PowerStrip_1_Port_2_Watt_Org {http="<[PowerStrip1:2000:JSONPATH($.data.watt[1])]" }
Number PowerStrip_1_Port_3_Watt_Org {http="<[PowerStrip1:2000:JSONPATH($.data.watt[2])]" }
Number PowerStrip_1_Port_4_Watt_Org {http="<[PowerStrip1:2000:JSONPATH($.data.watt[3])]" }
Number PowerStrip_1_Port_5_Watt_Org {http="<[PowerStrip1:2000:JSONPATH($.data.watt[4])]" }
Number PowerStrip_1_Port_6_Watt_Org {http="<[PowerStrip1:2000:JSONPATH($.data.watt[5])]" }

I’ve added this Items at sitemap and it works fine. But i need to convert the number.
I tried it with a rule to convert the state itself. but it will repeat the change event endless. So i try to put the this state to an other Item:

Number PowerStrip_1_Port_1_Watt "Verbrauch"
Number PowerStrip_1_Port_2_Watt "Verbrauch"
Number PowerStrip_1_Port_3_Watt "Verbrauch"
Number PowerStrip_1_Port_4_Watt "Verbrauch"
Number PowerStrip_1_Port_5_Watt "Verbrauch"
Number PowerStrip_1_Port_6_Watt "Verbrauch"

with this rule:

rule "Watt"
	when
		Item PowerStrip_1_Port_1_Watt_Org changed or
		Item PowerStrip_1_Port_2_Watt_Org changed or
		Item PowerStrip_1_Port_3_Watt_Org changed or
		Item PowerStrip_1_Port_4_Watt_Org changed or
		Item PowerStrip_1_Port_5_Watt_Org changed or
		Item PowerStrip_1_Port_6_Watt_Org changed
	then
	 val watt = (triggeringItem.state as Number)/1000
	val gItemName2 = triggeringItem.name.replace("_Org","")
	//logWarn("Wattänderung.test", triggeringItem.name.toString()  )
	logWarn("Wattänderung.test", gItemName2  )
    postUpdate(gItemName2,watt)
end

but i get follow error message:

==> /var/log/openhab2/events.log <==
 [vent.ItemStateChangedEvent] - PowerStrip_1_Port_1_Watt_Org changed from 6996 to 5469

==> /var/log/openhab2/openhab.log <==
 [WARN ] [thome.model.script.Wattänderung.test] - PowerStrip_1_Port_1_Watt
 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'Watt': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.postUpdate(java.lang.String,java.lang.String) on instance: null

if i use “PowerStrip_1_Port_1_Watt” in postUpdate directly, so it will work.

i’m not sure whats wrong?

I think, you cann’t use a variable item name for PostUpdate.
Is it to convert Watt to KW?

rule "Watt P1"
	when
		Item PowerStrip_1_Port_1_Watt_Org changed
	then
                PowerStrip_1_Port_1_Watt.postUpdate((PowerStrip_1_Port_1_Watt_Org.state as Number)/1000)
end

but this code works:

rule "Status"
	when
  		 Item PowerStrip_1_Port_1_Status changed or
  		 Item PowerStrip_1_Port_2_Status changed or
  		 Item PowerStrip_1_Port_3_Status changed or
  		 Item PowerStrip_1_Port_4_Status changed or
  		 Item PowerStrip_1_Port_5_Status changed or
  		 Item PowerStrip_1_Port_6_Status changed
	then
		//val status = if(PowerStrip_1_Port_1_Status.state == 1) "ON" else "OFF"
		val status = if(triggeringItem.state == 1) "ON" else "OFF"
		val  gItemName = triggeringItem.name.replace("_Status","")
	
		logWarn("StartPostUpdate",status)
   		postUpdate(gItemName,status)
end

its exactly the same with other items and convert the state from 0:1 to on:off

That will work. In fact, this is the only time I recommend using the postUpdate Action over the Item method. The post update Action takes two Strings are arguments, the name of the Item and a String version of the new state.

You need to call toString on watt. The postUpdate Action will only accept Strings. Sometimes the Rules DSL is able to convert objects to Strings automatically but there are a few cases where it often fails to do so, namely enum types (e.g. ON/OFF, OPEN/CLOSED, etc) and Numbers.

postUpdate(gItemName2, watt.toString)

amazing!!! i didn’t detect this type issue because normally i get error messages with type errors.

now it works. thanks !!

yes you can:

@Webfeger can you try

postUpdate(gItemName2.toString, watt)

haha beat me to it

But you came with the code. I just come with experience. :slight_smile: