local FLOW_DEVICE = 'Watermeter - Current usage' -- Flow device
local LEAK_DEVICE = 'Watermeter - Leakage percent' -- Percent dummy device

return {
    active = true,

    -- Trigger section: run the script every 1 minute
    on = {
        timer = {
            'every 1 minutes'
        }
    },

    -- Custom logging level for this script
    logging = {
        level = domoticz.LOG_INFO,
        marker = "AWTRIX_LEAK"
    },

    data = {
        time_0_flow = { initial = 0 },
        total_time = { initial = 0 },
        currentIndex = { initial = 0 }
    },


    execute = function(domoticz)
        -- Flow in liters/minute
        local flow = tonumber(domoticz.devices(FLOW_DEVICE).rawData[1])
        
        -- Dummy device in %
        local leakage = domoticz.devices(LEAK_DEVICE)
        local time_with_flow = tonumber(leakage.rawData[1])
        local new_time_with_flow = time_with_flow

        -- 1 / Leakage "open valve"
        if (flow > 0) then
            domoticz.data.time_0_flow = 0 -- There is flow
            new_time_with_flow = new_time_with_flow + 1 -- Time with flow
            if (new_time_with_flow > 100) then
                new_time_with_flow = 100
            end
        else
            new_time_with_flow = 0
            domoticz.data.time_0_flow = domoticz.data.time_0_flow + 1 -- Time without flow
        end

        -- 2 / Micro continuous flow (drip)
        domoticz.data.total_time = domoticz.data.total_time + 1 -- Time without since last 2 hours with no flow
        if (domoticz.data.time_0_flow > 120) then
            -- 2 hours with no flow
            domoticz.data.total_time = 0
        elseif (domoticz.data.total_time > (60 * 24)) then
            -- 24 hours since last 2 hours with no flow
            new_time_with_flow = 100
        end

        -- Log
        domoticz.log(new_time_with_flow .. ' minutes with flow')
        domoticz.log(domoticz.data.time_0_flow .. ' minutes without flow')
        domoticz.log(domoticz.data.total_time .. ' minutes without 2hrs without flow')

        -- Update dummy device %
        if (time_with_flow ~= new_time_with_flow) then
            leakage.update(0, new_time_with_flow)
        end

        -- Description for AWTRIX3
        local flowStatus = string.format("%.1f L/min", flow)
        local leakStatus = string.format("%d%%", new_time_with_flow)
        
        -- Create individual JSON entries for flow and leak
        local statuses = {
            { app = 'Water1', text = flowStatus, icon = 18191 }, -- Flow status with its icon
            { app = 'Water2', text = leakStatus, icon = 59365 } -- Leak status with its icon
        }
        
        -- Send to AWTRIX3
        local appDevice = domoticz.devices('AWTRIX3 - Send Custom App')
        if appDevice then
            local statusesJSON = domoticz.utils.toJSON(statuses)
            domoticz.log("Sending data to AWTRIX: " .. statusesJSON, domoticz.LOG_INFO)
            appDevice.setDescription(statusesJSON)
            appDevice.switchOn().afterSec(2)
        else
            domoticz.log("AWTRIX3 - Send Custom App device not found", domoticz.LOG_ERROR)
        end

    end
}