[SOLVED] Parsing json and assigning to an item

  • Platform information:

    • Hardware: RPI 3B
    • OS: openhabian
    • openHAB version: 2.4 latest stable release
  • Issue of the topic: I am retrieving a number value through json and try to assign it to an item. This returns an error: ‘Could not invoke method: org.eclipse.smarthome…sendCommand(org.eclipse.smarthome.core.items.Item,java.lang.Number) on instance: null


  • Item:

Number testItem “Test item” (testgroup)

  • Rule:

var String json = …some json request…
var int test = transform(“JSONPATH”, “…value”, json)

I didn’t bother typing it here, but I have obviously logged the hell out of this. The json call returns the correct value, the variable test has a value which seems correct, and it’s really the sendCommand that fails (but only when the value is coming from the json transform, a manually assigned value works fine)

I am kind of getting desperate! Does anyone have an idea where to look?

Try this. Also, be careful with the funky double quotes. Might just be a copy/paste thing, but your strings above don’t have the standard double quote character.

    try {
        val Number test = Integer.parseInt(transform("JSONPATH", "...value", json))
    } catch (NumberFormatException e) {
        logInfo("test","Caught NumberFormatException: {}", e.getMessage)

Hey Mark,

thanks for the suggestion!
Never mind the syntax, I typed it out on my iPad which seems to use those weird double quotes…

I have just tried it and… exactly the same behavior as before, catch is not picking up the error.

2019-05-07 08:44:31.311 [INFO ] [.eclipse.smarthome.model.script.Test] - Number value 5668
2019-05-07 08:44:31.315 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule ‘testJSON’: An error occurred during the script execution: Could not invoke method: org.eclipse.smarthome.model.script.actions.BusEvent.sendCommand(org.eclipse.smarthome.core.items.Item,java.lang.Number) on instance: null

Code used:

try {
var String json = sendHttpGetRequest(url)
var Number test = transform(“JSONPATH”, “$…value”, json)
logInfo(“Test”, "Number value " + test)
logInfo(“Test”, “Number assigned”)
} catch (NumberFormatException e) {
logInfo(“test”,“Caught NumberFormatException: {}”, e.getMessage)

Weird thing is that this works fine…

var Number test = 1
logInfo(“Test”, "Number value " + test)
logInfo(“Test”, “Number assigned”)


2019-05-07 08:57:00.150 [INFO ] [.eclipse.smarthome.model.script.Test] - Number value 1
2019-05-07 08:57:00.166 [ome.event.ItemCommandEvent] - Item ‘testItem’ received command 1
2019-05-07 08:57:00.168 [INFO ] [.eclipse.smarthome.model.script.Test] - Number assigned

So ‘something’ weird is going on with the transform.

You’re trying to send an int type variable via sendCommand. Note that when it works in your simple test, you send a Number type.
I think the command needs to be a string or easily convertible to a string, but you know that it works with a Number, so …

Okay, that’s rubbish - just read your most recent test properly, where you do use a Number. I’d still try Number.toString in the sendCommand()

transform returns a String type, not a Number type. If you were editing your rule in VS Code, you would see multiple errors.

I have this exact test case in a rule and I know it works.

    /* JSONPATH parseInt example */
    val String sJson = '{"state":"ON","brightness":255}'
    try {
        val Number nBright = Integer.parseInt(transform("JSONPATH","$.brightness",sJson))
        val Number converted = (nBright * 100)
        logInfo("test","Converted = " + converted)
    } catch (NumberFormatException e) {
        logInfo("test","Caught NumberFormatException: {}", e.getMessage)
2019-05-07 06:37:00.138 [INFO ] [.eclipse.smarthome.model.script.test] - TEST: Test rule is executing
2019-05-07 06:37:00.138 [INFO ] [.eclipse.smarthome.model.script.test] - Converted = 25500
openhab> smarthome:items list | grep TestItem_Number                                                                                       
TestItem_Number (Type=NumberItem, State=25500, Label=Test Number, Category=null)

Damn, I always forget that doesn’t force a Number type var

Yep, in VS Code, that expression generates an error.

Type mismatch: cannot convert from String to Number(org.eclipse.xtext.xbase.validation.IssueCodes.incompatible_types)

Ah darn! That looks like a good fix! At work now but I’ll try it tonight!

BTW I do use VS code sometimes, but notepad++ more often. Perhaps this is a good moment to switch :man_facepalming:

Thanks Mark!

Yep, that fixed it…


Thanks again Mark: I would never have thought of this

1 Like