sendCommand or postUpdate does not work in OH 2.1 - why?

I just try to upgrade from openhab 1.8 to 2.1.

In OH 1.8 I have over 100 rules with sendCommand or postUpdate.
In OH 2.1 none of these rules works

Here is an example which works in OH 1.8 but does not work in OH 2.1:

in default.items:
Number D25_alive "D25 alive [%d]"
Number D25_alive_ack “D25 alive bestaetigen [%d]”

in default.rules:

var Number D25_alive = 0
var Number D25_alive_ack = 0

// ----------- Include the data of mqtt -----------
Rule "D25 alive"
When
Item D25_alive received update // D25 sends an alive packet
Then
SendCommand (D25_alive_ack, 1) // send a 1 back
end

The error message:

2017-02-11 16: 32: 55.258 [ERROR] [.script.engine.ScriptExecutionThread] -
Rule ‘D25 alive’: An error occured 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

Do I forget something important - a binding or something else?

Having variables and Items with the same name looks like it will end in trouble? I don’t suppose sendCommand(D25_alive_ack, … knows whether to operate on the Item or the variable of that name.

yeah! That makes sense.

I have made now a test rule without item:

var Number test = 0
// -----------

rule "test rule"
when
Item test received update
then
test.sendCommand(1)
end

It gives the following errors:

2017-02-12 17:21:17.799 [ERROR] [.script.engine.ScriptExecutionThread] - Rule ‘test rule’:
An error occured during the script execution: The name ‘.sendCommand()’ cannot be resolved to an item or type.

i tried also:

var Number test11 = 0

rule "test rule"
when
Item test10 received update
then
test11.sendCommand(1)
end

and got:

2017-02-12 17:38:33.395 [ERROR] [.script.engine.ScriptExecutionThread] - Rule ‘test rule’:
An error occured during the script execution: The name ‘.sendCommand()’ cannot be resolved to an item or type.

Don’t think you can do that without an item.
Items are objects with methods ‘attached’ to them. This seems similar to the discussion here:


try to define an item and use this item with sendCommand.

Now i have two Items:

Number test10
Number test11

but i got the same Error.

silly question: did you delete the var declaration in your rules?
Also you may try postUpdate rather than sendCommand, as Numbers do not really take commands
By the way, it is helpful if you use the code fences when formatting rules or logs or anything; they are at the top line of forum window where you type things in and they make for great formatting and easier reading.

No. That was probably my fault.

Now I have:

rule "test rule"
when
    Item test10 received update
then
	postUpdate (test11, 1)
end

and got:

2017-02-12 18:19:31.573 [ItemStateChangedEvent ] - test10 changed from 2 to 1

Thank you!!

I will look at my other rules from OH 1.8 whether they need a sendcommand or a postUpdate.

Glad you got it to work.
Just one more thing: while your version works, it is more recommended (by those in the forum who know much more than me) to use
item.postUpdate(value) or in your case test11.postUpdate(1)
doing so apparently can help avoid some issues down the line.

Yes, I read that.

OH 1.8 also works with a var number declaration.
OH 2.1 seems to be somewhat more critical.

I’m trying to switch from OH 1.8 to OH 2.1 at the moment. SItemaps, items and bindings are already running.
Now the .rules file has to be adapted.

I don’t know how it ever worked, but I guess OH 1.8 happens to look for Items before variables. You were lucky, now your luck has run out.

Why would you want Items and variables with the same names? How can you tell which one you meant to refer to when you write rules?

The rules are already a few years old. At that time, I probably thought I had to define a variable and an item with the same name.

And because everything worked well, I never changed anything.