Page 3 of 3

Re: Updating incremental counter with TIME value / json

Posted: Friday 02 April 2021 13:04
by 19leva89

Code: Select all

--[[ getUptime.lua for [ dzVents >= 2.4 ] ]]--

return 
{
    on =
    {
        devices = 
        { 
            74, 88, 260, 350, 366, 368, 369, 370, 371, 
        },
    },

    logging =   
    {   
        level = domoticz.LOG_DEBUG,
        marker = "getUptime",
    },

    data =
    {
        devices =
        {
            initial = {},
        },
    },
    
    execute = function(dz, item)
        
        --- Your settings below this line
        local count = dz.devices(420) -- change to device idx of your total counting device
        
        local usage =
        {
            -- default is the power for devices not in this table
            default = 10,
            
            -- [device idx] = power
            [74] = 10, -- Bedside-Lamp
            [88] = 10, -- Backlight-Kitchen
            [260] = 12, -- Light-Bedroom 
            [350] = 20, -- Light-Loggia
            [366] = 10, -- Light-Сorridor
            [368] = 5, -- Light-Kitchen
            [369] = 11, -- Light-Bathroom
            [370] = 6, -- Light-Kitchen-Extract
            [371] = 20, -- Backlight-Bathroom
        }
        --- No changes required below this line
        
        local dvData = dz.data.devices[item.name] or {}

        local function getDataTotal(field)
            local total = 0
            for device, record in pairs(dz.data.devices) do
                total = total + record[field]
            end
            return total
        end

        local function setData(item, delta)
            local secondsOn = (dvData.secondsOn and ( dvData.secondsOn + delta ) ) or 0
            local energy = (dvData.energy and ( dvData.energy + ( delta * ( usage[item.idx] or usage.default ) ) ) ) or 0  -- Watt seconds
            dvData =
            {
                lastState = item.state,
                secondsOn = secondsOn,
                lastSwitchSeconds = os.time(),
                lastSwitchTime = dz.time.rawDate .. " " .. dz.time.rawTime,
                energy = energy,     
            }
        end
        
        if next(dvData) == nil then
            setData(item, 0)
        else
            if item.state ~= dvData.lastState then
                local deltaTime = 0
                if item.state == "Off" then
                    deltaTime = os.time() - dvData.lastSwitchSeconds
                end
                setData(item, deltaTime) 
            
                local round_to_4 = dz.utils.round( getDataTotal('secondsOn'), 4 )  
                count.updateCounter(round_to_4)
            end
            
            dz.log('Device ' .. item.name .. ", State ==>> " .. item.state .. "; Date Time ==>> " .. dvData.lastSwitchTime .. "; secondsOn ===>> " .. 
                    dvData.secondsOn .. "; Energy ===>> " .. dz.utils.round( ( dvData.energy / 3600 / 1000 ), 4 ) .. ' kWh' , dz.LOG_DEBUG)
        end
        
        dz.log('Total energy counted over all devices: ' .. getDataTotal('energy'), dz.LOG_DEBUG)
        dz.log('Total seconds counted over all devices: ' .. getDataTotal('secondsOn'), dz.LOG_DEBUG)

    end
}

Re: Updating incremental counter with TIME value / json

Posted: Friday 02 April 2021 13:21
by waaren
19leva89 wrote: Friday 02 April 2021 13:04
Checked with the latest stable and to also make it work in that version change

Code: Select all

        dz.log('Total energy counted over all devices: ' .. getDataTotal('energy'), dz.LOG_DEBUG)
        dz.log('Total seconds counted over all devices: ' .. getDataTotal('secondsOn'), dz.LOG_DEBUG)
        
    end
}
to

Code: Select all

        dz.log('Total energy counted over all devices: ' .. getDataTotal('energy'), dz.LOG_DEBUG)
        dz.log('Total seconds counted over all devices: ' .. getDataTotal('secondsOn'), dz.LOG_DEBUG)
        
        dz.data.devices[item.name] = dvData

    end
}

Re: Updating incremental counter with TIME value / json

Posted: Friday 02 April 2021 13:42
by 19leva89
Thanks, it worked! Seconds and power began to count.

Code: Select all

2021-04-02 14:32:22.782 Status: User: Admin initiated a switch command (370/Light-Kitchen-Extract/Off)
2021-04-02 14:32:23.036 Status: dzVents: Info: Handling events for: "Light-Kitchen-Extract", value: "Off"
2021-04-02 14:32:23.037 Status: dzVents: Info: getUptime: ------ Start internal script: Light-kWh: Device: "Light-Kitchen-Extract (Dummy-Sensors)", Index: 370
2021-04-02 14:32:23.039 Status: dzVents: Debug: getUptime: Processing device-adapter for test-power: Counter device adapter
2021-04-02 14:32:23.039 Status: dzVents: Debug: getUptime: Device Light-Kitchen-Extract, State ==>> Off; Date Time ==>> 2021-04-02 14:32:22; secondsOn ===>> 55; Energy ===>> 0.0001 kWh
2021-04-02 14:32:23.039 Status: dzVents: Debug: getUptime: Total energy counted over all devices: 0
2021-04-02 14:32:23.039 Status: dzVents: Debug: getUptime: Total seconds counted over all devices: 0
2021-04-02 14:32:23.040 Status: dzVents: Info: getUptime: ------ Finished Light-kWh

Re: Updating incremental counter with TIME value / json

Posted: Wednesday 01 February 2023 7:58
by mzonneveld
FrankVZ wrote: Monday 04 June 2018 1:26 Is there a way, to store a value - an user variable - into a dummy counter, that measures time?

I tried a lot of different settings, but without any luck

I would like to create a simple run time counter, since I have to replace a special device every 8000 running hours.

So I made a simple blockly, that should write a new value of 1 minute to an incremental dummy counter.

But whatever I try, the counter does not show any activity.

I also tried different json commands, in order to increase the counter, but without any luck too...
e.g. http://<ip>:<port>/json.htm?type=command&param=udevice&idx=88&nvalue=59
The result is always "ok", but no sign/values change within the counter (still got the question-marks).

Use parameter svalue=59 instead of nvalue=59

If I set the counter to "counter" the value increases. But this gives me only the result in minutes, and hours would be sufficient :)

The device 89 is a dummy hardware counter, with the type "time" (instead of count, energy, gas, water etc).

What would be the correct way, to increase the the counter every minute?

Re: Updating incremental counter with TIME value / json

Posted: Wednesday 01 February 2023 8:59
by waltervl
mzonneveld wrote: Wednesday 01 February 2023 7:58
It is a little difficult to understand your post as the quotes not used correctly.
But I sugguest to try the script as mentioned in the following post: viewtopic.php?p=221939#p221939
You will have to create a dummy incremental timer that will be updated by the script.