Homey Community Forum

Variable suddenly has a lot of decimals?!

I’m using two variables with the Virtual Devices App, where I have a virtual thermostat. The virtual thermostats reads thermometers in each room, and can change a variable for setting the temperature in the room.

This has worked flawlessly for a long time, but it seems like something has changed with a later Homey update. The temperature has been at 19 for ages, and my better half wanted to up it to 20 yesterday. We’ve done this before without any issues. The virtual thermostat basically changes with 0.5 in increments, and I’ve set the flow to react 3 seconds later if changed. Otherwise it will go up/down reacting to temperature change in the room.

So over to my issue; For some stupid reason the variable wasn’t set to 20 when increased, but had a bunch of decimals, which seemed to create problems for the virtual thermostats or the flows, so it didn’t react when I tried to change the temperature, and the wall heaters which are controlled by the virtual thermostats never turned off.

I had to manually edit the variables to a round number, and then I could increase/decrease again - everything working as normal.

What happened?

Both variables got the same exact number.

Search for “round”, many topics with solutions!

But I don’t see why it wouldn’t happen in the first place? I never saw this issue before the update recently?

Homey does the calculation with FoatingPoint, becaus of this, sometimes 1+1 is not exactly 2 but fi 1,999999999999 , there is your problem rising, so I recoment to use the “round” functiion always where the number of decimals is important.

This bug has been around for quite some time (at least since Homey 1.5.x) and (to my personal annoyance) not resolved yet. There are multiple workarounds to circumvent this though (as @JPe4619 mentioned :slight_smile: )

Wow, that’s messed up for a simple variable management IMHO.

So where would I set the “round” function?

This is triggered when the virtual device changes the target temperature.
Bold is the variable.

It isn’t actually a Homey bug, it is a general Javascript bug (the coding language Homey uses) that has been there ever since the language was developed.

Seriously? :rofl:
I used to make stuff in JavaScript back in the days, but I never ran into that personally. (( not saying I don’t believe you by the way. I’m just surprised ))

Just Google “Javascript Float Precision” you’ll find millions of results with “how to handle” and “why does it happen” :stuck_out_tongue_closed_eyes:.

Rather than ‘setting’ the variable, try ‘calculating’ the variable with curly brackets. This should probably be enough to circumvent it.

{{ Target temperature * 100 / 100 }}

But how would I then set the actual variable?
I could have 1 decimal btw, as the virtual thermostat uses 0.5C increments.

That depends. If you want your variable to he a hard ‘0.5’, then just set it to ‘0.5’.
If you want your variable to be incremented by 0.25, you can do it like so:

{{ (variable + 0.25) * 100 / 100 }}

Edit, added comment: Very dirty workaround, but multiplying and dividing by 100 is blunt enough to circumvent the issue. Maybe 10 also works. I always use 100.

Ok. So this is an alternative to the “round” function? Because I could round up to nearest 0.5C if that is an option.

What I don’t get is that I wasn’t actually doing any math. I just changed the variable from 19 to 20 but through the virtual thermostat. It works now when I go up/down again, but I just don’t want this issue to happen again in the future.

But I’m still a bit confused where I’d put this calculation? “Lille Soverom Temperatur” means “small bedroom temperature” and is the variable I created which I want to be set. “Target temperature” is the tag from the virtual thermostat.

But this would only be if I increase step by step, what I’m actually doing is letting the virtual thermostat change the variable. But for some reason setting it to “20” instead gave me that 19.9999*. So that would probably mean that the virtual thermostat device is doing some math, and not correcting it. I guess the rounding should preferably be done inside the app?

So this should work (removing the first one though):

I’m not familiar with a ‘built-in’ round function. Maybe someone else knows.
You could probably do something in the ‘Better Logic’ which applies the MathJS libraries (so you could make some calculation with rounding and multiplying by 2 and deviding by two, which effectively would always give you a figure that is multitude of 0.5.

In regards to it doing no math. I think you are right that the virtual thermostat actually does do math (adding or substracting the amount you changed). But I have no way to be sure :slight_smile: Could check the code of course.

And I agree with you, but the rounding should be done inside Homey if you ask me. Given the fact that Javascript is the core-issue, the amount of decimals should be an attribute of each variable in my oppinion. But complaining about that won’t resolve your issue in the short term.

I would look into the workaround I gave you. See if it works (so you won’t need an extra app for it). Since the error is so incredibly small (more than 5th decimal), it is VERY unlikely that would actually need to round to 0.5 by hand.

Yup, thanks! It seems to do what it should now (as it did before as well though). I’ll keep an eye on it the next few days, and try changing the thermostat now and then.

something like this wil work:
{{round (Energiekosten vandaag,2)}}

the 2 means 2 digits after the ,

in your case {{round(Target temperature,1)}}

That’s nice to know :slight_smile:
Do you know of any documentation/feature list of the math-function of the Logic app?

11

this will round on 0.5 in a Homey variable.
A-NUMBER01 is the name of the variable in this case, but it can be 2 different variables as well.

1 Like