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:
Thing mqtt:topic:mosquitto:davis "Davis VantagePro2" (mqtt:broker:mosquitto) {
Channels:
Type number : insidetemp "Binnentemperatuur" [ stateTopic="/davis/wflexp.json", transformationPattern="JSONPATH:$.tempin" ]
}
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,
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?
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:
Number InsideTemp "Binnentemperatuur [%.1f °C]" <temperature> (gTemperatuur, gDavisTemperature)
Then set up a rule to
convert this string into a number
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… )
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. )
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
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 …
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?
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 …