Enhance round function in dzVents/runtime/Utils.lua Topic is solved

Use this forum to discuss possible implementation of a new feature before opening a ticket.
A developer shall edit the topic title with "[xxx]" where xxx is the id of the accompanying tracker id.
Duplicate posts about the same id. +1 posts are not allowed.

Moderators: leecollings, remb0

Post Reply
Pjedr
Posts: 38
Joined: Friday 27 December 2013 3:13
Target OS: Linux
Domoticz version: Beta
Location: Friesland
Contact:

Enhance round function in dzVents/runtime/Utils.lua

Post by Pjedr »

Hello folks,

I just converted half of my lua scripts to dzVents scripts.
In the domoticz.utils.round I mis the option to use negative decimals as a way to round on the left side of the indented point.

-1 should round 3333.333 to 3330
-2 should round 3333.333 to 3300
-3 should round 3333.333 to 3000
etcetera

My testresults:
2020-07-30 12:30:00.613 Status: dzVents: Info: Test 333.333 dz.utils.round sGroep2_WattRnd=333
2020-07-30 12:30:00.613 Status: dzVents: Info: Test 333.333 dz.utils.fRound sGroep2_WattRnd=300
2020-07-30 12:30:00.613 Status: dzVents: Info: Debug script_time: Rounding number: 333.333 with idp -2 for Groep2_Watt

Current code:

Code: Select all

function self.round(value, decimals)
	local nVal = tonumber(value)
	local nDec = ( decimals == nil and 0 ) or tonumber(decimals)
	if nVal >= 0 and nDec > 0 then
		return math.floor( (nVal * 10 ^ nDec) + 0.5) / (10 ^ nDec)
	elseif nVal >=0 then
		return math.floor(nVal + 0.5)
	elseif nDec and nDec > 0 then
		return math.ceil ( (nVal * 10 ^ nDec) - 0.5) / (10 ^ nDec)
	else
		return math.ceil(nVal - 0.5)
	end
end
Suggested code enhancement:

Code: Select all

function self.round(value, idp)
	local nVal=tonumber(value)
	local idp=tonumber(idp)
	local mult = 10^(idp or 0) --Round number to identent point
	if nVal >= 0 then
		roundednum=math.floor(nVal * mult + 0.5) / mult
	else
		roundednum=math.ceil(nVal * mult - 0.5) / mult
	end
	local str = tostring(roundednum)
	if str:sub(-2) == ".0" then str=str:sub(1,-3) end
	return tonumber(str)
end
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Enhance round function in dzVents/runtime/Utils.lua

Post by waaren »

Pjedr wrote: Thursday 30 July 2020 12:49 In the domoticz.utils.round I mis the option to use negative decimals as a way to round on the left side of the indented point.
I like the idea. What is your use cases in domoticz?

When I test the new function with this script

Code: Select all

return
{
    on =
    {
        devices =
        {
            'roundTrigger',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -
        marker = 'round Trigger',
    },

    
    execute = function(dz)
    
        dz.log('------------------------')
        dz.log('12.23  : round = >> ' .. dz.utils.round(12.23)  ..', newRound = >> ' .. dz.utils.nRound(12.23)  ,dz.LOG_DEBUG)
		dz.log('-12.23 : round = >> ' .. dz.utils.round(-12.23) ..', newRound = >> ' .. dz.utils.nRound(-12,23) ,dz.LOG_DEBUG)
        
        dz.log('------------------------')
        dz.log('12.23,1  : round = >> ' .. dz.utils.round(12.23,1)  ..', newRound = >> ' .. dz.utils.nRound(12.23,1)  ,dz.LOG_DEBUG)
		dz.log('-12.23,1 : round = >> ' .. dz.utils.round(-12.23,1) ..', newRound = >> ' .. dz.utils.nRound(-12,23,1) ,dz.LOG_DEBUG)
        
        dz.log('------------------------')
        dz.log('12.03,1  : round = >> ' .. dz.utils.round(12.03,1)  ..', newRound = >> ' .. dz.utils.nRound(12.03,1)  ,dz.LOG_DEBUG)
		dz.log('-12.03,1 : round = >> ' .. dz.utils.round(-12.03,1) ..', newRound = >> ' .. dz.utils.nRound(-12,03,1) ,dz.LOG_DEBUG)
        
        dz.log('------------------------')
        dz.log('1223.03,-1  : round = >> ' .. dz.utils.round(1223.03,-1)  ..', newRound = >> ' .. dz.utils.nRound(1223.03,-1)  ,dz.LOG_DEBUG)
		dz.log('-1223.03,-1 : round = >> ' .. dz.utils.round(-1223.03,-1) ..', newRound = >> ' .. dz.utils.nRound(-1223,03,-1) ,dz.LOG_DEBUG)
	end
}
I get

Code: Select all

2020-07-30 13:44:16.192  Status: dzVents: Info: round Trigger: ------------------------
2020-07-30 13:44:16.192  Status: dzVents: Debug: round Trigger: 12.23  : round = >> 12, newRound = >> 12            OK
2020-07-30 13:44:16.192  Status: dzVents: Debug: round Trigger: -12.23 : round = >> -12, newRound = >> -12          OK
2020-07-30 13:44:16.192  Status: dzVents: Info: round Trigger: ------------------------
2020-07-30 13:44:16.192  Status: dzVents: Debug: round Trigger: 12.23,1  : round = >> 12.2, newRound = >> 12.2      OK
2020-07-30 13:44:16.192  Status: dzVents: Debug: round Trigger: -12.23,1 : round = >> -12.2, newRound = >> -12      NOK 
2020-07-30 13:44:16.192  Status: dzVents: Info: round Trigger: ------------------------
2020-07-30 13:44:16.192  Status: dzVents: Debug: round Trigger: 12.03,1  : round = >> 12.0, newRound = >> 12        OK (converted from float to int)
2020-07-30 13:44:16.192  Status: dzVents: Debug: round Trigger: -12.03,1 : round = >> -12.0, newRound = >> -12      OK (converted from float to int)
2020-07-30 13:44:16.192  Status: dzVents: Info: round Trigger: ------------------------
2020-07-30 13:44:16.192  Status: dzVents: Debug: round Trigger: 1223.03,-1  : round = >> 1223, newRound = >> 1220   OK
2020-07-30 13:44:16.192  Status: dzVents: Debug: round Trigger: -1223.03,-1 : round = >> -1223, newRound = >> -1223 NOK
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Pjedr
Posts: 38
Joined: Friday 27 December 2013 3:13
Target OS: Linux
Domoticz version: Beta
Location: Friesland
Contact:

Re: Enhance round function in dzEvents/runtime/Utils.lua

Post by Pjedr »

My use case is that I have a text to voice function which uses free voicerss
#VoiceRSS 350 free per day http://www.voicerss.org/personel/

So I need to limit the calls per day.
I do do that by keeping the generated mp3 is in a cache.
So I only get to voicerss when a line of tekst is not already in the cache.

By rounding the numbers for use in text, i need less mp3.
For example power Watts ranging from 1 to 1000 is 1000 calls.
When rounding to two digits I only need 10 calls (10 steps of 100 = 1000)

No I try to also ad nearest 50 in it.
Then numbers can be rounded rounded to 50 100 150 200 ....1000 = 20 steps.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Enhance round function in dzVents/runtime/Utils.lua

Post by waaren »

Pjedr wrote: Thursday 30 July 2020 16:51 My use case is that I have a text to voice function which uses free voicerss
No I try to also ad nearest 50 in it.
Then numbers can be rounded rounded to 50 100 150 200 ....1000 = 20 steps.
Thx for the explanation. Probably best to make this a separate script- or helper function as I don't anticipate many dzVents user are going to need this.
I am sure they wil let me know if they do :)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Enhance round function in dzEvents/runtime/Utils.lua

Post by waaren »

Pjedr wrote: Thursday 30 July 2020 16:51 Now I try to also ad nearest 50 in it.
This function seem to work.

Code: Select all

local function blockRound(value, blockSize)
	return math.floor(value + blockSize / 2 - (( value + blockSize / 2 )  % blockSize ))
end
testSuite

Code: Select all

print('blockRound(17, 10) ==> 20 : ' .. blockRound(17, 10 )) 
print('blockRound(12, 10) ==> 10) : ' .. blockRound(12 , 10 ))
print('blockRound(15, 10) ==> 20) : ' .. blockRound(15 , 10 ))
print('blockRound(-15, 10) ==>  -10 : ' .. blockRound(-15 , 10 ))
print('blockRound(-17, 10) ==> -20 : ' .. blockRound(-17 , 10 ))
print('blockRound(-12, 10) ==> -10 : ' .. blockRound(-12 , 10 ))
print('blockRound(1230, 100) ==> 1200 : ' .. blockRound(1230 , 100 ))
print('blockRound(1230, 1000) ==> 1000 : ' .. blockRound(1230 , 1000 ))
print('blockRound(1270, 500) ==> 1500 : ' .. blockRound(1230 , 500 ))
print('blockRound(1270, 50) ==> 1250 : ' .. blockRound(1270 , 50 ))
print('blockRound(1270, 30) ==>  1260 : ' .. blockRound(1270 , 30 ))
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Pjedr
Posts: 38
Joined: Friday 27 December 2013 3:13
Target OS: Linux
Domoticz version: Beta
Location: Friesland
Contact:

Re: Enhance round function in dzVents/runtime/Utils.lua

Post by Pjedr »

Ok, thanks, i will create my own helper functions in the global_data.lua so it is not overwritten when upgrading.
In the meanwhile I also made a solution with flexible nearest numbers for example 5 20 25 33 50 75 200 500

Code: Select all

function self.fNearest(value, nearest)
	local nVal=tonumber(value) bNearest = 0 local nDec=0
	local nNea = ( nearest == nil and 0 ) or tonumber(nearest)
	print('Utils: '..nVal..'|'..nNea)
	if nNea then
		if nNea > 0 then bNearest=1 end
    	if bNearest == 1 then nVal=nVal/nNea print('nVal/nNea='..nVal) end
	end
	local mult = 10^(nDec or 0) --Round number to identent point
	if nVal >= 0 then
		roundednum=math.floor(nVal * mult + 0.5) / mult
	else
		roundednum=math.ceil(nVal * mult - 0.5) / mult
	end
	print('nDec=roundednum: '..nDec..'='..roundednum)
	if bNearest == 1 then
		roundednum=roundednum*nearest print('roundednum*nNea='..roundednum)
	end
	local str = tostring(roundednum)
	if str:sub(-2) == ".0" then str=str:sub(1,-3) end
	return tonumber(str)
end
Spoiler: show

Code: Select all

2020-07-30 22:40:00.523  Status: dzVents: Utils: 3333324.3336662|5
2020-07-30 22:40:00.523  Status: dzVents: nVal/nNea=666664.86673325
2020-07-30 22:40:00.523  Status: dzVents: nDec=roundednum: 0=666665.0
2020-07-30 22:40:00.523  Status: dzVents: roundednum*nNea=3333325.0
2020-07-30 22:40:00.523  Status: dzVents: Info: Test 3333324.3336662 dz.utils.fRound sGroep2_WattRnd=3333325
2020-07-30 22:40:00.523  Status: dzVents: Utils: 3333324.3336662|20
2020-07-30 22:40:00.523  Status: dzVents: nVal/nNea=166666.21668331
2020-07-30 22:40:00.523  Status: dzVents: nDec=roundednum: 0=166666.0
2020-07-30 22:40:00.523  Status: dzVents: roundednum*nNea=3333320.0
2020-07-30 22:40:00.523  Status: dzVents: Info: Test 3333324.3336662 dz.utils.fRound sGroep2_WattRnd=3333320
2020-07-30 22:40:00.523  Status: dzVents: Utils: 3333324.3336662|25
2020-07-30 22:40:00.523  Status: dzVents: nVal/nNea=133332.97334665
2020-07-30 22:40:00.523  Status: dzVents: nDec=roundednum: 0=133333.0
2020-07-30 22:40:00.523  Status: dzVents: roundednum*nNea=3333325.0
2020-07-30 22:40:00.523  Status: dzVents: Info: Test 3333324.3336662 dz.utils.fRound sGroep2_WattRnd=3333325
2020-07-30 22:40:00.523  Status: dzVents: Utils: 3333324.3336662|33
2020-07-30 22:40:00.523  Status: dzVents: nVal/nNea=101009.82829292
2020-07-30 22:40:00.523  Status: dzVents: nDec=roundednum: 0=101010.0
2020-07-30 22:40:00.523  Status: dzVents: roundednum*nNea=3333330.0
2020-07-30 22:40:00.523  Status: dzVents: Info: Test 3333324.3336662 dz.utils.fRound sGroep2_WattRnd=3333330
2020-07-30 22:40:00.523  Status: dzVents: Utils: 3333324.3336662|50
2020-07-30 22:40:00.523  Status: dzVents: nVal/nNea=66666.486673325
2020-07-30 22:40:00.523  Status: dzVents: nDec=roundednum: 0=66666.0
2020-07-30 22:40:00.523  Status: dzVents: roundednum*nNea=3333300.0
2020-07-30 22:40:00.523  Status: dzVents: Info: Test 3333324.3336662 dz.utils.fRound sGroep2_WattRnd=3333300
2020-07-30 22:40:00.523  Status: dzVents: Utils: 3333324.3336662|75
2020-07-30 22:40:00.523  Status: dzVents: nVal/nNea=44444.324448883
2020-07-30 22:40:00.523  Status: dzVents: nDec=roundednum: 0=44444.0
2020-07-30 22:40:00.523  Status: dzVents: roundednum*nNea=3333300.0
2020-07-30 22:40:00.523  Status: dzVents: Info: Test 3333324.3336662 dz.utils.fRound sGroep2_WattRnd=3333300
2020-07-30 22:40:00.524  Status: dzVents: Utils: 3333324.3336662|200
2020-07-30 22:40:00.524  Status: dzVents: nVal/nNea=16666.621668331
2020-07-30 22:40:00.524  Status: dzVents: nDec=roundednum: 0=16667.0
2020-07-30 22:40:00.524  Status: dzVents: roundednum*nNea=3333400.0
2020-07-30 22:40:00.524  Status: dzVents: Info: Test 3333324.3336662 dz.utils.fRound sGroep2_WattRnd=3333400
2020-07-30 22:40:00.524  Status: dzVents: Utils: 3333324.3336662|250
2020-07-30 22:40:00.524  Status: dzVents: nVal/nNea=13333.297334665
2020-07-30 22:40:00.524  Status: dzVents: nDec=roundednum: 0=13333.0
2020-07-30 22:40:00.524  Status: dzVents: roundednum*nNea=3333250.0
2020-07-30 22:40:00.524  Status: dzVents: Info: Test 3333324.3336662 dz.utils.fRound sGroep2_WattRnd=3333250
2020-07-30 22:40:00.524  Status: dzVents: Utils: 3333324.3336662|333
2020-07-30 22:40:00.524  Status: dzVents: nVal/nNea=10009.982983983
2020-07-30 22:40:00.524  Status: dzVents: nDec=roundednum: 0=10010.0
2020-07-30 22:40:00.524  Status: dzVents: roundednum*nNea=3333330.0
2020-07-30 22:40:00.524  Status: dzVents: Info: Test 3333324.3336662 dz.utils.fRound sGroep2_WattRnd=3333330
2020-07-30 22:40:00.524  Status: dzVents: Utils: 3333324.3336662|500
2020-07-30 22:40:00.524  Status: dzVents: nVal/nNea=6666.6486673325
2020-07-30 22:40:00.524  Status: dzVents: nDec=roundednum: 0=6667.0
2020-07-30 22:40:00.524  Status: dzVents: roundednum*nNea=3333500.0
2020-07-30 22:40:00.524  Status: dzVents: Info: Test 3333324.3336662 dz.utils.fRound sGroep2_WattRnd=3333500
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Enhance round function in dzVents/runtime/Utils.lua

Post by waaren »

Pjedr wrote: Thursday 30 July 2020 22:54 Ok, thanks, i will create my own helper functions in the global_data.lua so it is not overwritten when upgrading.
In the meanwhile I also made a solution with flexible nearest numbers for example 5 20 25 33 50 75 200 500
I seem to get the same results with this one

Code: Select all

local function blockRound(value, blockSize)
	return math.floor(value + blockSize / 2 - (( value + blockSize / 2 )  % blockSize ))
end
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest