Calculate duty cycle

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

Moderator: leecollings

Post Reply
Jack007
Posts: 6
Joined: Friday 27 December 2019 18:41
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: NL
Contact:

Calculate duty cycle

Post by Jack007 »

Hi,

Just a warning that i'm a complete noob within Domoticz and programming. I don't even know if this question is something for Lua, dzventz of some other tool.

My question;
Since a few weeks i'm using the ems-esp from BbqKees to monitor my boiler. The boiler is controlled in an on/off mode. These on/off switch moments are visible in Domoticz.
Screenshot 2025-01-03 192100.png
Screenshot 2025-01-03 192100.png (12.89 KiB) Viewed 254 times
What I would like to know/show/calculate is the current duty-cycle the boiler is operating; something like:
(T last off - T prev. last on) / (T last on- T prev. last on) = (19:02:25 - 18:56:45) / (19:16:45 - 18:56:45) = 340 / 1200 = 28.3%
Calculation could be performed every T last on.

I have no clue how to do that. A rough construct would already help a lot.

TIA
Jack
Jack007
Posts: 6
Joined: Friday 27 December 2019 18:41
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: NL
Contact:

Re: Calculate duty cycle

Post by Jack007 »

Nobody...?
bitzy
Posts: 36
Joined: Tuesday 18 April 2023 8:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: Calculate duty cycle

Post by bitzy »

what type of boiler are u talking about? and u mean "Duty Cycle (%)=( Active Run Time / Total Time)*100"? u need a total run time on a given time period or u want to see the time it was On between a Off/On/Off cycle?
what do u want to use this for?
JBN
Posts: 40
Joined: Saturday 13 July 2013 8:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Sweden
Contact:

Re: Calculate duty cycle

Post by JBN »

I would consider DzVents and the use of persistant variables - you can see more here: https://wiki.domoticz.com/DzVents:_next ... _variables and look at the special History variable.

This is completely untested but might serve as an idea of how you might do it:

Code: Select all

return {
        on = {
            devices = {<DEVICEID>}
        },
        data = {
            cycles = {  initial = {} }
             
        },
        execute = function(domoticz, sensor)
            -- add new data
            local Time = require('Time')

            table.insert(domoticz.data.cycles, {state=sensor.state, time=Time().getISO()})
            
            if(sensor.state == "Off" and #domoticz.data.cycles > 3) then
                local T_last_off = domoticz.data.cycles[4].time
                local T_last_on = domoticz.data.cycles[3].time
                local T_previous_off = domoticz.data.cycles[2].time
                local T_previous_on = domoticz.data.cycles[1].time
            
                local dividend  = Time(T_last_off).compare(Time(T_previous_off)).secs
                local divisor = Time(T_last_on).compare(Time(T_previous_on)).secs
                local percent = dividend / divisor
                
                print("Dividend: "..dividend)
                print(" Divisor: "..divisor)
                print(" Percent: "..percent)
                
            end
            
            while(#domoticz.data.cycles > 3) do
                table.remove(domoticz.data.cycles, 1)
            end
            
        end
        }
Last edited by JBN on Friday 10 January 2025 20:23, edited 2 times in total.
Kedi
Posts: 553
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: Calculate duty cycle

Post by Kedi »

Is this example from ChatGPT?
Logic will get you from A to B. Imagination will take you everywhere.
JBN
Posts: 40
Joined: Saturday 13 July 2013 8:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Sweden
Contact:

Re: Calculate duty cycle

Post by JBN »

I wish, I just jotted it down while eating breakfast (stupid, I know but I got a bit inspired, it won't happen again).

I have updated the example to something a tad bit more useful. But the key Google word was persistent data in DzVents something I have not tried before but seems really useful.

I thought the historical persistent variables would work differently tho but I didn't get that to work when I tried briefly now. So I swapped to using LUA table instead (it is likely overkill in this example but also something useful).
Jack007
Posts: 6
Joined: Friday 27 December 2019 18:41
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: NL
Contact:

Re: Calculate duty cycle

Post by Jack007 »

The suggested code helped a lot, it gave me a starting point. Thanks.

I modified it a bit (see below), but it is not working.
The errorlog shows this error:

Code: Select all

 2025-01-12 14:13:34.888 Error: dzVents: An error occurred when calling event handler DC_CH
2025-01-12 14:13:34.888 Error: dzVents: ...ack/domoticz/scripts/dzVents/generated_scripts/DC_CH.lua:49: attempt to call a nil value (field 'updatepercentage') 
The error arises when the dummy device ('Duty cycle CV') should be updated (defined as percentage).
Any idea what is wrong here?

An additional question; how to debug this? (Almost) nothing shows up in the log.

Code: Select all

return {
    on = {
            --device that switches on/of
            devices = {'ems-esp Boiler (Heating active)'}
    },
    data = {
        cycles = {  initial = {} }
             
    },

    execute = function(domoticz, sensor)
        
        -- senser state has changed
        -- start calculation and show current cycles array size (should be 2)
        domoticz.log('Calculate duty cycle CH: '..tostring(#domoticz.data.cycles), domoticz.LOG_INFO)
        
        -- add the current state and time to cycles array
        -- after startup (2 cycles) it should always be max index=3
        local Time = require('Time')
        table.insert(domoticz.data.cycles, {state=sensor.state, time=Time().getISO()})
        
        -- We are looking for a on-off-on cycle    
        if(sensor.state == "On" and #domoticz.data.cycles > 2) then
            
            -- last added has index 3
            local T_last_on = domoticz.data.cycles[3].time
            local T_last_off = domoticz.data.cycles[2].time
            local T_previous_on = domoticz.data.cycles[1].time
            
            domoticz.log("Duty T_on:  "..tostring(T_last_on), domoticz.LOG_DEBUG)
            domoticz.log("Duty T_off: "..tostring(T_last_off), domoticz.LOG_DEBUG)
            domoticz.log("Duty T_on:  "..tostring(T_previous_on), domoticz.LOG_DEBUG)

            -- calculate time between 2xOn in secs.
            local max_duty = Time(T_last_on).compare(Time(T_previous_on)).secs
            -- calculate last On time in secs.
            local cur_duty = Time(T_last_off).compare(Time(T_previous_on)).secs
            
            -- Calculate DC
            local duty_cycle = cur_duty / max_duty
            -- Convert to percentage
            duty_cycle=100*duty_cycle

            domoticz.log("max_duty  : "..tostring(max_duty), domoticz.LOG_DEBUG)
            domoticz.log("cur_duty  : "..tostring(cur_duty), domoticz.LOG_DEBUG)
            domoticz.log("duty_cycle: "..tostring(duty_cycle), domoticz.LOG_DEBUG)

            -- update the percentage dummy device
            domoticz.devices('Duty cycle CV').updatepercentage(duty_cycle)
        end
            
        -- reduce array to max. 2
        -- delete oldest
        while(#domoticz.data.cycles > 2) do
           table.remove(domoticz.data.cycles, 1)
        end
            
    end
}
User avatar
waltervl
Posts: 5293
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Calculate duty cycle

Post by waltervl »

Domoticz Dzvents is case sensitive so it should be updatePercentage:

Code: Select all

domoticz.devices('Duty cycle CV').updatePercentage(duty_cycle)
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Jack007
Posts: 6
Joined: Friday 27 December 2019 18:41
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: NL
Contact:

Re: Calculate duty cycle

Post by Jack007 »

Domoticz Dzvents is case sensitive
Good to known (and not to be forgotten). Thanks
It actually seems to work now.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest