Utility value to dummy percentage

Moderator: leecollings

Post Reply
Tarzan737
Posts: 60
Joined: Friday 01 June 2018 20:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Utility value to dummy percentage

Post 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
User avatar
felix63
Posts: 244
Joined: Monday 07 December 2015 9:30
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Location: Gouda
Contact:

Re: Utility value to dummy percentage

Post 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
}
Tarzan737
Posts: 60
Joined: Friday 01 June 2018 20:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Utility value to dummy percentage

Post 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?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Utility value to dummy percentage

Post 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 ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Tarzan737
Posts: 60
Joined: Friday 01 June 2018 20:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Utility value to dummy percentage

Post by Tarzan737 »

it looks like this.. and i use denkovi daenetip4 hardware
Attachments
Screenshot (17).png
Screenshot (17).png (373.66 KiB) Viewed 3512 times
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Utility value to dummy percentage

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Tarzan737
Posts: 60
Joined: Friday 01 June 2018 20:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Utility value to dummy percentage

Post 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%
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Utility value to dummy percentage

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Tarzan737
Posts: 60
Joined: Friday 01 June 2018 20:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Utility value to dummy percentage

Post by Tarzan737 »

Thank u very much!! it works fine!!
Tarzan737
Posts: 60
Joined: Friday 01 June 2018 20:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Utility value to dummy percentage

Post 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 :(
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Utility value to dummy percentage

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Tarzan737
Posts: 60
Joined: Friday 01 June 2018 20:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Utility value to dummy percentage

Post by Tarzan737 »

Yepp!! seems to work! Thanks again
Tarzan737
Posts: 60
Joined: Friday 01 June 2018 20:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Utility value to dummy percentage

Post 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?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Utility value to dummy percentage

Post 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 :D
So 502 is 100% and 492 = 0% ?
Or do you mean something else ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Tarzan737
Posts: 60
Joined: Friday 01 June 2018 20:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Utility value to dummy percentage

Post by Tarzan737 »

Yepp! Correct. (In this case with tank level)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Utility value to dummy percentage

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Tarzan737
Posts: 60
Joined: Friday 01 June 2018 20:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Utility value to dummy percentage

Post by Tarzan737 »

Yes that works as i wish, what should i do without you :) thanks again
Tarzan737
Posts: 60
Joined: Friday 01 June 2018 20:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Utility value to dummy percentage

Post by Tarzan737 »

Hello..
has anything changed with domoticz?

i cant make this code work anymore..

anyone knows why?

i use the same hardware (daenetip4)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Utility value to dummy percentage

Post by waaren »

Tarzan737 wrote: Monday 06 April 2020 21:52 Hello..
has anything changed with domoticz?
Yes :D
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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Tarzan737
Posts: 60
Joined: Friday 01 June 2018 20:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Utility value to dummy percentage

Post 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 :)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest