Page 1 of 1

activate from change state in a device

Posted: Saturday 23 January 2021 16:47
by zuputrus
Sorry for question, since I'm newbie in dzVents, there is a lot of things I don't know. I'm reading the wiki, but still I'm confused with some concepts.
I want to give a variable a value (value 1) if a device changes its state 3 or more times in a minute. In other case value should be 0 by default.
In that way, I can use that variable to start one or other script depending its value.
What I don't know if it's possible to evaluate if a device has changed x times in a period of time. could be possible?

Thanx in advance. Very interesting all the capabilities the system offer. It's great. I've achieved already control surplus of PV installation and give power to three resistences (in waterfall sequence, for heating house) and system looks very robust¡¡

Re: activate from change state in a device

Posted: Saturday 23 January 2021 17:52
by waaren
zuputrus wrote: Saturday 23 January 2021 16:47 I want to give a variable a value (value 1) if a device changes its state 3 or more times in a minute. In other case value should be 0 by default.
In that way, I can use that variable to start one or other script depending its value.
What I don't know if it's possible to evaluate if a device has changed x times in a period of time. could be possible?
Could look like below example

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            'myDevice', -- change to your device name
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = '3 in a row',
    },

    data =
    {
        deviceHistory =
        {
            initial = 
            { 
                firstTime = ( os.time() - 61 ),
                counter = 0,
                lastState = 'unknown', 
            },
        },
    },

    execute = function(dz, item)
        
        local myVar = dz.variables('threeChanges') -- your varname as type integer with initial value of 0            
        
        if dz.data.deviceHistory.lastState ~= item.state then
            dz.log(item.name ..' changed state from ' .. dz.data.deviceHistory.lastState .. ' to ' .. item.state
                            .. ' (' .. item.lastUpdate.secondsAgo .. ' seconds ago)' , dz.LOG_DEBUG )
            dz.data.deviceHistory.lastState = item.state
            if dz.data.deviceHistory.firstTime < ( os.time() - 60 ) then  -- last State change more then 60 seconds ago
                dz.data.deviceHistory.firstTime = os.time()
                dz.data.deviceHistory.counter = 1
            else
                dz.data.deviceHistory.counter = dz.data.deviceHistory.counter + 1
                dz.log('Number of times the state changed is ' .. dz.data.deviceHistory.counter, dz.LOG_DEBUG )
                if dz.data.deviceHistory.counter >= 3 then
                    if myVar.value == 0 then
                        dz.log('Setting the var to 1', dz.LOG_DEBUG )
                        myVar.cancelQueuedCommands()
                        myVar.set(1)
                        myVar.set(0).afterSec(60).silent()
                        dz.data.deviceHistory.counter = 0
                    else
                        dz.log('Var already set to 1', dz.LOG_DEBUG )
                    end
                end
            end
        end
    end
}


Re: activate from change state in a device

Posted: Sunday 24 January 2021 10:02
by zuputrus
Thanks a lot Waaren¡¡ I'm going to work with it. I'll tell you how it works. Great¡