Comparing two float variables in dzVents

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
arkoko
Posts: 34
Joined: Saturday 17 December 2016 12:13
Target OS: NAS (Synology & others)
Domoticz version: Beta
Location: Estonia
Contact:

Comparing two float variables in dzVents

Post 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?
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Comparing two float variables in dzVents

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
arkoko
Posts: 34
Joined: Saturday 17 December 2016 12:13
Target OS: NAS (Synology & others)
Domoticz version: Beta
Location: Estonia
Contact:

Re: Comparing two float variables in dzVents

Post 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 ***
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Comparing two float variables in dzVents

Post by dannybloe »

Mm.. guess that the value could be nil. You'll have to check it before passing it to the round function.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
arkoko
Posts: 34
Joined: Saturday 17 December 2016 12:13
Target OS: NAS (Synology & others)
Domoticz version: Beta
Location: Estonia
Contact:

Re: Comparing two float variables in dzVents

Post 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
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Comparing two float variables in dzVents

Post 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.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
arkoko
Posts: 34
Joined: Saturday 17 December 2016 12:13
Target OS: NAS (Synology & others)
Domoticz version: Beta
Location: Estonia
Contact:

Re: Comparing two float variables in dzVents

Post by arkoko »

Oeh, I just noticed this typo as well.
Thank you for your effort and sorry for the noise :)
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 1 guest