Transform mqtt reading

Hi There

is there a way to directly transform a mqtt reading so that it changes the itemtype.

Example:

I read a number from mqtt that represents a battery level. My goal is to create a switch that is switched on if the level is bwlow a defined value.

My mqtt item is

Number GF_LR_MiFloraDypsisLutescens_BatteryLevel "Batterie [%d %%]" <plant> (GF_LR_MiFloraDypsisLutescens) {mqtt="<[igor:miflora/Goldfruchtpalme:state:JSONPATH($.battery)]"}

How to change this to get a switch???

Thomas

You will have to use a JS transform and put your if statements in JavaScript code to return ON when it is in the ON range and OFF otherwise. Luckily it is JSON which is easy to parse in JavaScript.

Here is an example one I use to convert the UV number from Wunderground to a set of Strings.

(function(i) {

    if(i <= 2) return "Low"
    else if(i <= 3) return "Moderate"
    else if(i <= 7) return "High"
    else if(i <= 10) return "Very High"
    else return "Extreme"


})(input)
// input variable contains data passed by openhab

Thanks for the advice. Today i found time to implement this.

Thomas