Collection Q&A post: Let variables VAR and INT

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Domosapiens
Normal user
Posts: 307
Joined: 06 Nov 2016, 13:45

Collection Q&A post: Let variables VAR and INT

#1 Post by Domosapiens » 24 Sep 2019, 16:45

@TD-er, @grovkillen ... others
TD-er wrote: 15 Sep 2019, 14:40 So I will also add it for references to [Var#n] types which I believe will make the rules quite a bit more powerful.
It will be included in PR https://github.com/letscontrolit/ESPEasy/pull/2617
Seen this post:
Rules: Clock#Time - possible to compare with a variable?
viewtopic.php?f=6&t=6957&p=39254&hilit= ... ion#p39254
and this ReadTheDocs PR:
https://github.com/letscontrolit/ESPEas ... 0e857f50be

Rules are enhanced with INT variables. INT is added in build 20190916.
P.R. states:
The variables set with the Let command will be available in rules as VAR#N or INT#N where N is 1..16.
N.B. INT and VAR use the same variable, only INT does round them to 0 decimals.
The combination of those 2 sentences ..... ????
Does that mean that the sum of variable mix is 16 maximum or ...... #16 Int plus #16 Float?


And how does it work ?
Let,1,10 ......means variable 1 is an Integer 10?
Let,2,10.0 ....means variable 2 is a Float 10.0 ?


If so ... is there a type conversion ?
Let,1,[VAR#2] ....is variable 1 still an Integer?


If so ... trunc or round:
Let,3,10.7 ....means variable 3 is a Float.
Let,1,[VAR#3] ....does Int variable 1 equals 10 or 11?


Let,4,[DS18B20#Temperature] ...does variable 4 get the same number of decimals as defined in the DS18B20 task?

So above variable 4 is a Float.
Is this a correct conversion from Float to Int:
IF [INT#4] <> 0
Or do I need to use
IF [VAR#4] <> 0.0


What is preferred from a performance point of view: DummyTasks or Variables ?

Hope this post can become a Q&A on Let variables VAR and INT
Thanks for help :?
30+ ESP units for production and test. Ranging from control of heating equipment, flow sensing, floor temp sensing, energy calculation, floor thermostat, water usage, to an interactive "fun box" for my grandson. Mainly Wemos D1.

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Collection Q&A post: Let variables VAR and INT

#2 Post by TD-er » 24 Sep 2019, 23:56

First... it has not yet been merged. (but will be soon)
And the int and var do share the same value.
The int option was introduced to make it more clear how these values are rounded.

In C++, when running something like this:

Code: Select all

float floatvar = -0.123;
int intvar = static_cast<int>(floatvar);
What will be the value of intvar? 0? -1?
That's confusing for a lot of users without a lot of programming experience.

And what about this one:

Code: Select all

float floatvar = 0.475;
if (floatvar == (475.0/1000.0)) { ... }
Will the compare in the if-statement be true or false?
I have seen a lot of experienced programmers make the mistake of using == on floating point variables.
But that's exactly what we are somewhat forcing users to do in rules.

So the int version of the variable is just cast to int, but then using the normal round operator.
The idea is that it will result in more predictable results in rules.

This means you can store some result in variable #N and either read it as a float, or as an int.
Internal it is stored as a float, but I was thinking of maybe even storing it as a String.
Then you could store just about anything in it and let the rules cast it to whatever you need.
But that's not there yet (the String part)
Maybe I could first convert it to double, so you're not throwing away too many decimals and can also store 32 bit unsigned long in there, but then it does make a difference when you store something in a variable or in a dummy var.

N.B. a float has roughly 6 - 7 decimals resolution (23 bit). Double has 15 - 16 decimals resolution. (52 bit)

Domosapiens
Normal user
Posts: 307
Joined: 06 Nov 2016, 13:45

Re: Collection Q&A post: Let variables VAR and INT

#3 Post by Domosapiens » 26 Sep 2019, 00:22

Thanks a lot TD-er.
int and var do share the same value
Understood

So the int version of the variable is just cast to int
Understood


Somewhere in my notes (written by grovkillen ?)
Exporting as a string ....
The percentage variable %v16% is a string while the square brackets [VAR#16] is a float value
A String or Float, what to use for a LCD?

Casting to Int:
Nice to see how a temperature can be used to control a PWM (example without boundary check!)
Let,4,[DS18B20#Temperature]
PWM,13,[INT#4]

From a robustness POV.... DummyTasks do survive a soft-reboot
Variables don't survive (AFAIK) ????

From a performance POV:
What is preferred from a performance point of view: DummyTasks or Variables ?
:?:
30+ ESP units for production and test. Ranging from control of heating equipment, flow sensing, floor temp sensing, energy calculation, floor thermostat, water usage, to an interactive "fun box" for my grandson. Mainly Wemos D1.

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: Collection Q&A post: Let variables VAR and INT

#4 Post by grovkillen » 26 Sep 2019, 06:32

What I mean with %v16% is treated like a string is that it's the type of syntax that is preferred when using displayed output. Thus the syntax that you should use when not computing it.

BUT that being said, you MUST use the %vN% syntax in formula since that's how the formula "engine" is built. Formula cannot use the [task#value] syntax.

I hope it's clear? :)
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

TD-er
Core team member
Posts: 8643
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Collection Q&A post: Let variables VAR and INT

#5 Post by TD-er » 26 Sep 2019, 10:40

I can't say what will be giving the best performance, dummy or variables.
I would expect the variable option is the fastest, since storing in a dummy will also trigger an event.

When processing rules, all values will be converted to some kind of string object internal.
The first step of rules parsing is to replace all occurrences of [...] with their string representation.
That's also how I differentiate between "var" and "int". The "int" conversion is using 0 decimals :)

Dummy values (as with any other output value of any task) are kept in RTC memory and restored on boot.
This means they can indeed survive a crash or warm reboot.
Variables are stored only in RAM, which is lost at reboot.

Post Reply

Who is online

Users browsing this forum: No registered users and 29 guests