Water meter to flow conversion

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

Moderator: leecollings

Post Reply
Maciek90
Posts: 35
Joined: Friday 05 March 2021 23:01
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Water meter to flow conversion

Post by Maciek90 »

Hi. Using ChatGPT, I've written a script that converts water meter values to flow in l/min. However, I'm having one problem with this script. The Flow device only refreshes when the Watertest counter value changes. If the Watertest counter value doesn't change in the next reading because there was no water consumption, the Flow device doesn't refresh. Could someone help me fix this?

Code: Select all

return {
    on = {
        devices = { 'Watertest' }
    },
    
    data = {
        lastValue = { initial = 0 },
        lastTime = { initial = os.time() }
    },

    execute = function(domoticz, device)
        local currentValue = device.counter
        local currentTime = os.time()

        local deltaLiters = currentValue - domoticz.data.lastValue
        local deltaTime = (currentTime - domoticz.data.lastTime) / 60 

        domoticz.log(string.format("ΔL: %.2f, Δt: %.2f min", deltaLiters, deltaTime))

        if deltaLiters >= 0 and deltaTime > 0 then
            local flow = deltaLiters / deltaTime
            flow = math.floor(flow * 1000)

            domoticz.log('Obliczony przepływ: ' .. flow .. ' L/min')

            local czujnik = domoticz.devices('Flow')
            if czujnik then
                czujnik.updateWaterflow(flow)
            else
                domoticz.log("Nie znaleziono urządzenia 'Przepływ'", domoticz.LOG_ERROR)
            end
        end

        domoticz.data.lastValue = currentValue
        domoticz.data.lastTime = currentTime
    end
}
User avatar
waltervl
Posts: 5962
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Water meter to flow conversion

Post by waltervl »

You can also use one of the scripts mentioned in this topic about the same subject viewtopic.php?t=30920
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Maciek90
Posts: 35
Joined: Friday 05 March 2021 23:01
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Water meter to flow conversion

Post by Maciek90 »

Thank you so much. The scripts you provided helped me achieve everything I wanted.
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests