Page 1 of 1
Utility value to dummy percentage
Posted: Monday 18 February 2019 22:12
by Tarzan737
Hello..
is it possible to convert a value to percent..
if i get a value on my utility sensor from let´s say 0 - 270 but want to see that in percent instead..
like 270 is 100% and 0 is 0
is it any script for that to a dummy sensor? or any solution
Re: Utility value to dummy percentage
Posted: Monday 18 February 2019 22:37
by felix63
First create a virtual sensor of type 'percentage'. Lets say 'percentage'. Then create the following dzvents script to convert the value from 'sensor' 0 - 270 to a percentage value in sensor 'percentage'. Haven't tested the script btw. Might contain errors!
Code: Select all
return {
-- 'active' controls if this entire script is considered or not
active = true, -- set to false to disable this script
-- trigger
-- can be a combination:
on = {
devices = {
-- scripts is executed if the device that was updated matches with one of these triggers
'sensor',
},
},
logging = {
-- level = domoticz.LOG_INFO,
marker = "Converter"
},
-- actual event code
-- in case of a timer event or security event, device == nil
execute = function(domoticz, device)
domoticz.devices('percentage').updatePercentage(domoticz.utils.round((domoticz.devices('Sensor').nValue/270)*100),2)
end
}
Re: Utility value to dummy percentage
Posted: Tuesday 19 February 2019 6:20
by Tarzan737
Thanks!
I couldnt really make it work last night with that script. I just renamed sensor and percentage to the name of my devices, something else to do?
Re: Utility value to dummy percentage
Posted: Tuesday 19 February 2019 8:27
by waaren
Tarzan737 wrote: Tuesday 19 February 2019 6:20
Thanks!
I couldnt really make it work last night with that script. I just renamed sensor and percentage to the name of my devices, something else to do?
Not every sensor use nValue to store the measured value. What is the type / subtype of your sensor ? (look at the devices tab)
What do you see in the logfile ?
Re: Utility value to dummy percentage
Posted: Tuesday 19 February 2019 17:26
by Tarzan737
it looks like this.. and i use denkovi daenetip4 hardware
Re: Utility value to dummy percentage
Posted: Tuesday 19 February 2019 18:40
by waaren
Tarzan737 wrote: Tuesday 19 February 2019 17:26
it looks like this.. and i use denkovi daenetip4 hardware
In this deviceType the value is stored in the sValue field. Can you test below script ?
Code: Select all
return {
on = {
devices = {
'sensor'
},
},
logging = {
level = domoticz.LOG_INFO,
marker = "Converter"
},
execute = function(dz, item)
local dataFromSensor = item.rawData[1]
local maxValue = 270
local decimals = 2
local percentage = dataFromSensor / maxValue * 100
local roundedPercentage = dz.utils.round(percentage,decimals )
local percentageDevice = dz.devices('percentage')
dz.log("Rounded percentage is " .. roundedPercentage .. "%")
percentageDevice.updatePercentage(roundedPercentage)
end
}
Re: Utility value to dummy percentage
Posted: Tuesday 19 February 2019 21:27
by Tarzan737
YEEESSS!! This seems to work!! Thank you very much for help..
if i want to have the opposite.. like value 0 is 100% instead.. because i don´t know how my new sensor works.. (haven`t recieved it)
value 1023 0% and value 0 100%
Re: Utility value to dummy percentage
Posted: Wednesday 20 February 2019 0:27
by waaren
Tarzan737 wrote: Tuesday 19 February 2019 21:27
YEEESSS!! This seems to work!! Thank you very much for help..
if i want to have the opposite.. like value 0 is 100% instead.. because i don´t know how my new sensor works.. (haven`t recieved it)
value 1023 0% and value 0 100%
set inverted = true in below script should do that
Code: Select all
return {
on = {
devices = {
'sensor'
},
},
logging = {
level = domoticz.LOG_INFO,
marker = "Converter"
},
execute = function(dz, item)
local inverted = false -- if max value of sensor should be reflected in 0% and 0 in 100%
local dataFromSensor = item.rawData[1]
local maxValue = 270
local decimals = 2
local percentage = dataFromSensor / maxValue * 100
if inverted then
percentage = 100 - percentage
end
local roundedPercentage = dz.utils.round(percentage,decimals )
local percentageDevice = dz.devices('percentage')
dz.log("Rounded percentage is " .. roundedPercentage .. "%")
percentageDevice.updatePercentage(roundedPercentage)
end
}
Re: Utility value to dummy percentage
Posted: Wednesday 20 February 2019 16:32
by Tarzan737
Thank u very much!! it works fine!!
Re: Utility value to dummy percentage
Posted: Wednesday 27 February 2019 21:12
by Tarzan737
Hello!!
i noticed that my 0 value not always is 0 when calibrating my sensors, it could be like 137 or something so the percentage will not
show correct.
i tried to add the line localMinvalue = 137 but nu success..
not that easy that i thought

Re: Utility value to dummy percentage
Posted: Wednesday 27 February 2019 23:53
by waaren
Tarzan737 wrote: Wednesday 27 February 2019 21:12
Hello!!
i noticed that my 0 value not always is 0 when calibrating my sensors, it could be like 137 or something so the percentage will not
show correct.
i tried to add the line localMinvalue = 137 but nu success..
not that easy that i thought
Could be something like this ? Adjust minValue to what you observe
Code: Select all
return {
on = {
devices = {
'sensor'
},
},
logging = {
level = domoticz.LOG_INFO,
marker = "Converter"
},
execute = function(dz, item)
local inverted = false -- if max value of sensor should be reflected in 0% and 0 in 100%
local dataFromSensor = item.rawData[1]
local maxValue = 270
local decimals = 2
local minValue = 0
local percentage = ( dataFromSensor - minValue ) / maxValue * 100
if inverted then
percentage = 100 - percentage
end
local roundedPercentage = dz.utils.round(percentage,decimals )
local percentageDevice = dz.devices('percentage')
dz.log("Rounded percentage is " .. roundedPercentage .. "%")
percentageDevice.updatePercentage(roundedPercentage)
end
}
Re: Utility value to dummy percentage
Posted: Thursday 28 February 2019 16:48
by Tarzan737
Yepp!! seems to work! Thanks again
Re: Utility value to dummy percentage
Posted: Tuesday 05 March 2019 19:47
by Tarzan737
hmm need help again
in my tank level sensor max value is 502 and min value 492
but it does not show correct at all, let´s say my sensor output is 499
with this formula ( dataFromSensor - minValue ) / maxValue * 100
it will take 499-492 = 7 then take 7/502 * 100
and the percentage is not correct..
any idea how to write the code?
Re: Utility value to dummy percentage
Posted: Tuesday 05 March 2019 22:48
by waaren
Tarzan737 wrote: Tuesday 05 March 2019 19:47
in my tank level sensor max value is 502 and min value 492
any idea how to write the code?
Sure but first need to understand
So 502 is 100% and 492 = 0% ?
Or do you mean something else ?
Re: Utility value to dummy percentage
Posted: Wednesday 06 March 2019 6:01
by Tarzan737
Yepp! Correct. (In this case with tank level)
Re: Utility value to dummy percentage
Posted: Wednesday 06 March 2019 10:02
by waaren
Tarzan737 wrote: Wednesday 06 March 2019 6:01
Yepp! Correct. (In this case with tank level)
Can you test this ?
Code: Select all
return {
on = {
devices = {
'sensor'
},
},
logging = {
level = domoticz.LOG_INFO,
marker = "Converter"
},
execute = function(dz, item)
local inverted = false -- if max value of sensor should be reflected in 0% and minValue in 100%
local dataFromSensor = item.rawData[1]
local maxValue = 502
local minValue = 492
local decimals = 2
local percentage = ( dataFromSensor - minValue ) / ( maxValue - minValue ) * 100
if inverted then
percentage = 100 - percentage
end
local roundedPercentage = dz.utils.round(percentage,decimals )
local percentageDevice = dz.devices('percentage')
dz.log("Rounded percentage is " .. roundedPercentage .. "%")
percentageDevice.updatePercentage(roundedPercentage)
end
}
Re: Utility value to dummy percentage
Posted: Thursday 07 March 2019 20:11
by Tarzan737
Yes that works as i wish, what should i do without you

thanks again
Re: Utility value to dummy percentage
Posted: Monday 06 April 2020 21:52
by Tarzan737
Hello..
has anything changed with domoticz?
i cant make this code work anymore..
anyone knows why?
i use the same hardware (daenetip4)
Re: Utility value to dummy percentage
Posted: Monday 06 April 2020 21:57
by waaren
Tarzan737 wrote: Monday 06 April 2020 21:52
Hello..
has anything changed with domoticz?
Yes
i cant make this code work anymore..
Please supply some more background. What is your current domoticz version / OS / What do you see in the log / what does not work
Re: Utility value to dummy percentage
Posted: Monday 06 April 2020 22:01
by Tarzan737
i think i did see now!! in options i had to check the box enable dz vents.. it should be the last version
had som hours just to make domoticz start again
