[SOLVED] Using number item in rules

I try the following rule in order to catch user input from the basicui and trigger settings of an external device. The trigger works as expected, but I am not able to read the value of the Number type Item.

  • Platform information:
    • RPi with openhabian and openHAB 2.5.0~S1549-1 (Build #1549)
    • Rules code related to the issue
rule "setPowerLimit"
	Item PW_powerLimit changed
	\\var String testString = PW_powerLimit.toString
	logInfo("new powerLimit is ", PW_powerLimit.toString)		
	var Number newPowerLimit = PW_powerLimit.state
	logInfo("new powerLimit is ", newPowerLimit)		

Number	 	PW_powerLimit 				"powerLimit [%d A]" 		{ http="<[http://<IP:port>/powerLimit:30000:JSONPATH($.powerLimit)]" }

The first logInfo in this minimal rule show that the item is actually a Number Item, and also show that the item contains “State=26”. The second logInfo should print the number, but it throws an error. I have tried many different options suggested, but I am not able to read the value to a variable.

I hope anyone spot my silly mistake.

  • Services configuration related to the issue
  • If logs where generated please post these here using code fences:
2019-03-18 11:25:44.465 [INFO ] [home.model.script.new powerLimit is ] - PW_powerLimit (Type=NumberItem, State=32, Label=powerLimit, Category=null)

2019-03-18 11:25:44.471 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'setPowerLimit': An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.LogAction.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null

As I understand your Logging, it seems not to be the same as what the Rule looks like:

The logInfo Command says: show testString, but the logger shows PW-powerLimit.

How does your item looks like ?

Have you just tried this:

var Number newPowerLimit = PW_powerLimit.state
logInfo("new powerLimit is ", newPowerLimit)

Thank you for your comment Peter,

I have edited the rule to make it evident that the printout comes from the rule directly. Also, trying to print the PW_powerLimit.state with the logInfo give similiar result as printing PW_powerLimit.state as Number

I have now included the item as well.

You are using the logInfo incorrectly. It expects two strings.

logInfo(“MyLogEntry”, new powerLimit is {} ", newPowerLimit)

Hi Jürgen,
you are quite right. I just tested in my setup (in old version :wink:)

rule "setPowerLimit"
    Item Dummy4 changed to ON
    var String testString = CPU_Temp_num.toString
    logInfo("new powerLimit is ","1: " + testString)       
    var Number newPowerLimit = (CPU_Temp_num.state as Number)
    logInfo("new powerLimit is ","2: " + newPowerLimit)

and the result now looks:

2019-03-18 11:56:06.234 [INFO ] [home.model.script.new powerLimit is ] - 1: CPU_Temp_num (Type=NumberItem, State=48.31, Label=CPU-OH2-Pi Temperatur, Category=temperature, Groups=[Chart_Sys_Temp])

2019-03-18 11:56:06.240 [INFO ] [home.model.script.new powerLimit is ] - 2: 48.31

Thanks opus :smiley: