activate from change state in a device

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

Moderator: leecollings

Post Reply
zuputrus
Posts: 9
Joined: Saturday 23 January 2021 16:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: Madrid. Spain
Contact:

activate from change state in a device

Post 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¡¡
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: activate from change state in a device

Post 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
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
zuputrus
Posts: 9
Joined: Saturday 23 January 2021 16:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.1
Location: Madrid. Spain
Contact:

Re: activate from change state in a device

Post by zuputrus »

Thanks a lot Waaren¡¡ I'm going to work with it. I'll tell you how it works. Great¡
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest