Really basic question about syntax (items and rules)

When writing some simple “proxy rules” (mapping between items associated with the Z-Wave binding and virtual items that are only used in rules, sitemaps, etc.) I normally do something like this:

rule "Proxy"
when
	Item sSwitch_Phy received command
then
	sendCommand(sSwitch_Vir, sSwitch_Phy.state)
end

This seems to work fine.

I have since day one been using a simple text editor (nano since I am working on Linux) but inspired by @rlkoshak’s constant talk about the wonders of “the openHAB Designer and Ctrl-Space”, I decided to give it a try, :smile:

The first thing I discovered was that the designer complained about my simple rules (as shown above). Playing along with the suggestions (using Ctrl-Space) I ended up with the following to please the designer:

rule "Proxy"
when
	Item sSwitch_Phy received command
then
	sendCommand(sSwitch_Vir.name.toString, sSwitch_Phy.state.toString)
end

This seems a little bit elaborate. Is this really how it is supposed to be written, or am I missing something here?

For some reason, the Designer is not understanding the other static method signatures for sendCommand in the BusEvent class. This is probably a bug with the Designer.

Although it’s unfortunate if there is a bug in the designer, I am glad to hear this as I was getting increasingly confused by this issue.

Going back to my inital example…

rule "Proxy"
when
	Item sSwitch_Phy received command
then
	sendCommand(sSwitch_Vir, sSwitch_Phy.state)
end

…and looking at the method signatures in the BusEvent class, I wonder what would be the correct way of doing what I want (which is basically propagating a Command from one Switch item to another)?

Looking at it now, I can see two problems with my example:

  1. When I receive a command on the item sSwitch_Phy, the state has not been updated yet, so I cannot really use the state information to determine what command to pass on to the next item (sSwitch_Vir).
  2. There is no sendCommand method that natively accepts a State so I would have to type cast this to something else.

Q: When you receive a command on a Switch item, is there a way to determine what the command was (e.g. ON or OFF)?

I guess the alternative approach would be to have two rules:

rule "Proxy #1"
when
	Item sSwitch_Phy received command ON
then
	sendCommand(sSwitch_Vir, ON)
end

rule "Proxy #2"
when
	Item sSwitch_Phy received command OFF
then
	sendCommand(sSwitch_Vir, OFF)
end

Is this the only correct way of doing this?

Check out the example here - the last one shows how you can use the receivedCommand inside a rule.

FYI - I write all my rules using the item.sendCommand(<CMD>) syntax (or item.postUpdate(<STATE>). I personally find it easier to read when I am visually parsing my rules.

2 Likes

Thank you!!

Exactly what I was looking for. I don’t know why I never registered that part of the documentation before.

Great tip about the item.sendCommand() syntax. I agree that this makes the rules much more readable. For sure, I will convert my ruels ASAP, :smile: