How to filter mqtt messages

I have channel that listens to mqtt messages. I only want it triggered if the message has certain things in it.

Messages look like either of these;

{"page": {"format": 1, "id": 10, "type": "sync"}}
{"page": {"format": 1, "id": 10, "type": "refresh"}}

I have a rule which I want to process only on refresh and visa-vera.

I have a channel with an Outgoing Value Transformation of

REGEX:(.*page.*refresh.*)

I assumed this would only set the linked item, when it was a match, not not where it wasn’t
but I see the “sync” messages too, in this item.

Maybe I’m not even doing it the right way! :slight_smile:

What I’m trying to d is have a rule which only is triggered by the refresh type, and another rule only triggered by the sync type.

Advise?

“Outgoing” is the clue that it applies to outgoing messages, i.e. those that openHAB publishes to MQTT.

You want Incoming … as used for state updates.

Why not extract the ‘type’, and either -
put it in an Item state for a rule to examine when updates
or
use it trigger an event, with the type as event payload.

These events don’t represent state though, right? They represent a momentary event. So you probably shouldn’t be using a State Channel linked to an Item anyway. Instead use a Trigger Channel and trigger your rule from the Channel directly.

In that rule you can parse the JSON and do a “sync” or “refresh” using the transform Action (ignoring the rest) so you would only need the one rule.

Alternatively, if you did want to keep this as state, you could use a text Channel and a String Item and the REGEX chained to a JSONPATH to extract the type. Then you can trigger the rule when the Item receives an update to “sync” or “refresh”.

There are lots of ways to go about this sort of thing, the best depends on details not provided in the question.

Hi @rossko57 , I can’t extract the type because it would only pass that to the rule via the item then, wouldn’t it? I need all the other things in the message for using in the rule too. So i need the whole payload.

I’ll try the channel and 1 rule for now! :slight_smile: thanks!

@rlkoshak , no I don’t need state, it’s just an event to process really so the channel trigger might work, just never used one before! :slight_smile:

The reason I’d prefer it filtered to 2 rules is just to keep it separate for my readability. It might be ok to have one rule, as some code is common in each. It’s just they might be big and i think it might be easier if 2.

Create multiple channels and have for each part of the message that’s relevant to you an own item

1 Like

channel trigger seemed to work well for what I need. thanks