Script from voltage to percentage  [Solved]

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

Moderator: leecollings

Post Reply
riekelt3
Posts: 5
Joined: Sunday 21 June 2020 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Script from voltage to percentage

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

Re: Script from voltage to percentage

Post 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)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
riekelt3
Posts: 5
Joined: Sunday 21 June 2020 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Script from voltage to percentage

Post by riekelt3 »

Thanks for the quick reply. I will figure it out and discribe it out.

Regards
Riekelt
riekelt3
Posts: 5
Joined: Sunday 21 June 2020 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Script from voltage to percentage

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

Re: Script from voltage to percentage

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
riekelt3
Posts: 5
Joined: Sunday 21 June 2020 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Script from voltage to percentage

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

Re: Script from voltage to percentage

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
riekelt3
Posts: 5
Joined: Sunday 21 June 2020 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: Script from voltage to percentage  [Solved]

Post by riekelt3 »

It's working now for 100%. Thank you for the support. I am very happy with this.
heistje
Posts: 23
Joined: Saturday 21 January 2017 13:42
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Belgium
Contact:

Re: Script from voltage to percentage

Post 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!
Bullseye on RPI 4
Aeotec Z-Wave Plus Z-Stick Gen5
Zwave-JS-UI
RFXtrx433E
Eth8020
P1 USB
Shellys
ESP8266
Drdre
Posts: 4
Joined: Tuesday 31 March 2015 21:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script from voltage to percentage

Post 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?
User avatar
waltervl
Posts: 5721
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Script from voltage to percentage

Post 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?
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
waltervl
Posts: 5721
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Script from voltage to percentage

Post 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
}
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Drdre
Posts: 4
Joined: Tuesday 31 March 2015 21:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script from voltage to percentage

Post by Drdre »

It's working thanks!!!!!
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest