[SOLVED] Rule to show window as open or closed when wifi contact sensor sends RF data

Im at my witts end with trying to fingure this out. copied your solution and altered to suit my situation but still not working. please advise!!!

things file,

Type contact : FrontDoor   "Front Door"          [ stateTopic="tele/Sonoff_Bridge/RESULT", transformationPattern="JS:FrontDoor.js"]
Type contact : RearDoor "Rear Door"        [ stateTopic="tele/Sonoff_Bridge/RESULT", transformationPattern="JS:RearDoor.js"]
}```

items file,

Contact Sonoff_Bridge_Front_Door  "Front Door: [%s]"            <frontdoor>           { channel="mqtt:topic:Sonoff_Bridge:FrontDoor" }
Contact Sonoff_Bridge_Rear_Door  "Rear Door: [%s]"            <door>           { channel="mqtt:topic:Sonoff_Bridge:RearDoor" }

sitemap,

Frame label="Security"   {
               Text item=Sonoff_Bridge_Front_Door
               Text item=Sonoff_Bridge_Rear_Door
         }

transform file

(function(jsonString) {
    var data = JSON.parse(jsonString);
    var door = data.RfReceived.Data;
    if (door === null) door = UNDEF;
    if (door == '2BC00A') door = 'OPEN';
    if (door == '2BC00E') door = 'CLOSED';
    return door;
})(input

anybody?

Any errors in the log?
Is the JS transformation installed?

No errors on log at all. Had to double check that the mqtt was working! Yeah all transformations services are installed. Do I need to add the rf bridge itself as a item or thing?

Yes, the RF bridge must be added as a things file (along with your mqtt broker)

You also need items file for all your channels.

Hi Vincent,
I now have a Kerui D026 which gives out 4 distinct RF codes, not just for open and close like the previous one:
abcdEE open
abcdE7 close
abcdEB tamper
abcdEF low battery

Using your JS transformation example does not work anymore as I guess its not 1 code or the other. Can you help me with the right transformations/rules to get this contact sensor functional in OH?

A lot of people are starting using the Kerui D026 door sensors. I am currently implementing 6 sensors.

So, i tried using this

Type contact : kerui01		[ stateTopic="tele/WIFIRF/RESULT", 			transformationPattern="JSONPATH:$.RfReceived.Data", open="64D72E", closed="64D727" ]	

which would be ideal.

On the log it gives

2019-05-10 21:19:15.765 [WARN ] [eneric.internal.generic.ChannelState] - Command ‘64D72E’ not supported by type ‘OpenCloseValue’: No enum constant org.eclipse.smarthome.core.library.types.OpenClosedType.64D72E

Why do we need the javascript transformation ?

Well, i just found it

Channel Type “contact”, “switch”

  • on : A optional number (like 1, 10) or a string (like “ON”/“Open”) that is recognized as on/open state.
  • off : A optional number (like 0, -10) or a string (like “OFF”/“Close”) that is recognized as off/closed state.

The contact channel by default recognizes "OPEN" and "CLOSED" . You can connect this channel to a Contact item. The switch channel by default recognizes "ON" and "OFF" . You can connect this channel to a Switch item.

If on and off are not configured it publishes the strings mentioned before respectively.

You can connect this channel to a Contact or Switch item

It seems that the contact item uses the on/off settings as the switch item.

So, this works!

	Type contact : kerui01		[ stateTopic="tele/WIFIRF/RESULT", 			transformationPattern="JSONPATH:$.RfReceived.Data", on="64D72E", off="64D727" ]	

No need for the classic rules example!

Hi Nick,
Are you using MQTT 2.4 binding for your example?

How did you cater for the battery and tamper RF data?

thanks!

This is for the 2.4 mqtt binding.
This way can’t control the other 2 signal codes. For tampering and battery you should use the rules file.
Btw, how can i test the low battery function without an empty battery ? :slight_smile:

HI Nick
If you look at the 4 RF codes typically given out by the kerui d026 eg.
abcdeE open
abcde7 closed
abcdeB tamper
abcdeF low battery

almost certainly, the last characters F is for low battery, whilst B is for tamper

1 Like

Nick,
Would you have an example of the rules file for the battery and tamper data for each sensor?

thanks

no, i haven’t implemented the battery and tamper notifications yet

Hey @Taffy,
I know it’s been a long time since you’ve posted that, but just in case you still haven’t figure this out…

Change your sitemap from this:

Text item=Sonoff_Bridge_Front_Door
Text item=Sonoff_Bridge_Rear_Door

To this:

Default item=Sonoff_Bridge_Front_Door
Default item=Sonoff_Bridge_Rear_Door

As for the transformation js, you should use 2 separate files, one for each door contact.
You should also be careful with your syntax. If you look more closely to the last line of the code, you forgot to close the last parenthesis.

So, use the one bellow to make 2 files, “FrontDoor.js” and “RearDoor.js” and just change the RF codes accordingly to match to your devices codes.

(function(jsonString) {
var data = JSON.parse(jsonString);
var door = data.RfReceived.Data;
if (door === null) door = UNDEF;
if (door == ‘2BC00A’) door = ‘OPEN’;
if (door == ‘2BC00E’) door = ‘CLOSED’;
return door;
})(input)

Hope this helps your situation :slight_smile:

I dont believe this will work if you have more than 1 sensor for the Kerui D026, as it also has 4 different RF outputs

Thanks for your reply. I have sorted out the correct setup now but I appreciate your input :grinning:

Hey guys,

Following the method @vzorglub is mentioning above I stumbled into a problem and I would really appreciate any advice you have to offer.

The JS transformation solution is working fine, but after adding a second JS transformation file along with the proper channel and items, I get this warning even though everything is still working fine:

[WARN ] [eneric.internal.generic.ChannelState] - Command ‘317F0A’ not supported by type ‘OpenCloseValue’: No enum constant org.eclipse.smarthome.core.library.types.OpenClosedType.317F0A

After some digging, I discovered what is causing the error !

Every RF code received by the RF Bridge is being forwarded for every single channel (things file) to all the JS transformation files (as many as the physical contacts).
The warning occurs because each transformation file includes only 2 RF codes (open/closed) for a specific device. So any unknown RF code to that specific JS transformation file is not transforming and remains a string (eg. ‘F2670E’) which gives the warning message.

I’m really struggling to find a solution for some time now, but so far no luck…

This is my setup:

home.items

Contact LivingRoom_Window       "Living Room Window [%s]"        <window>    (gLivingRoom, gWindow, MagicMirror) [ "ContactSensor" ] { channel="mqtt:topic:mosquitto:rf_bridge:livingroomwindow" }
Contact Office_Window           "Office Window [%s]"             <window>    (gOffice, gWindow, gMagicMirror)    [ "ContactSensor" ] { channel="mqtt:topic:mosquitto:rf_bridge:officewindow" }

home.things

Type contact:livingroomwindow "Living Room Window"   [ stateTopic="tele/rf_bridge/RESULT", transformationPattern="JS:livingroom_window.js" ]
Type contact:officewindow     "Office Window"        [ stateTopic="tele/rf_bridge/RESULT", transformationPattern="JS:office_window.js" ]

livingroom_window.js

(function(jsonString) {
    var data = JSON.parse(jsonString);
    var window = data.RfReceived.Data;
    if (window === null) window = UNDEF;
    if (window == '317F0A') window = 'OPEN';
    if (window == '317F0E') window = 'CLOSED';
    return window;
})(input)

officewindow_window.js

(function(jsonString) {
    var data = JSON.parse(jsonString);
    var window = data.RfReceived.Data;
    if (window === null) window = UNDEF;
    if (window == '31910A') window = 'OPEN';
    if (window == '31910E') window = 'CLOSED';
    return window;
})(input)

In my case, RF Code ‘317F0A’ is not included in “officewindow.js” (but only in “livingroom_window.js”) and that returns the string and not a stare which is causing the warning.

Any help would be appreciated :slight_smile:
Thank you in advance !

This looks like a similar wheel being invented, by rule instead of transform

Thanks for your reply!
Indeed, quite similar situation. With this solution though I’m loosing the “contact” thing and start using the “string” instead. I’m just wild guessing but there might be another “hybrid” way through the channel or thing configuration, to filter out and send out relevant (included) RF codes to every JS.

What do you think?

Don’t think there is a clean way for a transform to say “hey I got some incoming data on this channel but you can’t have any”. (Which is what your WARN means)

Not sure what you see the problem with String as. The general technique there is to pull all messages into a working String, then analyze for magic codewords and postUpdate whatever Item of whichever yype you wish.

I see what you mean.
Actually the reason I initially turned my things to from string to contact was because I was trying to interrogate with google home and apple’s homekit but with no luck.
I mean the lights and switches are all working fine as well as temperature and humidity readings but not the magnetic contacts or motion sensors.
As you can see in my initial post I use the [ “ContactSensor” ] tag as described by the documentation but it’s not working despite my thing is a contact.

I guest it’s time for me to abandon this approach and set up every RF as string along with the proper json transformation. :sweat_smile: