Page 1 of 1

Comparing two float variables in dzVents

Posted: Friday 27 October 2017 21:51
by arkoko
Hi!
I need to compare two float variable with different round. For example values 18 and 21.50. Direct comparing does not work:

Code: Select all

if (myCurrSetPoint ~= domoticz.variables('NightTemp').value)

I found workaround to get it working:

Code: Select all

if (myCurrSetPoint*100 ~= domoticz.variables('NightTemp').value*100)
But i doubt that this is optimal solution. Does more elegant way exists in dzVents script?

Re: Comparing two float variables in dzVents

Posted: Sunday 29 October 2017 12:24
by dannybloe
That's not a dzVents thingy but a Lua thingy I'm affraid. I guess rounding is the best way as floats in Lua are a bit weird to compare indeed. You can use

Code: Select all

if domoticz.round(myCurrentSetPoint, 1) ~- domoticz.round(domoticz.variables('NightTemp').value, 1) then
But I'm not sure if that works.

Re: Comparing two float variables in dzVents

Posted: Tuesday 31 October 2017 11:43
by arkoko
Thank you for your help. I assume that in your code ~- shoul be as ~=, but with if it didn't work:

Code: Select all

if ((domoticz.round(myCurrentSetPoint, 1)) ~= (domoticz.round(domoticz.variables('NightTemp').value, 1))) then

Code: Select all

2017-10-31 12:31:00.307  Error: dzVents: Error: An error occured when calling event handler round_test
2017-10-31 12:31:00.307  Error: dzVents: Error: ...z/var/scripts/dzVents/../../dzVents/runtime/Domoticz.lua:243: attempt to perform arithmetic on local 'x' (a nil value)
Writing to log seems working:

Code: Select all

 domoticz.log('*** Start night SetPoint: ' .. domoticz.round(myCurrSetPoint, 1) .. 'C -> ' .. domoticz.round(domoticz.variables('devolo1NightTemp').value, 1) .. 'C ***')

Code: Select all

2017-10-31 12:20:00.261  dzVents: Info:  *** Start night SetPoint: 20C -> 16C ***

Re: Comparing two float variables in dzVents

Posted: Tuesday 31 October 2017 11:54
by dannybloe
Mm.. guess that the value could be nil. You'll have to check it before passing it to the round function.

Re: Comparing two float variables in dzVents

Posted: Tuesday 31 October 2017 12:52
by arkoko
OK, it could, but in my case it's not.
Some details. Code part:

Code: Select all

domoticz.log('*** Original values: ' .. myCurrSetPoint .. ' -> ' .. domoticz.variables('devolo1NightTemp').value .. ' ***')
domoticz.log('*** Rounded  values: ' .. domoticz.round(myCurrSetPoint, 1) .. ' -> ' .. domoticz.round(domoticz.variables('devolo1NightTemp').value, 1) .. ' ***')
if ((domoticz.round(myCurrentSetPoint, 1)) ~= (domoticz.round(domoticz.variables('NightTemp').value, 1))) then
And log:

Code: Select all

2017-10-31 13:48:00.339  dzVents: Info:  ------ Start internal script: round_test:, trigger: at 13:48
2017-10-31 13:48:00.364  dzVents: Info:  *** Original values: 20.00 -> 16.1 ***
2017-10-31 13:48:00.364  dzVents: Info:  *** Rounded  values: 20 -> 16.1 ***
2017-10-31 13:48:00.365  Error: dzVents: Error: An error occured when calling event handler round_test
2017-10-31 13:48:00.365  Error: dzVents: Error: ...z/var/scripts/dzVents/../../dzVents/runtime/Domoticz.lua:243: attempt to perform arithmetic on local 'x' (a nil value)
2017-10-31 13:48:00.365  dzVents: Info:  ------ Finished round_test

Re: Comparing two float variables in dzVents

Posted: Tuesday 31 October 2017 12:59
by dannybloe
devolo1NightTemp ~= NightTemp
It's not the same. Clearly, NightTemp is empty. That x from the error is exactly the value that you pass in the round function.

Re: Comparing two float variables in dzVents

Posted: Tuesday 31 October 2017 13:09
by arkoko
Oeh, I just noticed this typo as well.
Thank you for your effort and sorry for the noise :)