Add value to item state with JS

Hi there,

i´ve build myself a little dashboard for fuel prices around me.
I can get the normal prices from Clever Tanken while i get some of the premium prices directly from the API of the fuel station.
While i get the full price to the third decimal place from the fuel stations APIs, i only get two decimal places from Clever Tanken, as they use two different places for price and subprice.
I recently learned how to modify values with JS transformation and wanted to use this to add 0.009 to the values i receive from Clever Tanken.
This works fine for some of the values but some have a strange behavior.

Transformation file called add009.js

(function(i) {
    return parseFloat(i) + 0.009;

Channel configuration in the http binding
REGEX:.*<span id="current-price-4">(.*)<\/span>.*<sup id="suffix-price-4">.*∩JS:add009.js

And here are some outputs of the log:

Item 'ctJetSuper' changed from 1.869 to 1.819
Item 'ctShellSuper' changed from 1.879 to 1.869
Item 'ctShellVPower' changed from 2.129 to 2.1189999999999998
Item 'ctJetSuperPlus' changed from 1.9089999999999998 to 1.849
Item 'ctJetSuper' changed from 1.819 to 1.759
Item 'ctJetSuperPlus' changed from 1.849 to 1.839
Item 'ctJetSuper' changed from 1.759 to 1.7489999999999999

It´s not critical because i fixed the decimal places in Grafana to 3 and it display the correct values.

I still would like to understand what is causing this issue and if it can be resolved.

kind regards

Floating point number precision ?

1 Like

Thanks for that, good to know.

So it´s part of the transformation Javascript (IEE 754 floating point representation) and a normal result.

I just changed the transformation file to Number instead of parseFloat and that´s does the trick.

(function(i) {
    return Number(i) + 0.009;

The Offset Profile was made for this purpose. Items | openHAB

Sometimes that can be easier or more straight forward to use.

It’s part of programming in general and you will see it any time you are using a programming language that uses IEE 754 floating point representation (almost all of them) which cannot represent every single floating point number exactly.

I don’t want anyone thinking that this is unique to just openHAB JS transformations.

1 Like

Thanks Rich, i haven´t looked at the offset profile yet.
I corrected my statement above.

For my example this would mean i need to add the offset profile like this?

{channel="http:url:tanken:GasStation1" [profile="system:offset", offset=+0.09]}

There´s currently no example for this profile in the docs.

I believe so. I only to UI Items so can’t say for sure. I don’t think the + is required.

VS Code already tells me that + is wrong but it also doesn´t work without :slight_smile:

{channel="http:url:tanken:GasStation1" [profile="system:offset", offset=0.09]}

Item 'ctAralUltimate' changed from 2.029 to 2.02

Who could know the correct syntax for text based configuration of the profiles?

The syntax looks reasonable. But note that your JS transform is adding 0.009 and the offset is adding 0.09.

Yeah that was a typo but it´s not working anyway.
I already tried a negative value like mentioned in the docs and the value stays the same.