Page 1 of 1

after switchOff: how long was that sensor on?

Posted: Saturday 02 May 2020 11:07
by renerene
After a device is switched off I would like to know what the total ON time was. Problem with the lastUpdate() method is, that it only goes back to the OFF event. What is an elegent way to program a .beforeLastUpdate method?

Time line:
[Sensor On] - time1 - [sensor Off] - time 2 - [script trigger] -> how to access time1?

Re: after switchOff: how long was that sensor on?

Posted: Saturday 02 May 2020 11:52
by waaren
renerene wrote: Saturday 02 May 2020 11:07 After a device is switched off I would like to know what the total ON time was. Problem with the lastUpdate() method is, that it only goes back to the OFF event. What is an elegent way to program a .beforeLastUpdate method?

Time line:
[Sensor On] - time1 - [sensor Off] - time 2 - [script trigger] -> how to access time1?
Something like this?

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            'myDevice',
            'myNextDevice',
            'myOtherDevice',
        },
    },
    
    logging = 
    {
        level = domoticz.LOG_DEBUG,  -- switch to LOG_ERROR when OK
        marker = 'time triggered temp reading',
    },

    data =
    {
        onTime =
        {
            initial = {},
        },
    },
        
    execute = function(dz, item)
        if item.state == 'On' then
            dz.data.onTime[item.name] = dz.time.dDate
        elseif item.state == 'Off' and dz.data.onTime[item.name] and dz.data.onTime[item.name] > 0 then
            dz.log('Device ' .. item.name .. ' was on for ' .. ( dz.time.dDate - dz.data.onTime[item.name] ) .. ' seconds' ,dz.LOG_FORCE)
            dz.data.onTime[item.name] = -1
        elseif item.state == 'Off' and dz.data.onTime[item.name] == -1 then
            dz.log('Device ' .. item.name .. ' was olready switched off.',dz.LOG_FORCE)
        elseif item.state == 'Off' and dz.data.onTime[item.name] == nil then
            dz.log('Start time for device ' .. item.name .. ' is not registered yet',dz.LOG_FORCE)    
        end  
    end    
}

Re: after switchOff: how long was that sensor on?  [Solved]

Posted: Saturday 02 May 2020 19:45
by renerene
Thank you very much!

Here is the final code. It's purpose it to log warm water usage and have the bathroom light flicker after 10 min shower:

Code: Select all

return {
	active = true,
	logging = {
		level = domoticz.LOG_DEBUG, -- comment to use the dzVents global logging setting
		marker = 'dzTapwaterCheck'
	},
	on = {
	    timer = {'every minute'},
	    devices = {'tapwater'},
	},
	data = {
        onTime = {initial=nil}
    },
	execute = function(dz, device, triggerInfo)
        if dz.devices('tapwater').active then
            if not dz.data.onTime then 
                dz.data.onTime=dz.time.dDate
            else
                local minuten=dz.utils.round((dz.time.dDate-dz.data.onTime)/60)
                if minuten==10 then
                    dz.helpers.managedNotify(dz, 'dzTapwaterCheck','Warm water is on for '..minuten..' minutes.', 0, 3, 0) --pushover
                end
                if minuten==10 or minuten==15 or minuten>19 then
                    dz.helpers.managedNotify(dz, 'dzTapwaterCheck','Warm water is on for '..minuten..' minutes.', 0, 5, 0) --speak
                    if dz.devices('pirBadkamer').active then
                        if dz.devices('badkamerLed').active then 
                            dz.devices('badkamerLed').switchOff().silent()
                            dz.devices('badkamerLed').switchOn().afterSec(3)
                        else
                            dz.devices('badkamerLed').switchOn().silent()
                            dz.devices('badkamerLed').switchOff().afterSec(3)
                        end
                    end
                end
            end
        elseif dz.data.onTime then --tapwater net uitgezet en onTime nog niet gebruikt in berekening?
            local minuten=dz.utils.round((dz.time.dDate-dz.data.onTime)/60)
            if minuten > 4 then
                dz.helpers.managedNotify(dz, 'dzTapwaterCheck','Warm water was turned off after '..minuten..' minutes.', -1, 3, 0) --pushover
            end
            dz.devices('tapwaterTotaal').incrementCounter(dz.time.dDate-dz.data.onTime) --aantal seconden gebruik beschrijven in totaal
            dz.data.onTime=nil
        end
    end
}