[SOLVED] Divide item value by 100

hi there. I’m having a bit of trouble formatting an item value to be divided by 100.

From searching on here I think how it’s set below should work, but the value isn’t being processed by the javascript.

items file

Number	Aqara_1_temp		"Aqara 1 Temperature [JS(divideBy100.js):%.2f ]"	<temperature>	(gAqara)		{ http="<[http://192.168.1.104:8080/api/A8587BBD47/sensors/2:10000:JSONPATH($.state.temperature)]" }

transform/divideBy100.js file

(function(i) {
    return parseFloat(i) / 100;
})(input)

What am I doing wrong?

I’ve not been able to find the solution. Thanks in advance.

Errors in the log? What is shown on the sitemap? What is the state of Aquara_1_temp?

There’s no errors in the log, it appears to load the item file OK.

The site map displays the same value as the raw JSON data does, with added “.00” formatting as stated in the item file, eg 2271.00 and 2271 respectively.

(it’s a temperature value, and i’m trying to move the decimal point to the right place for displaying)

It looks like it should work. I’m not sure what could be wrong.

Have tried it without the parseFloat? just

(function(i) {
    return i/100;
})(input)

yeah, i’ve tried lots of different things including returning a fixed number value, and the js file doesn’t appear to be doing anything.

Just to try it. CReate a string item with the same data source and what is displayed there

String Aqara_1_temp "Aqara 1 Temperature [%s ]" <temperature> (gAqara) { http="<[http://192.168.1.104:8080/api/A8587BBD47/sensors/2:10000:JSONPATH($.state.temperature)]" }

The JS transform returns a string not a number.
So your item definition is almost correct
Just change the label formating:

Number	Aqara_1_temp		"Aqara 1 Temperature [JS(divideBy100.js):%s ]"	<temperature>	(gAqara)		{ http="<[http://192.168.1.104:8080/api/A8587BBD47/sensors/2:10000:JSONPATH($.state.temperature)]" }

Better still would be to use only one transform:

transform

(function(i) {
    var data = JSON.parse(i);
   return parseFloat(data.state.temperature) / 100;
})(input)

item

Number	Aqara_1_temp		"Aqara 1 Temperature [%.1f]"	<temperature>	(gAqara)		{ http="<[http://192.168.1.104:8080/api/A8587BBD47/sensors/2:10000:JS(divideBy100.js)]" }

In this case the transform will still return a String but the binding will parse it into a Number automatically and you don’t need an second transformation for the label

hi Vincent

thanks, but the first of those has no effect (apart from removing the “.00”), and the 2nd causes basicUI to not display (I didn’t investigate further).

hi Thomas

thanks, but has no effect. I still get a very big number, and no decimal places.

It may seem a silly question but is the JS transform installed?

that was a very very good question at a very silly person! Thank you! :slight_smile:

And no, it wasn’t. Now that it is, it’s working with my original version.

I might have missed “install the JS transform” in the tens of topics I read before starting this one… but I don’t think I did. I don’t think it was mentioned in any of them.

1 Like

How about profiles?

profile=“offset” already exists. You would need a profile=“gain”, so you could divide the value before it arrives at the item.

Does anyone know if there is already the possibility to solve this with profiles?

I don’t think so. But that would be a great feature request.

The answer is “yes but”. If the HTTP binding were a 2.x version binding then we could use profiles to solve this. Each of the transforms are supported as a Profile so the JS transform Vincent posted would work as a Profile. But Profiles are only supported with Channel Links. The HTTP binding doesn’t have Channels so Profiles are not available.

One other limitation of Profiles is it only processes incoming data. You can’t use a Profile to transform data you send out. For that, until there is a mechanism like Profiles for sending out messages to devices we will need to use Rules for that if the binding doesn’t natively support transforms.

I’m having the same issue trying to divide a value by 100 before feeding into my item.

divide_by_100.js

(function(i) {
   return parseFloat(i) / 100;
})(input)

thing

    Bridge poller input1 [ start=13082, length=3, refresh=1000, type="input" ] {
         Thing data batt_voltage    [ readStart="13082", readValueType="uint16" ]
    }

item

Number pv_battery_voltage   "voltage [%.2f]V"   <power>  ["power"]       { channel="modbus:data:solar_charger:input1:batt_voltage:number" [profile="transform:JS", function="divide_by_100", sourceFormat="%d"]}

I already tried a few things but I can’t get it to work. Any help is appreciated a lot!

function="divide_by_100.js",

but I think you may run into this problem, I’m not at all convinced JS profiles work with Number Items yet.

but as you’e using Modbus, you could apply the transform in your data Thing, it’s in the docs.

Thing data batt_voltage    [ readStart="13082", readValueType="uint16", readTransform="JS(divide_by_100.js)" ]
Thing data batt_voltage    [ readStart="13082", readValueType="uint16", readTransform="JS(divide_by_100.js)" ]

This solves my problem! Thx a lot!