I’m 20 minutes in looking for an example, but can’t find one.
A MQTT device returns a payload that looks like this:
[22]
I can grab that value as a string, but need to strip the brackets and store it as a number.
Is there an easy way to do this in the MQTT thing with TransformationPatterns? I suspect it is regex but I can’t find an example…
duiffie
(Marco)
January 28, 2020, 2:21pm
2
something like this, as JS transformation?
(function(x){
return x.match(/\[([0-9]+)\]/)[1];
})(input)
Appreciate it! I’m not sure if I can apply the JSON transformation and the JS transformation to a channel in a MQTT thing, but I think I’ll just add the JSON parsing to this code and use a single transformation.
duiffie
(Marco)
January 28, 2020, 3:10pm
4
Mqtt things file:
Thing topic <name> "<name>" {
Channels:
Type <type> : <name> "<name>" [ stateTopic="<statetopic>", transformationPattern="JS:<transformationfile.js>" ]
}
Don’t forget to install the JS transformation service.
rossko57
(Rossko57)
January 28, 2020, 3:39pm
5
That part was hidden.
MQTT binding does allow a little magic; in the docs you will find chaining transformations.
neat… I’ll give this a try!
rlkoshak
(Rich Koshak)
January 28, 2020, 5:04pm
7
You can. See MQTT 2.5 M1+ How to implement the equivalent to MQTT1 REGEX filters for an example using PaperUI. You will need to find someone else to help translate that to .things files.
Also, a JS transform is way overkill for this. Just use REGEX:\[(.*)\]
(assuming there is no white space around it).