Page 1 of 1

Water meter to flow conversion

Posted: Tuesday 22 July 2025 3:29
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
}

Re: Water meter to flow conversion

Posted: Tuesday 22 July 2025 11:20
by waltervl
You can also use one of the scripts mentioned in this topic about the same subject viewtopic.php?t=30920

Re: Water meter to flow conversion

Posted: Thursday 24 July 2025 10:37
by Maciek90
Thank you so much. The scripts you provided helped me achieve everything I wanted.