rswennen
(Rohnny Swennen)
December 26, 2022, 6:36pm
1
Why is my item configuration not working in MQTT ?
I have following under channels in the .thing file
Type string : bsblan_heating_circuit_1_operating_mode “Heating circuit 1 operating mode” [ stateTopic=“BSB-LAN/700”, transformationPattern=“REGEX: - (.*)” ]
And this is the item definition
String bsblan_heating_circuit_1_operating_mode “Heating circuit 1 operating mode” {channel=“mqtt:topic:synology:sensors:bsblan_heating_circuit_1_operating_mode”}
The MQTT input is 3 - Comfort
I’m trying to strip out the Comfort and have Comfort as value for the item.
What’s wrong with my REGEX in the channel definition ?
Try: (?<= - ).*
This catches everything after " - ", but without that, so the result would be “Comfort”
rswennen
(Rohnny Swennen)
December 27, 2022, 9:59am
3
@Oggerschummer
Thomas thanks a lot. I tried it in both the thing
Type string : bsblan_heating_circuit_1_operating_mode "Heating circuit 1 operating mode" [ stateTopic="BSB-LAN/700", transformationPattern="REGEX:(?<= - ).*" ]
as well as using it as a profile in the item configuration
String bsblan_compressor_toestand_item "Compressor toestand" (gBSBLan) {channel="mqtt:topic:synology:sensors:bsblan_compressor_toestand" [profile="transform:REGEX", function="(?<= - ).*"]}
and neither works. The item is either NULL or empty.
I tested your regex code in regex101 and it is indeed correct.
I’m a bit puzzled
With the UI config REGEX is working fine for me (I do no longer use .things-Files).
You may have run into this issue reported:
opened 10:18AM - 02 Sep 22 UTC
bug
When using REGEX Statements with the MQTT Binding in OH 3.3 to filter messages w… ith Arrays, the REGEX statement only works correct when defining the Thing via UI. When defining a Thing via textfile, no message is passed at all, despite the fiter REGEX espression looks identical to the one defined in the UI.
## Expected Behavior
When using REGEX Filters on MQTT Things which have been based on textual configurations, messages that match the REGEX filter should get passed through.
## Current Behavior
Only on MQTT Things defined via UI the REGEX filter works as expected. Text based MQTT things with REGEX Expressions don't pass messages at all.
I am using a MQTT Smartmeter Device that reads the power consumption from the grid. Unfortunately, the meter does provide only a single value per packet, still they are all using the same format:
```yaml
{"Time":"2022-08-29T18:44:01","Z1":{"76_7_0":-4.04}}
```
The value after Z1 changes, so 76_7_0 could be 1_8_1 or 2_8_0 f.e., hence I needed to implement the REGEX function.
Now, when defining the MQTT Thing in a text file, the expression looks like that:
```csv
Thing topic Zaehler @ "MQTT" {
Channels:
Type number : z1Zaehlerstand "Zählerstand" [stateTopic="tele/Zaehler/SENSOR", transformationPattern="REGEX:(.*1_8_0.*)∩JSONPATH:$.Z1.1_8_0"]
Type number : z1BezugTarif1 "Bezug Tarif 1" [stateTopic="tele/Zaehler/SENSOR", transformationPattern="REGEX:(.*1_8_1.*)∩JSONPATH:$.Z1.1_8_1"]
Type number : z1BezugTarif2 "Bezug Tarif 2" [stateTopic="tele/Zaehler/SENSOR", transformationPattern="REGEX:(.*1_8_2.*)∩JSONPATH:$.Z1.1_8_2"]
Type number : z1Einspeisung "Einspeisung" [stateTopic="tele/Zaehler/SENSOR", transformationPattern="REGEX:(.*2_8_0.*)∩JSONPATH:$.Z1.2_8_0"]
Type number : z1Leistung "Aktuelle Gesamtleistung" [stateTopic="tele/Zaehler/SENSOR", transformationPattern="REGEX:(.*16_7_0.*)∩JSONPATH:$.Z1.16_7_0"]
Type number : z1LeistungL1 "Aktuelle Leistung L1" [stateTopic="tele/Zaehler/SENSOR", transformationPattern="REGEX:(.*36_7_0.*)∩JSONPATH:$.Z1.36_7_0"]
Type number : z1LeistungL2 "Aktuelle Leistung L2" [stateTopic="tele/Zaehler/SENSOR", transformationPattern="REGEX:(.*56_7_0.*)∩JSONPATH:$.Z1.56_7_0"]
Type number : z1LeistungL3 "Aktuelle Leistung L3" [stateTopic="tele/Zaehler/SENSOR", transformationPattern="REGEX:(.*76_7_0.*)∩JSONPATH:$.Z1.76_7_0"]
}
```
Without the REGEX, the well know error is to be seen in the logs. With the REGEX, ALL outcome is filtered and no packet is passed through at all.
So I defined the things in the UI, using the same parameters:
```yaml
UID: mqtt:topic:6296256670:Zaehler
label: Generic MQTT Thing
thingTypeUID: mqtt:topic
configuration: {}
bridgeUID: mqtt:broker:jarvis
channels:
- id: z1Leistung
channelTypeUID: mqtt:number
label: Einspeisung / Bezug (Zähler)
description: ""
configuration:
stateTopic: tele/Zaehler/SENSOR
transformationPattern: REGEX:(.*16_7_0.*)∩JSONPATH:$.Z1.16_7_0
unit: W
- id: z1Zaehlerstand
channelTypeUID: mqtt:number
label: Zählerstand
description: ""
configuration:
stateTopic: tele/Zaehler/SENSOR
transformationPattern: REGEX:(.*1_8_0.*)∩JSONPATH:$.Z1.1_8_0
unit: Wh
- id: z1Einspeisung
channelTypeUID: mqtt:number
label: Einspeisung
description: ""
configuration:
stateTopic: tele/Zaehler/SENSOR
transformationPattern: REGEX:(.*2_8_0.*)∩JSONPATH:$.Z1.2_8_0
unit: Wh
```
Which worked like a charm.
## Steps to Reproduce (for Bugs)
Generate a MQTT Thing via text config and apply REGEX filters. Check outcome, most probably there will be no messages getting through. Do the same again, but this time define the Thing via UI and apply the same REGEX expressions to it. Should now pass messages that match the REGEX expression.
## Context
Now, call me old fashioned, but I would like to stay with the text files as they are more easy to work with for me. Backup and Migration are more easy as well. According to the documentation, this should work.
## Your Environment
Openhab 3.3 as VM, based on Debian and Openhabian-Script, running on Proxmox.
Just try to model one of these channels with a separate testing Thing in the UI - if it works you know the reason…
Try
.*\s\-\s(.*)
or
.*?([a-zA-Z]+)
to avoid spaces within your regular expression.
rswennen
(Rohnny Swennen)
December 27, 2022, 1:13pm
6
@anon71759204 thanks a lot almost there
The second one works. Apparently OH seems to have issues with spaces and with the backslashes in the first regex you provided as well.
The only issue left is the second regex splits every space which isn’t really delivering the desired result.
The input
5 - Charged, nominal temperature
return temperature rather than
Charged, nominal temperature
So I now only need to figure out a REGEX that provide the part after the - without leading space without using a backslash and without using a space in the regex.
rswennen
(Rohnny Swennen)
December 27, 2022, 1:42pm
8
that did the trick. I was confuse by RegEx - Transformation Services | openHAB but as there are examples but also there the text indeed mentions the need for escape.
One last question.
What’s the difference between
transformationPattern="REGEX:.*\\s-\\s(.*)"
and
transformationPattern="REGEX:.*\\s\\-\\s(.*)"
so the regex
.*\s\-\s(.*) and .*\s-\s(.*)
both work so wondering what the bacslash in front of the - does in the expression (just out of curiosity to better understand regex)
Thanks, that did not come to mind… Escaping is such an IT anachronism, but often required. I wonder if using the UI this can be prevented (Does the UI escape automatically when storing the REGEX statement?). Will have to try it.
- is a reserved character for range definitions (within […]). Escaping means: this is a literal hyphen, not a range definition. In your case it doesn’t matter because the - isn’t inside a […].