Help with RFXCOM and 4 Port Relay - Lighting4 Protocol

I’ve just received a 4 port Aoke relay from Aliexpress which I’m trying to get working with openhab.
Following information I found at

I’ve managed to get two of the relays on the board to toggle when setup as a Switch item, but to work with the rest of them I need to be able to send more than a simple OFF and ON Command to the board.
Using RFXMGR on windows I’ve managed to find the address, and just taking the most significant 5 sets of 4 binary numbers I have a working Hex and also Decimal number that I can plug into Items as a switch to get it working with the V1.9.0 binding. An item formatted similar to below works.

 Switch RFXTest { rfxcom=">123456.550:LIGHTING4.PT2262:Command" }

But this seems to send either 0001 or 0100 as the last 4 bits witch toggles the first and third relays on the board. To access the other 2 I need to replace the last byte with the decimal version of 0000-1111, as work with RFXMGR suggests that with the number I’ve found, the last 4 bits represents the individual relay states.
I’m assuming the switch item just sends either 0001 or 0100 meaning relay 3 on , relay 1 off, or relay 3 off, relay 1 on

So far I’ve tried replacing the word “Command” with “RawData” and also configuring the item as a string item.but this didn’t work.
Is there any chance of getting the binding modified so a number item can be added to the list of valid commands? I can then use a rule to create the correct number for the number of relays I want ON / OFF
@Speeder can you help as you worked on the other problem?

This isn’t a binding problem but a switch problem. Use a String out Number Item and put it on your sitemap as a switch with mappings to convert the ON and OFF to the number or text you need.

Alternatively, create a proxy Switch Item and a rule to send the number or String to the String Item.

Thanks for the reply, can you give me an example? I already have the wanted value stored as a number item between and 15

Here is a Number Item on my sitemap which controls which charts become visible.

Switch item=Nest_Temp_Chart_Period label="Period" mappings=[0="Hour", 1="Day", 2="Week"]

If you replaced the “real” Item with a proxy Item put the proxy on the sitemap like above. then create a rule:

rule "Send command"
when
     Item ProxyItem received command
then
    switch(ProxyItem.state as DecimalType) {
        case 1: RealItem.sendCommnad("cmd for 1")
        case 2: RealItem.sendCommand("cmd for 2")
    }
end

Thanks - can you suggest what the item should be? The following 3 don’t seem to work and throw
"Invalid item type for value selector ‘Command’!" followed by many lines of text. Perhaps “Command” needs replacing with something else?

String RFXTest { rfxcom=">123456.550:LIGHTING4.PT2262:Command" }
Number RFXTest { rfxcom=">123456.550:LIGHTING4.PT2262:Command" }
Switch RFXTest { rfxcom=">123456.550:LIGHTING4.PT2262:Command" }

My Sitemap now contains

Switch item=RFXTest label="rfxcom test" mappings=[0="0", 1="1", 2="2", 3="3", 4="4"]

I think you need to use RawData for your String Item. See the table at the bottom of the RFXCOM binding wiki page.

Can you advise what type “Item ProxyItem” should be?

With Switch I get “Received HTTP POST request at ‘items/RFXProxy’ with an invalid status value ‘2’.”

With Number I get “Error during the execution of rule ‘RFX Proxy’: Cannot cast org.openhab.core.library.items.NumberItem to org.openhab.core.library.types.DecimalType”

With String I get “Error during the execution of rule ‘RFX Proxy’: Cannot cast org.openhab.core.library.items.StringItem to org.openhab.core.library.types.DecimalType”

The items are one of

String RFXProxy
Number RFXProxy
Switch RFXProxy

The Rule is

rule "RFX Proxy"
when 
	Item RFXProxy received command
then
switch(RFXProxy as DecimalType) {
	case 1: RFXTest.sendCommand(1)
	case 2: RFXTest.sendCommand(2)
	case 3: RFXTest.sendCommand(3)
	case 4: RFXTest.sendCommand(4)
}
end

and the sitemap has

Switch item=RFXProxy label="rfxcom test" mappings=["0"="0", "1"="1", "2"="2", "3"="3", "4"="4"]

The item itself is

String RFXTest { rfxcom=">123456.550:LIGHTING4.PT2262:RawData" }

Number

You have a syntax error. Your switch statement should be, as in my original above:

switch(RFXProxy.state as DecimalType) {

Thanks, now a numbered item, and the missing “,state” added to the rules. As it doesn’t yet work I added a log statement to the bottom of the rule and I now see that “RFXTest is uninitialized” after I send it a command. I can postUpdate(RFXTest, 0) from a rule which works but the proxy rule doesn’t change it. My other RFXCOM items seem to work so it must be something with the binding item?

I’ve added an additional rule to tell me what is going on

rule "Log RFXTest state"
    when Item RFXTest changed then {
    logInfo("RFXTest", "RFXTest is " +RFXTest.state) }
end

The main rule reads

rule "RFX Proxy"
    when 
	Item RFXProxy received command
then
	switch(RFXProxy as DecimalType) {
        case 1: RFXTest.sendCommand("1")
	case 2: RFXTest.sendCommand("2")
	case 3: RFXTest.sendCommand("3")
	case 4: RFXTest.sendCommand("4")
}
end

The items now read

String RFXTest { rfxcom=">123456.550:LIGHTING4.PT2262:RawData" }
Number RFXProxy

Nothing appears in the log, but adding log statements after the sendCommand suggest the rule is running, but the result seems to be just disappearing??

Also substituting RFXTest.sendCommand(“1)” etc for another of my items, for both a number and a string item successfully updates it, so I wonder if “RawData” is being ignored?

Just to be sure, change your switch statement to use String instead of numbers:

switch(RFXProxy.state.toString) {
    case "1": ...
}

If that doesn’t work I’m out of ideas. I don’t know the RFXCOM binding and it appears to be one of the more arcane ones.

Nope, that didn’t work. I’ll take a look on the recent changes on GitHub to see if I can work out what its expecting. I believe @Speeder is the guy that did the recent changes, so I’ll send him a PM if he doesn’t catch this thread.
Thanks for your help!

Just to be sure… your rule has to be

rule "RFX Proxy"
when 
    Item RFXProxy received command
then
    switch(RFXProxy.state as DecimalType) {
        case 1: RFXTest.sendCommand("1")
	case 2: RFXTest.sendCommand("2")
	case 3: RFXTest.sendCommand("3")
	case 4: RFXTest.sendCommand("4")
    }
end

you even could use

switch(receivedCommand as DecimalType) {

as your trigger is received command, so receivedCommand will contain the command :slight_smile:

String version:

rule "RFX Proxy"
when 
    Item RFXProxy received command
then
    switch(receivedCommand.toString) {
        case "1": RFXTest.sendCommand("1")
        case "2": RFXTest.sendCommand("2")
        case "3": RFXTest.sendCommand("3")
        case "4": RFXTest.sendCommand("4")
    }
end

When using logInfo, be aware that you have to use only strings to compose the output:

logInfo("RFXTest", "RFXTest is " + RFXTest.state.toString)

or use

logInfo("RFXTest", "RFXTest is {}", RFXTest.state)

which should convert the state to a string itself.

Thanks very much for the reply.
To update this a little, I’ve taken a look at recent changes to the binding on Git and also tried changing just the string the rule is sending to into another string item not attached to the binding which seems to work.
It seems like the only outgoing command available with Lighting4 is a switch, putting the binding into debug logging seems to confirm that in its current state it can only process Switch items, and RawData only works with commands coming into the Binding from the radio receiver.
The details are at https://github.com/openhab/openhab/blob/448c32c69c97d0e77f5abf56cf312335566e2626/bundles/binding/org.openhab.binding.rfxcom/src/main/java/org/openhab/binding/rfxcom/internal/messages/RFXComLighting4Message.java in case you fancy a look, the relevant bit seems to start at line 278

Hi @kevin,

You’re right, the only item type available for now with lighting4 message is OnOffType (switch, dimmer). And Rawdata type is only useful for received data.
I prepared an update of the binding to support your specific case : https://github.com/Speederc/openhab/blob/Speederc-RfxCom-Lighting4/bundles/binding/org.openhab.binding.rfxcom/src/main/java/org/openhab/binding/rfxcom/internal/messages/RFXComLighting4Message.java
Unfortunately I’m not able to compile it in the next days. Are you able to do so ?

This update adds the support for Number RFXTest { rfxcom=">123456.550:LIGHTING4.PT2262:Command" } and RFXTest.sendCommand(3)

Regards

Thanks very much indeed!
I’ll have a go at compiling if you can point me to a set of easy instructions. I’ve got a spare pi that I can use without upsetting the live system.
Last time I tried it didn’t go that well, but I’ve learnt a bit more these days

Everything is explained in : https://github.com/openhab/openhab-distro/blob/master/docs/sources/development/ide.md

@Speeder
I think the compile happened - I copy and pasted the text from the link you gave me into the same file in the binding in Eclilse. It had one item it was not happy with which was on line 294.
Your original text

} else if (instanceof DecimalType) {

I changed to

} else if (type instanceof DecimalType) {

This got rid of the error and I was able to right click the binding and choose export, I then replaced the binding on my openhab system with it.
The item was set as

Number RFXTest { rfxcom=">123456.550:LIGHTING4.PT2262:Command" }

But after a reboot I got the following after Loading model ‘default.items’
[i.internal.GenericItemProvider] - Binding configuration of type ‘rfxcom’ of item ?RFXTest? could not be parsed correctly.
org.openhab.model.item.binding.BindingConfigParseException: Invalid item type for value selector ‘Command’!

Changing "Number for “String” didn’t work either. Changing it back to “Switch” makes the item work albeit only as ON and OFF type.

I think something deeper than the added line may need to change to allow it to accept something other than “Command” at the end of the item, RFXTest.sendCommand(“3”) and RFXTest.sendCommand(3) didn’t work either.
Which file defines inside the IDE binding what form the items can take?

@Speeder
Further update, I spent a while looking at it, and I ended up changing the section around line 290 to read

case COMMAND:
            if (type instanceof OnOffType) {
                command = (type == OnOffType.ON ? Commands.ON_1 : Commands.OFF_4);
                commandId = (command.ordinal());
            } else {
                throw new RFXComException("Can't convert " + type + " to Command");
            }
            break;
        case NUMBER:
            if (type instanceof DecimalType) {
                commandId = ((DecimalType) type).intValue();
            } else {
                throw new RFXComException("Can't convert " + type + " to Command");
            }
            break;

Once I’d done this I clicked on the red unhappy type message which gave me a clue that I also needed to add “Number” as an allowed item elsewhere in the Binding. Once I did this and saved both files it exported sucessfully and I now have a working item similar to

Number RFXTest { rfxcom=">123456.550:LIGHTING4.PT2262:Number" }

and from a rules file I can send commands like

RFXTest.sendCommand("15")

I’m happy to share what I’ve done if someone can talk me through how to do the sharing

Hi @kevin
You’re right, the same valueSelector (“Command”) can not be used for 2 different item types.
A new valueSelector has to be defined in RFXComValueSelector.java and in RFXComLighting4Message.java.
If you want to merge your modification in the main Openhab stream, you have to create a fork of the main github repository, update the 2 files in a new branch and then propose a pull request in Openhab.

Regards

Hi @Speeder
I’ve created a new branch, updated the files and created 1 Pull request for each file with the description in the message. Can you let me know if this is correct?