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

Just started with MQTT 2.4 and I have window contact sensors that send a code 1D280A when open, and 1D280E when closed to the Sonoff RF bridge. The RF bridge in my things file for received RF data is

 Type string : receiveddata  "Received Data"        [ stateTopic="tele/sonoff/bridge1/RESULT", transformationPattern="JSONPATH:$.RfReceived.Data"]

And my items file is

String Bridge1_Received_Data  "RF Bridge Received Data: [%s]"            <none>          (Bridge1) { channel="mqtt:topic:bridge1:receiveddata" }

How do i create a rule that reads the incoming data and shows in the Paper UI and sitemap that the window is open or closed? I have learnt the open and close codes to key 1 and 2 of the RF bridge but I suspect thats completely unnecessary for contact sensors like these?

Have you tried using MAP transformation in the sitemap file to transform the data to OPEN/CLOSED?

I have not actually put up a sitemap yet as I have not finalised its design. How would you do a MAP transformation in this instance? Do you mean a rule is not needed?

You don’t even need a map transform, use the binding:

Type string : receiveddata  "Received Data" [
    stateTopic="tele/sonoff/bridge1/RESULT",
    transformationPattern="JSONPATH:$.RfReceived.Data",
    on="1D280A",
    off="1D280E"
]

sorry, what do you mean by use the binding? in things, items?
This is only for 1 contact sensor, and I believe I can connect quite a number of sensors to each Sonoff RF bridge. How do I differentiate and label them?

In things: as I show above

You will need a rule for this

So where do I put this?

Type string : receiveddata  "Received Data" [
    stateTopic="tele/sonoff/bridge1/RESULT",
    transformationPattern="JSONPATH:$.RfReceived.Data",
    on="1D280A",
    off="1D280E"

That’s your thing as you posted above.
I only added the on and off parameters.

I must be an idiot. I still don’t see any changes in the paper UI after adding the on and off parameters. I must be omitting something else?

What do you see?

Exactly the same as before. Under the “RF bridge received data: 1D280E” when contact is closed and “RF bridge received data: 1D280A” when contact is open. All this under Paper UI

Maybe a silly question but do you have the jsonpath transformation service installed?

Yes Sir. jsonpath transformation service is installed

Just making sure.:wink:

Sometime after making a change to a file OH needs to stop and restart. You might want to try stopping and restart OH to see if that makes a difference.:crossed_fingers:

I had an inkling that it might not work.
It was worth a try.
You are going to need a JS transformation instead

I have no idea what js transformation means

Documentation, addons, transformations js or javascript

I have read the transformation services in the documentation. It seems like a rule is still applicable for my situation?

Ok,

Create a file called windows.js in the transformation folder with the following contents:

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

And change your thing to:

Type string : receiveddata  "Received Data" [
    stateTopic="tele/sonoff/bridge1/RESULT",
    transformationPattern="JS:windows.js"
]
2 Likes

Do you mean the transform folder under conf, or is the transformation folder somewhere else?
When I place the windows.js file in the transform folder(and changed the thing), I get this error

[WARN ] [l.generic.ChannelStateTransformation] - Transformation service JS for pattern windows.js not found!

And I get this entry in my paper UI

{"RfReceived":{"Sync":13940,"Low":460,"High":1350,"Data":"1D280E","RfKey":2}}