dzVents calculating active time

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

Moderator: leecollings

Post Reply
HvdW
Posts: 612
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

dzVents calculating active time

Post by HvdW »

Hi,
I have this script for car battery charging. (simplified for readability)

Code: Select all

return {
	on = {
		devices = {'mySwitch',},
	},
	data = {
	    'timeOff', 'time8A','time10A','time13A','time16A',
	    },
		logging = {
	    -- Level can be domoticz.LOG_INFO, domoicz.LOG_MODULE_EXEC_INFO, domoticz.LOG_DEBUG, domoticz.LOG_ERROR or domoticz.LOG_FORCE
        --level = domoticz.LOG_INFO,
        level = domoticz.LOG_DEBUG,
		marker = '---- mySwitch -----',
	},
	

	execute = function(domoticz, triggerObject)
    -- domoticz.devices('mySwitch').dump()
    local Level = domoticz.devices('mySwitch')
    if Level.state == 'Off' then
        domoticz.log('0 Level set to Off  :' .. Level.state,domoticz.LOG_DEBUG)
        -- execute a command
    elseif Level.state == '6A' then
        domoticz.log('6 Level set to 6A  :' .. Level.state,domoticz.LOG_DEBUG)
    elseif Level.state == '8A' then
        domoticz.log('8 Level set to 8A  :' .. Level.state,domoticz.LOG_DEBUG)
    elseif Level.state == '10A' then
        domoticz.log('10 Level set to 10A  :' .. Level.state,domoticz.LOG_DEBUG)
    elseif Level.state == '13A' then
        domoticz.log('13 Level set to 13A  :' .. Level.state,domoticz.LOG_DEBUG)
	elseif Level.state == '16A' then
        domoticz.log('16 Level set to 16A  :' .. Level.state,domoticz.LOG_DEBUG)
	end
	domoticz.log('Result mySwitch set to  :' .. Level.state,domoticz.LOG_DEBUG)
 end
}
-- example script from @waaren
--[[
return 
{
	on = 
	{
		timer = 
		{
			'every minute',
		},
	},
	
	execute = function(dz)
	    local myTime = '15:00:00'
	    local someTime = dz.time.makeTime(dz.time.rawDate .. ' ' .. myTime) -- this will create the time object someTime
	
	    -- compare require two dzVents time objects (first one here is dz.time, second one is someTime )  
		dz.log('Delta time between  ' .. dz.time.rawTime .. ' and ' .. someTime.rawTime .. ' is ' .. dz.time.compare(someTime).minutes .. ' minutes.', dz.LOG_FORCE)
	end
}
]]--
I'd like to have some help.
I want to calculate and save the time that the switch is off, 8A, 10A etc.
Best is to start an array and add minutes that a part of the script is active.

How can this be achieved.
Bugs bug me.
HvdW
Posts: 612
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: dzVents calculating active time

Post by HvdW »

A friend of mine suggested me to ask Bing.
The script was produced within a few seconds and the result after some finetuning done by Bing as well, is this.

Code: Select all

return {
    on = {
        devices = {
            'YourSwitchSelector'
        }
    },
    data = {
        resetTrigger = { initial = 0 },
        startTime = { initial = 0 },
        totalTime = { initial = {0, 0, 0, 0, 0} },
        lastState = { initial = 0 },
        laadpaalStart = { initial = 0 }
    },
    execute = function(domoticz, item)
        -- Check if the reset trigger is activated
        -- domoticz.data.resetTrigger = 1
        -- if set to 1 all data will be reset to 0
        -- uncomment to reset
        -- the script ends after the reset.
        -- comment domoticz.data.resetTrigger = 1 again to let the script execute all the way
        if domoticz.data.resetTrigger == 1 then
            domoticz.data.startTime = 0
            domoticz.data.totalTime = {0, 0, 0, 0, 0}
            domoticz.data.lastState = 0
            domoticz.data.laadpaalStart = 0
            domoticz.data.times = {}
            domoticz.data.totalActiveTime = 0
            domoticz.data.percentages = {}
            domoticz.data.kWh = {}
            domoticz.data.totalKWh = 0
            domoticz.data.solarKWh = 0
            local laadpaalMeter = domoticz.devices('Laadpaal')
            domoticz.data.laadpaalStart = laadpaalMeter and laadpaalMeter.usage or 0
            domoticz.log('Values reset to zero. Laadpaal start value set to: ' .. domoticz.data.laadpaalStart, domoticz.LOG_INFO)
            -- Reset the trigger to 0
            domoticz.data.resetTrigger = 0
            return
        end

        if domoticz.devices('EVSE Switch 16A').state == 'Off' then
            local now = os.time()
            local stateMap = {
                [0] = 1,  -- Off
                [10] = 2, -- 8A
                [20] = 3, -- 10A
                [30] = 4, -- 13A
                [40] = 5  -- 16A
            }
            local state = stateMap[item.level]

            domoticz.log('Current state: ' .. (state or 'unknown'), domoticz.LOG_INFO)

            if domoticz.data.lastState ~= 0 and domoticz.data.totalTime[domoticz.data.lastState] then
                local duration = now - domoticz.data.startTime
                domoticz.log('Duration for state ' .. domoticz.data.lastState .. ': ' .. duration .. ' seconds', domoticz.LOG_INFO)
                domoticz.data.totalTime[domoticz.data.lastState] = domoticz.data.totalTime[domoticz.data.lastState] + duration
            end

            domoticz.data.startTime = now
            domoticz.data.lastState = state or 0

            -- Convert total time from seconds to truncated minutes
            local times = {
                '8A: ' .. math.floor(domoticz.data.totalTime[2] / 60) .. 'm',
                '10A: ' .. math.floor(domoticz.data.totalTime[3] / 60) .. 'm',
                '13A: ' .. math.floor(domoticz.data.totalTime[4] / 60) .. 'm',
                '16A: ' .. math.floor(domoticz.data.totalTime[5] / 60) .. 'm'
            }

            -- Calculate total active time excluding 'Off' state
            local totalActiveTime = 0
            for i = 2, 5 do
                totalActiveTime = totalActiveTime + domoticz.data.totalTime[i]
            end

            -- Calculate percentage of time each state was active
            local percentages = {
                '8A: ' ..  string.format('%.0f', (domoticz.data.totalTime[2] / totalActiveTime) * 100) .. '%',
                '10A: ' .. string.format('%.0f', (domoticz.data.totalTime[3] / totalActiveTime) * 100) .. '%',
                '13A: ' .. string.format('%.0f', (domoticz.data.totalTime[4] / totalActiveTime) * 100) .. '%',
                '16A: ' .. string.format('%.0f', (domoticz.data.totalTime[5] / totalActiveTime) * 100) .. '%'
            }

            -- Calculate kWh based on Amperes and duration
            local kWh = {
                '8A: ' .. string.format('%.2f', (domoticz.data.totalTime[2] / 3600) * 8 * 230 / 1000) .. ' kWh',
                '10A: ' .. string.format('%.2f', (domoticz.data.totalTime[3] / 3600) * 10 * 230 / 1000) .. ' kWh',
                '13A: ' .. string.format('%.2f', (domoticz.data.totalTime[4] / 3600) * 13 * 230 / 1000) .. ' kWh',
                '16A: ' .. string.format('%.2f', (domoticz.data.totalTime[5] / 3600) * 16 * 230 / 1000) .. ' kWh'
            }

            -- Calculate total kWh for all states combined
            local totalKWh = 0
            for i = 2, 5 do
                totalKWh = totalKWh + (domoticz.data.totalTime[i] / 3600) * (i * 2 + 6) * 230 / 1000
            end

            -- Calculate total kWh from solar energy (8A, 10A, 13A, 16A)
            local solarKWh = 0
            for i = 2, 5 do
                solarKWh = solarKWh + (domoticz.data.totalTime[i] / 3600) * (i * 2 + 6) * 230 / 1000
            end

            -- Get kWh from 'Laadpaal' meter
            local laadpaalMeter = domoticz.devices('Laadpaal')
            local laadpaalKWh = laadpaalMeter and laadpaalMeter.usage or 'N/A'

            -- Update the text sensor with the total active time for each state and kWh
            local textSensor = domoticz.devices('YourTextSensor')
            if textSensor then
                local text = table.concat(times, ', ') .. 
                '\n\n' .. table.concat(percentages, ', ') .. 
                -- '\n\n' .. table.concat(kWh, ', ') .. 
                '\n\n' .. 'Laadpaal: ' .. laadpaalKWh .. ' kWh waarvan zon: ' .. string.format('%.2f', solarKWh) .. ' kWh\n  '
                textSensor.updateText(text)
                domoticz.log('Updated text sensor with: ' .. text, domoticz.LOG_INFO)
            else
                domoticz.log('Text sensor not found', domoticz.LOG_ERROR)
            end

            domoticz.log('Total active time for each state: ' .. table.concat(times, ', '), domoticz.LOG_INFO)
            domoticz.log('Percentage of active time for each state: ' .. table.concat(percentages, ', '), domoticz.LOG_INFO)
            domoticz.log('kWh for each state: ' .. table.concat(kWh, ', '), domoticz.LOG_INFO)
            domoticz.log('Total kWh: ' .. string.format('%.2f', totalKWh), domoticz.LOG_INFO)
            domoticz.log('Laadpaal kWh: ' .. laadpaalKWh, domoticz.LOG_INFO)
            domoticz.log('Solar kWh: ' .. string.format('%.2f', solarKWh), domoticz.LOG_INFO)
        end -- if domoticz.devices('EVSE Switch 16A').state == 'Off'
    end
}
The finetuning took more time.
Bugs bug me.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest