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
Config:
Item:
Number testItem “Test item” (testgroup)
Rule:
var String json = …some json request…
var int test = transform(“JSONPATH”, “…value”, json)
testItem.sendCommand(test)
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))
testItem.sendCommand(test)
} catch (NumberFormatException e) {
logInfo("test","Caught NumberFormatException: {}", e.getMessage)
}
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)
testItem.sendCommand(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)
testItem.sendCommand(test)
logInfo(“Test”, “Number assigned”)
returns:
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()