How to convert MQTT into command?

openhab2
mqtt
binding
Tags: #<Tag:0x00007fe0519167f0> #<Tag:0x00007fe0519166b0> #<Tag:0x00007fe051916548>

(Ronald Brak) #1

Hi all,

This is my first attempt to get my vera and OH2 to work together.

I managed to get Mosquitto working, OH2 MQTT Binding and MQTT Action installed and everything is communicating with the broker.

So, mqtt from vera to broker works and mqtt from OH2 to the broker is working.

The question is: how can I use the message from VERA so that OH2 flips a virtual switch?

The message that is received from VERA by the broker (jarves) looks like:
{“DeviceId”:74,“DeviceName”:“Fornuis”,“DeviceType”:“urn:schemas-upnp-org:device:BinaryLight:1”,“OldStatus”:0,“RoomId”:1,“RoomName”:“Keuken”,“ServiceId”:“urn:upnp-org:serviceId:SwitchPower1”,“Status”:1,“Time”:1519561129,“Variable”:“Status”}

i have been looking for a solution in the pas 36 hours or so but can’t seem to find it.

A working example for the .items file is very much appreciated. :slight_smile:


(Rich Koshak) #2

Any reason you are not using the Mios binding?

From the MQTT README:

Item myItem {mqtt="<direction>[<broker>:<topic>:<type>:<transformer>:<regex_filter>], <direction>[<broker>:<topic>:<type>:<transformation>], ..."}

This is an inbound Item so:

direction This is always “<” for inbound messages.

The <broker> is the name of your broker in mqtt.cfg
The <topic> is the name of the topic

Since you want to send a command to the switch per the MQTT README,

type Describes what the message content contains: a status update or command. Allowed values are ‘state’ or ‘command’.

so you want to use command.

To make this easier we won’t use a transformation and create a separate config for each of ON and OFF.

The <regex_filer> needs to be a regular expression that only matches for the message that message. Something like .*\"DeviceName\":\"Fornuis\".*\"Status\"\:1.

Putting it together you have

mqtt="<[mybroker:my/topic:command:ON:.*\"DeviceName\":\"Fornuis\".*\"Status\"\:1], <[mybroker:my/topic:command:OFF:.*\"DeviceName\":\"Fornuis\".*\"Status\"\:0]

There are examples all over the place, these two are the most relevant:



(Ronald Brak) #3

Hi Rich,
Thanx for your reply.

the reason for not using the Mios binding is that I would like to couple multiple locations together.
In the future i want to add my work place for instance and there is no Vera but a different system.
So to be able to communicate with all the different setups, i thought it would be best to use 1 type of communication for everything.

back on topic: I have seen a lot of examples, but just can’t seem to get it to work.
With your example I put together the switch as follow:

Switch Fornuis { mqtt="<[jarves:Vera/Events/Fornuis:command:ON:.*\"DeviceName\":\"Fornuis\".*\"Status\"\:1], <[jarves:Vera/Events/Fornuis:command:OFF:.*\"DeviceName\":\"Fornuis\".*\"Status\"\:0]" }

my broker = jarves
the topic = Vera/Events/Fornuis

When I save the .items file the following error appears in the logs:

2018-02-26 22:21:02.609 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘jarves.items’ has errors, therefore ignoring it: [48,23]: mismatched character ‘:’ expecting set null
[48,106]: mismatched input ‘1’ expecting RULE_STRING
[48,107]: mismatched input ‘]’ expecting RULE_ID
[48,118]: extraneous input ‘:’ expecting RULE_ID
[48,154]: mismatched character ‘:’ expecting set null
[48,195]: mismatched character ‘’ expecting ‘"’

As far as I understand this error, it complains about this part : *“Status”**:**1], and that’s every time I try to put together this item.
So that’s why I got lost, just can’t figure out what’s wrong.


(Josar) #4

Try

Switch Fornuis { mqtt="<[jarves:Vera/Events/Fornuis:command:ON:REGEX(s/.*\"Status\":(.?).*/$1/g)], <[jarves:Vera/Events/Fornuis:command:OFF:REGEX(s/.*\"Status\":(.?).*/$0/g)]" }

But you have to install the regex transformation!


(Ronald Brak) #5

Thank you,

Installed regex transformation and copied the item.

Error:

Binding configuration of type ‘mqtt’ of item ‘Fornuis’ could not be parsed correctly.
org.eclipse.smarthome.model.item.BindingConfigParseException: Configuration ‘jarves:Vera/Events/Fornuis:command:ON:REGEX(s/.“Status”:(.?)./$1/g)’ is not a valid inbound configuration: Configuration requires 4 or 5 parameters separated by ‘:’

Then I got the idea of using the MAP function.
I created /etc/openhab2/transform/jarves_vera.map :


"Status":1=ON
"Status":0=OFF

And the item:

Switch Fornuis { mqtt="<[jarves:Vera/Events/Fornuis:command:ON:MAP(jarves_vera.map)], <[jarves:Vera/Events/Fornuis:command:OFF:MAP(jarves_vera.map)]"}

This time no errors, but unfortunately also no actions.
The switch is not moving at all.


(Rich Koshak) #6

The whole point of OH is to bridge between different technologies and to coordinate and make them work together. Use the Mios binding to talk to your Vera and whatever binding is appropriate to talk to what you will have at work.

You might need to escape that : as well. Also, with matching part of the binding config (i.e. the fifth field), if you have anything there it assumes REGEX as a regular expression is the only thing supported in the fifth field.

 Switch Fornuis { mqtt="<[jarves:Vera/Events/Fornuis:command:ON:.*\"Fornuis\".*\"Status\"\:1.*]" }   

The MQTT binding is pretty picky with :, and { } characters. The “:” may even need to be double escaped. I’ve also added a .* to match the rest of the message which was missing from before.

Anyway, the above means, in words: Match on any message sent to Vera/Events/Fornuis on the jarves broker and set the Switch to ON with a command for any message that contains the words "Fornuis" and "Status":1.

The error you are getting is because the binding parser is treating the “:” as a field separator and we want to keep it as part of the regular expression.

We could probably avoid all of this escaping with:

     Switch Fornuis { mqtt="<[jarves:Vera/Events/Fornuis:command:ON:.*Fornuis.*Status..1.*]" }

As long as the message doesn’t vary widely from the example this should work just as well and it sidesteps the problem with the “:” and also lets us avoid escaping the “”.

Unless you plan on putting the entire JSON string that gets sent to you into the .map file that won’t work. You need to extract the Status field from the full JSON first and since you can’t apply more than one transformation at a time that is why I suggested to use the REGEX match which is essentially doing a map for you. Any message that matches the regex gets mapped to ON.


Broadlink Mqtt binding config
(Ronald Brak) #7

Thanx for all the help!

Eventualy I’ve got it working with this line.

Switch Fornuis {mqtt="<[jarves:Vera/Events/Fornuis:command:ON:.*Fornuis.*\"Status..1.*],<[jarves:Vera/Events/Fornuis:command:OFF:.*Fornuis.*\"Status..0.*]" }