Page 1 of 1

Script from voltage to percentage

Posted: Sunday 21 June 2020 15:01
by riekelt3
Hello,

I have rainwater tank in my garden. I have a fibaro smart implant. I added already succesful to domoticz. I can now readout the voltage from the sensor. How can I change the data from voltage to percentage in a script? Please can anyone help me?

regards,
Riekelt

Re: Script from voltage to percentage

Posted: Sunday 21 June 2020 15:12
by waaren
riekelt3 wrote: Sunday 21 June 2020 15:01 I have rainwater tank in my garden. I have a fibaro smart implant. I added already succesful to domoticz. I can now readout the voltage from the sensor. How can I change the data from voltage to percentage in a script? Please can anyone help me?
What is the type and subtype (devices tab) of the current sensor?
What is the min and max voltage?
Should min voltage be 0% and max voltage be 100%?
What should be the frequency to update the virtual percentage sensor? (every change in voltage or every minute or every hour or something else)

Re: Script from voltage to percentage

Posted: Sunday 21 June 2020 21:07
by riekelt3
Thanks for the quick reply. I will figure it out and discribe it out.

Regards
Riekelt

Re: Script from voltage to percentage

Posted: Monday 22 June 2020 13:36
by riekelt3
The type of the sensor is: general and the subtype: voltage
The min voltage is: 1,45V and the Max: 2,06V
Yes the min voltage should be 0% and the max voltage should be 100%
The frequence should be every change in voltage

Regards
Riekelt

Re: Script from voltage to percentage

Posted: Monday 22 June 2020 23:08
by waaren
riekelt3 wrote: Monday 22 June 2020 13:36 The frequence should be every change in voltage
Could be something like

Code: Select all

return
{
    on =
    {
        devices =
        {
            'waterLevelVoltage'      -- change to name of voltage device
        }
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when script executes without issues
        marker = 'watertank level',
    },

    execute = function(dz, item)
        local measuredVoltage = item.voltage
        local tankLevelIndicator = dz.devices('waterLevel') -- change to name of percentage device

        local minVoltage = 1.45
        local maxVoltage = 2,06

        local function guardPercentage( percentage )
           if percentage > 0.05 and percentage <= 100 then return percentage end
           dz.log('Unexpected voltage (' .. percentage .. ')', dz.LOG_ERROR)
           if percentage > 100 then return 100 else return 0 end
        end

        local levelPercentage = dz.utils.round( guardPercentage ( ( measuredVoltage - minVoltage ) * ( 100 / ( maxVoltage - minVoltage ) ) ) ,1 )

        dz.log('Percentage = ' .. levelPercentage, dz.LOG_DEBUG)

        tankLevelIndicator.updatePercentage(levelPercentage)

    end
}

Re: Script from voltage to percentage

Posted: Friday 26 June 2020 20:06
by riekelt3
The script is working. I am very happy with this. Only when I see the voltage and the percentage it is for 2 seconds. And than the voltage and percentage go to 0. Untill the next voltage is coming from the sensor.

Re: Script from voltage to percentage

Posted: Saturday 27 June 2020 7:33
by waaren
riekelt3 wrote: Friday 26 June 2020 20:06 The script is working. I am very happy with this. Only when I see the voltage and the percentage it is for 2 seconds. And than the voltage and percentage go to 0. Untill the next voltage is coming from the sensor.
if domoticz receive a volt update it will update the volt sensor. Not much you can do about that. The percentage sensor is controlled by dzVents.
You could change the guardPercentage function to

Code: Select all

      local function guardPercentage( percentage )
            if percentage >= 0.1 and percentage <= 100 then return percentage end
            dz.log('Unexpected voltage (' .. percentage .. ')', dz.LOG_DEBUG)
            if percentage > 100 then 
                return 100 
            else 
                return tankLevelIndicator.percentage
            end
        end

This will prevent the 0 update to mess with the level

Re: Script from voltage to percentage  [Solved]

Posted: Sunday 28 June 2020 20:20
by riekelt3
It's working now for 100%. Thank you for the support. I am very happy with this.

Re: Script from voltage to percentage

Posted: Thursday 30 July 2020 15:58
by heistje
I also used this script for a my garden watertank, not with a fibaro smart implant but with a Water Level Sensor I bought at Aliexpress: https://www.aliexpress.com/item/4000098 ... c4dO2kw75)
and
a ETH8020 (analog input 0-5VDC)

Would like to thank you guys! Thanks to this script it works like a charm!

Re: Script from voltage to percentage

Posted: Friday 18 February 2022 14:48
by Drdre
I also need a script but then from voltage (0-5) to windspeed km/h (0-100).
Sombody can help me with this script?

Re: Script from voltage to percentage

Posted: Friday 18 February 2022 14:58
by waltervl
Drdre wrote: Friday 18 February 2022 14:48 I also need a script but then from voltage (0-5) to windspeed km/h (0-100).
Sombody can help me with this script?
You can use the script above but make some modifications.
But what is your formula to go from voltage to windspeed?

Re: Script from voltage to percentage

Posted: Friday 18 February 2022 15:08
by waltervl
Assuming 0 volt = 0 kmh and 5 volt = 100 kmh so a factor 20:
formula kmh = V*20

NOT TESTED!!! Could contain Error!!!

Code: Select all

return
{
    on =
    {
        devices =
        {
            'windspeedvoltage'      -- change to name of voltage device
        }
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when script executes without issues
        marker = 'windspeed',
    },

    execute = function(dz, item)
        local measuredVoltage = item.voltage
        local WindspeedDevice = dz.devices('windspeed') -- change to name of Custom Sensor for windspeed.

        local windspeed = dz.utils.round( measuredVoltage  * 20 ,1 )

        dz.log('Wind Speed = ' .. windspeed, dz.LOG_DEBUG)

        WindspeedDevice.updateCustomSensor(windspeed)

    end
}

Re: Script from voltage to percentage

Posted: Sunday 20 February 2022 15:34
by Drdre
It's working thanks!!!!!