MQTT and regex filter

Hello, I tried several times to use regex filter for incomming mqtt messages, but without any success.

Please, could you help me? Im interesting only for the messages “Connection Lost”, everything else I want to ignore.

Switch Heating_switch “Infra panel” [ “Switchable” ]
{ mqtt="<[broker:/sonoff_1/status/LWT:state:MAP(switch_status.map):REGEX(.Lost.)]" }

Thank you,
Juraj

REGEX(Lost$)

or

REGEX(Connection Lost)

Hi, thank you, but this is not working. :frowning:

Do you get any error messages in your log?

No, there is nothing in the log.

After using REGEX filter, the input string (Connected / Connection Lost) is fully ignored and no transform was made.

I am at work now.
There are some regex validators online.

REGEX(.*Connection Lost.*)

The REGEX must match the full message. .* matches 0 or more of any characters.

Just using . matches 0 or 1 character. In particular, the message may have two characters at the end if it uses Windows style new lines.

Hello Rich, thank you for the answer, but this is not working too. :frowning:

This is my items file (without regex filter everything is working, but I have warning messages in log file for Connected event, which i want to filter:

Switch Heating_switch “Infra panel” [ “Switchable” ]
{ mqtt=">[broker:/sonoff_1/gpio/12:command:ON:1],
>[broker:/sonoff_1/gpio/12:command:OFF:0],
<[broker:/sonoff_1/status:state:MAP(switch_status.map)],
<[broker:/sonoff_1/Relay/Switch:state:MAP(switch_status.map)],
<[broker:/sonoff_1/status/LWT:state:MAP(switch_status.map):REGEX(.*Connection Lost.*)]" }

and switch_status.map:

Connection\ Lost=OFF
0=OFF
1=ON
Switch:\ ON=ON
Switch:\ OFF=OFF

There is no error in the log file.

OS Openhabian
Openhab version 2.3.0~20180331153334-1
binding-mqtt1 - 1.12.0.SNAPSHOT

If there is any whitespace before or after “Connection Lost” then your switch_status.map will not be able to map it to OFF.

But why not just tell it to go to OFF without the map?

<[broker:/sonoff_1/status/LWT:state:OFF:REGEX(.*Connection Lost.*)]

“Connection Lost” always means OFF so there is no need to map.

1 Like

I tried this, but still without success :-(.
The map is working correctly, until I use the regex_filter.

I also tried to use only “Lost” string (in case of white space) and manually publish LWT message Lost via MQTT.fx, but this is not working too.

Doh! You don’t use “REGEX( )” for the message matching. You can only use regular expressions so the REGEX is redundant.

This example from the docs:

Number humidity "humidity [%.1f%%]" {mqtt="<[broker:weatherstation/readings:state:JS(convertPercent.js):humidity=.*]"}

will match all messages that start with “humidity=”.

So you need:

<[broker:/sonoff_1/status/LWT:state:OFF:Connection Lost]

If there is a chance of there being leading or trailing whitespace use

<[broker:/sonoff_1/status/LWT:state:OFF:.*Connection Lost.*]

Thank you Rich, this one is working great :-ň

<[broker:/sonoff_1/status/LWT:state:OFF:Connection Lost]

Doh! indeed…
That proves it again
Read the docs, again and again and again…

Thanks

1 Like