MQTT: Generic thing

Is it possible to have one switch item linked up to recieve values from two mqtt paths - one representring on and another for off? T

Button presses come in on the following two endpoints with a ‘value’ of 0 for a simple press.

zwave/switchxxxx/central_scene/endpoint_0/scene/001

zwave/switchxxxx/central_scene/endpoint_0/scene/002

I know how to connect if they would both send on the same tree structure but am I able to hook it up where the item listens to all sub topics of zwave/switchxxxx/central_scene/endpoint_0/scene/ and then process the rest via a transform?

Thank you!

Sure. A single Item can have any number of Channels. However, sending out commands to this Item become a little trickier as the ON/OFF commands will go to both Channels.

You can do this too with a wild card subscription. In your case you’d subscribe to zwave/switch/xxxx/central_scene/endpoint_0/+. The + is a wild card meaning all topics under endpoint_0 but not the topics under those (i.e. just a single level). # means all subrtopics recursively.

Given

foo/bar/baz/biz
foo/bar/bim/bap

subscribing to foo/bar/+ will subscribe to messages posted to foo/bar/baz and foo/bar/bim but subscribing to foo/bar/# will subscribe to all messages posted to foo/bar/baz, foo/bar/baz/biz, foo/bar/bim and foo/bar/bim/bap.

Unfortunately, in MQTT, I don’t know if the source topic of the message is provided to the transformations. So it would only really work correctly if it’s a different message posted to each topic and you can tell by the message which topic it came from.

To make it work if that’s not the case, you will need to create an event Channel on the MQTT Broker Thing with the wild card subscription. Then trigger a rule when any message comes in on that Channel. event.event (or what ever is correct for the rules language you are using to get the triggering event for a Thing event triggered rule) will contain topic#message which you can easily split apart and process as appropriate. See the MQTT EventBus rule template for a JS example.

You could do something like this in a transformation:

See #3.

You would then up with a single item that produces 1.0 or 2.0. Of course that isn’t a switch (probably a string or a number depending on how you set up the channel), but you could do what you want in a rule.

I don’t understand that logic… Why would it go down only one level for foo/bar/baz, but two levels (and not just one) for foo/bar/bim/bap? Or did you mean foo/bar/bim/?

Thanks for all of the help everyone! for completeness I figured I’d reply with what I ended up with. I did 2 different channels on the thing - one for on and one for off. Then linked them both with the item. That way I could have the on command listen to the ON state topic and off listen to OFF. For this switch the payload is the same regardless - no way to tell the difference from that alone.

Type switch:Switch_BikeON "Switch Bike ON" [stateTopic = "zwave/Switch_Bike/central_scene/endpoint_0/scene/001", transformationPattern = "JSONPATH:$.value", on = "0"]

Type switch:Switch_BikeOFF "Switch Bike OFF" [stateTopic = "zwave/Switch_Bike/central_scene/endpoint_0/scene/002", transformationPattern = "JSONPATH:$.value", off = "0"]

This. Autocorrect is the bane of my existence. + goes down only one level, # goes shown all the levels.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.