[SOLVED] Rule Error "could not invoke method"

Hey guys. I have a rule to basically make two tplink switches a three way switch. I have a rule to make the two mirror each other’s state. The rule has worked for several months. I had one little hiccup where the rule stopped working randomly, but a restart of the DiskStation that i have OH2 installed on seemed to get it working again. Here is the rule.

rule "CPkitchen1"
when
	Item CP_Kitchen_Lights changed
then
    val LightsState = CP_Kitchen_Lights.state
    CP_Kitchen_Slave.sendCommand(LightsState)
end

rule "CPkitchen2"
when
	Item CP_Kitchen_Slave changed
then
    val SlaveState = CP_Kitchen_Slave.state
    CP_Kitchen_Lights.sendCommand(SlaveState)
end

A few more months pass and the rule stops working again, and this time a restart was not helpful. I take a look at the log files and I noticed this error. Obviously it has something to do with the “sendCommand” part of the rule but I don’t know enough to figure out the details. Any help would be greatly appreciated!


2018-10-25 11:10:28.887 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule 'CPkitchen2': 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

Add some logging. I wouldn’t be surprised that CP_Kitchen_Slave.state is NULL. You cannot send a NULL as a command.

The Rules themselves are super simple so this is the only think I can think of that would be causing this error.

From the most recent startup in the event.log file

2018-10-25 10:56:58.977 [hingStatusInfoChangedEvent] - 'tplinksmarthome:hs200:5a5dac5b' changed from UNKNOWN to ONLINE
2018-10-25 10:56:59.113 [vent.ItemStateChangedEvent] - CP_Kitchen_Lights changed from NULL to OFF

and

2018-10-25 10:56:58.947 [hingStatusInfoChangedEvent] - 'tplinksmarthome:hs200:D47A1E' changed from UNKNOWN to ONLINE
2018-10-25 10:56:59.046 [vent.ItemStateChangedEvent] - CP_Kitchen_Slave changed from NULL to OFF

Makes sense that you can’t send a NULL. What other logging can I add?

Thanks for the quick reply!

Confirmed through SSH, both switches have the state OFF

We really need to know what the state is inside the Rule.

There isn’t a whole lot that can go wrong here. You are successfully avoiding an infinite loop by using the changed trigger.

The error generated is almost always caused by trying to sendCommand with a type that the Item doesn’t support.

Both Items have to be Dimmers or Numbers or else the Rules would never have worked.

All that leaves, as far as I can figure, is that CP_Kitchen_Slave.state is NULL or UNDEF when CPkitchen2 runs.

Note that if CP_Kitchen_Slave does change to NULL or to UNDEF, the Rule will trigger.

Okay i think im missing where i need to do this…console?

sorry I haven’t had to do any debuging or logging before so this is new to me. do i need to put a line such as

logDebug("xxxxx", "xxxxx")

in the rule? if so on which line? underneath the sendCommand(SlaveState) line?

Yes but use logInfo. logDebug won’t show up unless you change the logger.

The statement needs to go before the line that generates the error. Otherwise it won’t run.

If there’s a glitch in communications to the TPlink box - router reboot, WiFi interference? - the binding will most likely set state to UNDEF or maybe NULL for the duration.
That’s a “normal event” really, and your rule won’t deal with it nicely.

Change the triggers:

rule "CPkitchen1"
when
	Item CP_Kitchen_Lights changed to ON or
	Item CP_Kitchen_Lights changed to OFF
then
    val LightsState = CP_Kitchen_Lights.state
    CP_Kitchen_Slave.sendCommand(LightsState)
end

rule "CPkitchen2"
when
	Item CP_Kitchen_Slave changed to ON or
	Item CP_Kitchen_Slave changed to OFF
then
    val SlaveState = CP_Kitchen_Slave.state
    CP_Kitchen_Lights.sendCommand(SlaveState)
end

This way you’ll avoid a NULL state

@vzorglub no difference when using the altered rule. still gives me the same error.

@rlkoshak these are the log results

10:58:08.177 [INFO ] [del.core.internal.ModelRepositoryImpl] - Loading model 'CP_Lights.rules'
10:58:12.448 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'CP_Kitchen_Lights' received command ON
10:58:12.461 [INFO ] [smarthome.event.ItemStateChangedEvent] - CP_Kitchen_Lights changed from OFF to ON
10:58:12.535 [INFO ] [pse.smarthome.model.script.SlaveState] - The Master state is ON
10:58:12.546 [INFO ] [pse.smarthome.model.script.SlaveState] - The Slave state is OFF
10:58:12.558 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'CPkitchen1': 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
10:58:16.458 [INFO ] [smarthome.event.ItemCommandEvent     ] - Item 'CP_Kitchen_Slave' received command ON
10:58:16.464 [INFO ] [pse.smarthome.model.script.SlaveState] - The Master state is ON
10:58:16.466 [INFO ] [smarthome.event.ItemStateChangedEvent] - CP_Kitchen_Slave changed from OFF to ON
10:58:16.472 [INFO ] [pse.smarthome.model.script.SlaveState] - The Slave state is ON
10:58:16.480 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'CPkitchen2': 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

@rossko57 i guess that could be the case, as this is multiple buildings all in a PtMP setup. However everything has a solid connection. no packet loss between my laptop (currently in the same building as the server) and the devices (different building).

And as stated above, the states are defined, not NULL or UNDEF

Try: CP_Kitchen_Lights.sendCommand(SlaveState.toString)

Show the definitions of both Items.

@vzorglub okay the .toString argument worked. What does that do? How does it affect the sendCommand?

@rlkoshak show definitions how?

If you defined your Items in .items files, paste those lines into your reply. Please How to use code fences.

If you defined them in PaperUI paste a screen grab of the page for each Item.

Ok let’s look at your code:

rule "CPkitchen2"
when
	Item CP_Kitchen_Slave changed to ON or
	Item CP_Kitchen_Slave changed to OFF
then
    val SlaveState = CP_Kitchen_Slave.state
    CP_Kitchen_Lights.sendCommand(SlaveState)
end

When you save the state of the light in a val SlaveState you are storing a state object.
It can be any generic state object because we didn’t specify the type. Therefore the sendCommand method is struggling to convert the val into a meaningful command for the item.
So if we add the .toString it will pull out the value of the state to a String (In this case “ON” or" OFF") and that can be used as a command.

You could have done a one liner:

    CP_Kitchen_Lights.sendCommand(CP_Kitchen_Slave.state)

That should work

I assumed, and am probably wrong, that SlaveState would have been either a State or Command type Object which sendCommand should have been able to handle. But I bet you are absolutely right, it’s an Object and sendCommand can’t handle that.

Good catch.

makes good sense! Thank you both for all your help!!! hopefully this will be the end of my problems with this particular rule. now on tho the next one!