# \[SOLVED\] MQTT value conversions within channel (REGEX)

**URL:** <https://community.openhab.org/t/solved-mqtt-value-conversions-within-channel-regex/71676>\
**Category:** Beginners\
**Created:** [April 3, 2019, 9:13pm UTC](https://community.openhab.org/t/solved-mqtt-value-conversions-within-channel-regex/71676 "2019-04-03T21:13:16Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![posixx](https://community.openhab.org/user_avatar/community.openhab.org/posixx/32/35988_2.png) [@posixx](https://community.openhab.org/u/posixx)\
**Post date:** [April 3, 2019, 9:13pm UTC](https://community.openhab.org/t/solved-mqtt-value-conversions-within-channel-regex/71676/1 "2019-04-03T21:13:16Z")

</div>

Hello OpenHAB enthousiasts,

I’m struggling for some time now understanding how to be able to transform values retrieved from a mqtt topic to usefull measurement values. For instance: i have bought a WifiLogger for use in my Davis VantagePRO2 weatherstation. I get the MQTT messages in, and am able to get the values i want. But; as the WifiLogger is only able to present temperature values in Fahrenheit i want to convert these values to read as Celsius. S far i get the value like this:

```csv
Thing mqtt:topic:mosquitto:davis "Davis VantagePro2" (mqtt:broker:mosquitto) {
    Channels:
        Type number : insidetemp "Binnentemperatuur" [stateTopic="/davis/wflexp.json", transformationPattern="JSONPATH:$.tempin"]
}

```

in my items file i have the following item:

```csv
Number:Temperature InsideTemp "Binnentemperatuur [%.1f °C]" <temperature> (gTemperatuur,gDavisTemperature) { channel="mqtt:topic:mosquitto:davis:insidetemp" }

```

So my item reads 71.4 °C instead of 21.9 °C.

I really hope someone can explain how to explicit tell openhab the value it receives from the mqtt tansform is in fahrenheit so afterwards it is converted automatically,

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [April 3, 2019, 9:49pm UTC](https://community.openhab.org/t/solved-mqtt-value-conversions-within-channel-regex/71676/2 "2019-04-03T21:49:41Z")

</div>

When MQTT feeds the value to the `Number:Temperature` Item, you would need a means to tell OH your value is in °F, as opposed to °C or K etc.  
Basically your transformation needs to add °F on the end of the numeric value, but a JSONPATH cannot do that on its own I think?

A chained transform should do - a REGEX perhaps, adding °F or | °F to the string?

---

<div class="post-metadata">

**Author:** ![posixx](https://community.openhab.org/user_avatar/community.openhab.org/posixx/32/35988_2.png) [@posixx](https://community.openhab.org/u/posixx)\
**Post date:** [April 3, 2019, 9:59pm UTC](https://community.openhab.org/t/solved-mqtt-value-conversions-within-channel-regex/71676/3 "2019-04-03T21:59:57Z")

</div>

Do you happen to have some sample code how to do that?

---

<div class="post-metadata">

**Author:** ![H102](https://community.openhab.org/user_avatar/community.openhab.org/h102/32/25595_2.png) [@H102](https://community.openhab.org/u/H102)\
**Post date:** [April 3, 2019, 11:45pm UTC](https://community.openhab.org/t/solved-mqtt-value-conversions-within-channel-regex/71676/4 "2019-04-03T23:45:37Z")

</div>

If you change Number: Temperature to just Number, dropping the UOM, will it work as needed?

If not try a forum search on “formatBeforePublish” and see if that could work for what you need.

---

<div class="post-metadata">

**Author:** ![rrichard](https://community.openhab.org/user_avatar/community.openhab.org/rrichard/32/32059_2.png) [@rrichard](https://community.openhab.org/u/rrichard)\
**Post date:** [April 4, 2019, 7:54am UTC](https://community.openhab.org/t/solved-mqtt-value-conversions-within-channel-regex/71676/5 "2019-04-04T07:54:08Z")

</div>

store the outcome of the json transformation in a **(proxy) item**, beware it is a string:

```csv
String insidetemp_mqttstring "Binnentemperatuur [%s]" { channel="mqtt:topic:mosquitto:davis:insidetemp" }

```

You can check this string in a sitemap. It should show your value in Fahrenheit.

Prepare a number item to store the outcome after the following transformation by rule below:

```csv
Number InsideTemp "Binnentemperatuur [%.1f °C]" <temperature> (gTemperatuur, gDavisTemperature)

```

Then set up a **rule** to

1. convert this string into a number
2. convert to degree celsius and store in a number item.

(Why not using UoM: i didn’t get it up running. It would be cool, because it would save you the conversations formula. But… )

```csv
rule "Convert MQTT Temperature String to Number and convert unit"
when
    Item insidetemp_mqttstring received update
then    
    var Number temp = Integer::parseInt(insidetemp_mqttstring.state.toString)
    InsideTemp.postUpdate((temp - 32) * 5/9) // Please check formula and syntax, i did not checked it!
end

```

Hope it works!

_(Perhaps someone can show us, how to use this UoM system. )_

---

<div class="post-metadata">

**Author:** ![posixx](https://community.openhab.org/user_avatar/community.openhab.org/posixx/32/35988_2.png) [@posixx](https://community.openhab.org/u/posixx)\
**Post date:** [April 4, 2019, 8:59am UTC](https://community.openhab.org/t/solved-mqtt-value-conversions-within-channel-regex/71676/6 "2019-04-04T08:59:35Z")

</div>

tnx Richard, but i don’t want to use rules and extra items to achieve this. I have a lot of values which need conversion for this MQTT topic (mph to km/h. Fahrenheit to Celcius, inch to hPa) so i want to use the UoM system as that’s where it’s designed for. I just need a way to be able to tell openhab what the QuantityType of a received value is.. Basically; all values received from my davis channel are imperial values; i need metric values

---

<div class="post-metadata">

**Author:** ![rrichard](https://community.openhab.org/user_avatar/community.openhab.org/rrichard/32/32059_2.png) [@rrichard](https://community.openhab.org/u/rrichard)\
**Post date:** [April 4, 2019, 9:07am UTC](https://community.openhab.org/t/solved-mqtt-value-conversions-within-channel-regex/71676/7 "2019-04-04T09:07:37Z")

</div>

i’m also not happy about these rules and also have many sensors.  
Somewhere i read:

“do not use js transformation - they are consuming cpu time”  
“in mqtt 2.5 there will be a transformation …”  
“use channel binding and profiles … it is so easy”  
“use node-red”  
and so on.

I use some of my rules also for publishing values to other bus system (e.g KNX). So i have full controll and can use numbers in rules … for making my home … smart. 🙂

It is a common task: read in MQTT messages and convert it universally …

---

<div class="post-metadata">

**Author:** ![posixx](https://community.openhab.org/user_avatar/community.openhab.org/posixx/32/35988_2.png) [@posixx](https://community.openhab.org/u/posixx)\
**Post date:** [April 4, 2019, 10:24am UTC](https://community.openhab.org/t/solved-mqtt-value-conversions-within-channel-regex/71676/8 "2019-04-04T10:24:01Z")

</div>

Guys,

I maybe have another option: the wifilogger sends a textfile every 10 seconds beside the json output. This is the format/content:

04/04/19 12:18:40 8.0 74 3.6 1.3 1.6 162 0.00 0.00 1000.3 SSE 1 km/h C mb mm — — 0.20 167.00 — 23.9 40 7.8 — 8.3 12:09 2.4 5:18 9.7 11:15 9.7 11:15 1000.6 0:12 998.3 5:31 2.16 2.16 4.8 7.8 — 0.9 0.01 278 — 0.00 0 0 0 0 0 0 0 0 0 0

The good news; all values are metric, so i can use them without conversion. But now i need to get the correct value from this string for each channel i configured. For instance, the 3th value is outside temperature. So how can i get this value through transformationPattern? JS is an option but, as many clearly mentioned, these consume a lot of resources. Any better ideas?

---

<div class="post-metadata">

**Author:** ![rrichard](https://community.openhab.org/user_avatar/community.openhab.org/rrichard/32/32059_2.png) [@rrichard](https://community.openhab.org/u/rrichard)\
**Post date:** [April 4, 2019, 10:29am UTC](https://community.openhab.org/t/solved-mqtt-value-conversions-within-channel-regex/71676/9 "2019-04-04T10:29:11Z")

</div>

you can use “REGEX” transformation.

First step, always, try to put this information into a string item and see, if openHAB can read this string.  
Post your steps … and hope some specialist visit this thread here … 🙂

---

<div class="post-metadata">

**Author:** ![posixx](https://community.openhab.org/user_avatar/community.openhab.org/posixx/32/35988_2.png) [@posixx](https://community.openhab.org/u/posixx)\
**Post date:** [April 4, 2019, 1:04pm UTC](https://community.openhab.org/t/solved-mqtt-value-conversions-within-channel-regex/71676/10 "2019-04-04T13:04:29Z")

</div>

string is coming in ok, as seen in the log:

2019-04-04 15:02:07.853 [WARN] [eneric.internal.generic.ChannelState] - Incoming payload '04/04/19 15:02:10 9.2 70 4.0 1.6 1.6 96 0.00 0.00 1000.5 E 1 km/h C mb mm — — 0.20 167.00 — 24.1 39 8.9 — 9.3 13:07 2.4 5:18 12.9 14:54 12.9 14:54 1000.8 14:25 998.3 5:31 2.16 2.16 12.9 9.4 — 1.6 0.03 543 — 0.00 0 0 0 0 0 0 0 0 0 0 ’ not supported by type ‘NumberValue’

I changed the channel to:

```auto
Type number : insidetemp "Binnentemperatuur" [stateTopic="/davis/realtime.txt", transformationPattern="REGEX:(?:[^]*\\ ){2}([^]*)" ]

```

This should retrun the 3rd value as tested on [regex101: build, test, and debug regex](https://regex101.com/r/4acpDD/1)

Anyone sees what’s the problem?

---

<div class="post-metadata">

**Author:** ![posixx](https://community.openhab.org/user_avatar/community.openhab.org/posixx/32/35988_2.png) [@posixx](https://community.openhab.org/u/posixx)\
**Post date:** [April 4, 2019, 11:07pm UTC](https://community.openhab.org/t/solved-mqtt-value-conversions-within-channel-regex/71676/11 "2019-04-04T23:07:45Z")

</div>

Solved it. for the REGEX transformation to work the complete payload must be matched by the regex. So the channel looks like this now:

```csv
Type number : insidetemp "Binnentemperatuur" [stateTopic="/davis/realtime.txt", transformationPattern="REGEX:(?:[^]*\\ ){22}([^]*).*" ]

```

Now i have the correct values for all measurements.
