Rules - Sending notification with command received from OpenHAB

Hi everyone.

I am dealing with rules and notifications.
I have some rollershutters connected to OH2, and I want to be notified in my IOS OH App whenever a command is send to them.

The commads received might be “UP” “DOWN” or “STOP” (I am not interested in percentage)

I have created the following rule, but I am getting error in OSGI console because of the receivedCommand:
rule "RS_1" when Item RS_1 changed then logInfo("RS_1", "TEST_RS_1") sendBroadcastNotification("Rollershutter 1: " + command.toString.lowerCase) end
I want to receive the command in the notification message. This way, I avoid creating one rule for each command.

Can someone point me in the right direction.
Thanks, regards.

I don’t know if this is something that changed in OH 2, but in OH 1 you would use receivedCommand.toString.lowerCase, not command.toString.lowerCase.

One other thing to be aware of is that even though you can often send a commands like UP, DOWN, etc. to an Item that stores its state as a number, the value of receivedCommand will be the new number value, not the UP/DOWN state.

Also, if RS_1 is an Item and not a Group, then is it not the case that receivedCommand will be the same as RS_1.state?

All of this is from the perspective of OH 1 so I could be totally wrong.

Unfortunately, it seems something has changed.

21:30:01.317 [ERROR] [.script.engine.ScriptExecutionThread] - Error during the execution of rule ‘RS_1’: The name ‘receivedCommand’ cannot be resolved to an item or type.

There should be another way. Let’s hope someone clarifies this for us.
Regarding:

One other thing to be aware of is that even though you can often send a commands like UP, DOWN, etc. to an Item that stores its state as a number, the value of receivedCommand will be the new number value, not the UP/DOWN state.

Is there any way to send the command name instead of the number value of it?
Thanks, regards.

You can send the command name but the problem is it almost immediately gets translated to the item’s native format (i.e. a number).

However, you could set up a proxy item that takes a string or is of the right type to accept the command type and trigger your rules on that item, forwardingo the command to the actual item in your rule.

Once again, I’ll caveat with I only know OH 1 and I’m drawing conjectures from some things I’ve seen posted about dimmers. I could be completely off base.

Thanks anyway Rich.
I’ll look into that.

Nevertheless, if anyone know how to achieve this in OH2, I would appreciate it very much,

Regards.
Gonzalo.

Just had a quick look and noticed a couple of things…

command should be receivedCommand but as you mentioned receivedCommand later on you may already know that.

Also you’ve got the rule firing when the item (state) has changed rather than when a command is received so at a guess receivedCommand is not in scope when this rule fires.

Try using the below and see if it helps:

Item RS_1 received command

Alternatively, if you do indeed want to fire the rule only once the item has changed you’ll probably need to use RS_1.state.toString.lowerCase instead in your sendBroadcastNotification call.

If the rule’s triggers did not include a received command, then there can’t be a receivedCommand available in the body of the rule.

[Smacks head] I read that in the wiki not more than a couple of weeks ago and it totally slipped my mind.

Thanks everyone.

It works flawlessly!

Here is how it ended up:

rule "RS_1"
when
        Item RS_1 received command
then
        logInfo("RS_1", "TEST_RS_1")
	sendBroadcastNotification("Rollershutter 1: " + receivedCommand.toString.lowerCase)
end