How to set timer - need to have active two sensors in specific time.  [Solved]

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

Moderator: leecollings

Post Reply
nowy
Posts: 7
Joined: Wednesday 21 October 2020 19:26
Target OS: -
Domoticz version:
Contact:

How to set timer - need to have active two sensors in specific time.

Post by nowy »

Hi All,
I need dzvents script that will activate the switch if at least two any motion sensors (from 10 sensors) are active simultaneously in last 5 minutes.
So if first sensor detects the move then timer starts and if in the next 5 minutes second sensor discovers move then switch will be activated.
To be honest I didn't find option in doc how to achive it.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to set timer - need to have active two sensors in specific time.

Post by waaren »

nowy wrote: Wednesday 21 October 2020 20:24 To be honest I didn't find option in doc how to achieve it.
The wiki cannot give a dzVents solution for all automation challenges. It provides a description of the framework , methods and attributes you can use to create scripts up to your task.

I need dzvents script that will activate the switch if at least two any motion sensors (from 10 sensors) are active simultaneously in last 5 minutes.
Something like below should get you started.

Code: Select all

-- 2 out of 10

local sensors =           -- table with names of all your motion sensors
{
    'motion Sensor1',
    'motion Sensor2',
    'motion Sensor3',
    'motion Sensor4',
    'motion Sensor5',
    'motion Sensor6',
    'motion Sensor7',
    'motion Sensor8',
    'motion Sensor9',
    'motion Sensor10',
}

local theSwitch = 'the switch' -- change to name of your switch

return
{
    on =
    {
        devices = sensors,
    },

    data =
    {
        motionSensors =
        {
            initial = {},
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
        marker = 'scene Control',
    },

    execute = function(dz, item)

        local function saveSensorActivationTime(str)
            dz.data.motionSensors[str] = os.time()
        end

        local function twoOutOfTen()
            local active = 0

            for _, sensorName in ipairs(sensors) do
                local seconds = os.time() -(  dz.data.motionSensors[sensorName] or 0 )
                if seconds < 300  then
                    dz.log(sensorName .. ' was activated ' .. seconds .. ' seconds ago.', dz.LOG_DEBUG )
                    active = active + 1
                    if active > 1 then return true end
                end
            end
        end

        if item.active then
            saveSensorActivationTime(item.name)
            if twoOutOfTen() then
                 dz.devices(theSwitch).switchOn() 
            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
nowy
Posts: 7
Joined: Wednesday 21 October 2020 19:26
Target OS: -
Domoticz version:
Contact:

Re: How to set timer - need to have active two sensors in specific time.

Post by nowy »

Where should I place additional condition like if domoticz.devices('Alarm').state == 'On') then execute rest function so turn on the switch if at least these two sensors are active.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to set timer - need to have active two sensors in specific time.

Post by waaren »

nowy wrote: Wednesday 21 October 2020 23:18 Where should I place additional condition like if domoticz.devices('Alarm').state == 'On') then execute rest function so turn on the switch if at least these two sensors are active.
There are multiple approaches to that but to keep it simple change

Code: Select all

if twoOutOfTen() then
    dz.devices(theSwitch).switchOn() 
end
to

Code: Select all

if twoOutOfTen() and dz.devices('Alarm').state == 'On'  then
    dz.devices(theSwitch).switchOn() 
end
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
nowy
Posts: 7
Joined: Wednesday 21 October 2020 19:26
Target OS: -
Domoticz version:
Contact:

Re: How to set timer - need to have active two sensors in specific time.

Post by nowy »

Tested and ...
Script works well when I added two tested switches and manually activated them but when I use my motion sensors scripts starts and right away finish. What can be wrong?

Code: Select all

2020-10-22 00:15:14.624 Status: dzVents: Info: XXX: ------ Start internal script: Test: Device: "Garaż (Dummy)", Index: 135
2020-10-22 00:15:14.626 Status: dzVents: Info: XXX: ------ Finished Test
Here is current script I have

Code: Select all

local sensors =           -- table with names of all your motion sensors
{
    'Salon',
    'Garaż',
    'Kuchnia',
}

local theSwitch = 'Włamanie noc' -- change to name of your switch

return
{
    on =
    {
        devices = sensors,
    },
    data =
    {
        motionSensors =
        {
            initial = {},
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
        marker = 'XXX',
    },
    execute = function(dz, item)
        local function saveSensorActivationTime(str)
            dz.data.motionSensors[str] = os.time()
        end
        local function twoOutOfTen()
            local active = 0
            for _, sensorName in ipairs(sensors) do
                local seconds = os.time() -(  dz.data.motionSensors[sensorName] or 0 )
                if seconds < 300  then
                    dz.log(sensorName .. ' was activated ' .. seconds .. ' seconds ago.', dz.LOG_DEBUG )
                    active = active + 1
                    if active > 1 then return true end
                end
            end
        end
        if item.active then
            saveSensorActivationTime(item.name)
            if twoOutOfTen()  and dz.devices('Alarm noc').state == 'On'  then
                 dz.devices(theSwitch).switchOn() 
            end
        end
end
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to set timer - need to have active two sensors in specific time.

Post by waaren »

nowy wrote: Thursday 22 October 2020 0:20 Tested and ...
Script works well when I added two tested switches and manually activated them but when I use my motion sensors scripts starts and right away finish. What can be wrong?
It seems that your motion sensors do not report that they are active or your Alarm does not report it has the 'On' state.

If a script does not work as expected and the log does not show sufficient information to understand why, a first step to find a possible cause is to add more (debug)log statements... I added some in below script.

Code: Select all

local sensors =           -- table with names of all your motion sensors
{
    'Salon',
    'Garaż',
    'Kuchnia',
}

local theSwitch = 'Włamanie noc' -- change to name of your switch
local myAlarm = 'Alarm noc' -- change to name of your alarm

return
{
    on =
    {
        devices = sensors,
    },

    data =
    {
        motionSensors =
        {
            initial = {},
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
        marker = 'scene Control',
    },

    execute = function(dz, item)

        local alarm = dz.devices(myAlarm)
        local switch = dz.devices(theSwitch)

        local function saveSensorActivationTime(str)
            dz.data.motionSensors[str] = os.time()
            dz.log('os.time() is now: ' .. os.time(), dz.LOG_DEBUG )
            if  _G.logLevel == dz.LOG_DEBUG then
                dz.utils.dumpTable(dz.data) -- show the content of persistent data
            end
        end

        local function twoOutOfTen()
            local active = 0

            for _, sensorName in ipairs(sensors) do
                if dz.data.motionSensors[sensorName] ~= nil then
                    local seconds = os.time() -  dz.data.motionSensors[sensorName]
                    dz.log(sensorName .. ' was activated ' .. seconds .. ' seconds ago.', dz.LOG_DEBUG )
                    if seconds < 300  then
                        active = active + 1
                    end
                 end
            end
            
            return active > 1 
        end

        dz.log(item.name .. ' state: ' .. tostring(item.state) .. '; active state: ' .. tostring(item.active), dz.LOG_DEBUG)

        if item.active then
            saveSensorActivationTime(item.name)
            dz.log(alarm.name .. ' state: ' .. tostring(alarm.state), dz.LOG_DEBUG)
            if twoOutOfTen() and alarm.state == 'On' then
                 dz.log('Switch ' .. switch.name .. ' will be activated',dz.LOG_DEBUG)
                 switch.switchOn()
            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
nowy
Posts: 7
Joined: Wednesday 21 October 2020 19:26
Target OS: -
Domoticz version:
Contact:

Re: How to set timer - need to have active two sensors in specific time.

Post by nowy »

Here you are logs:

Code: Select all

2020-10-22 12:19:16.629 Status: dzVents: Info: scene Control: ------ Start internal script: Test: Device: "Garaż (Dummy)", Index: 135
2020-10-22 12:19:16.632 Status: dzVents: Debug: scene Control: Processing device-adapter for Alarm noc: Switch device adapter
2020-10-22 12:19:16.634 Status: dzVents: Debug: scene Control: Processing device-adapter for Włamanie noc: Switch device adapter
2020-10-22 12:19:16.634 Status: dzVents: Debug: scene Control: Garaż state: 21.70;48.64;1; active state: false
2020-10-22 12:19:16.635 Status: dzVents: Info: scene Control: ------ Finished Test
2020-10-22 12:19:21.935 Status: dzVents: Info: scene Control: ------ Start internal script: Test: Device: "Salon (Dummy)", Index: 183
2020-10-22 12:19:21.938 Status: dzVents: Debug: scene Control: Processing device-adapter for Alarm noc: Switch device adapter
2020-10-22 12:19:21.940 Status: dzVents: Debug: scene Control: Processing device-adapter for Włamanie noc: Switch device adapter
2020-10-22 12:19:21.940 Status: dzVents: Debug: scene Control: Salon state: 24.60;49.67;1; active state: false
2020-10-22 12:19:21.941 Status: dzVents: Info: scene Control: ------ Finished Test
2020-10-22 12:19:46.622 Status: dzVents: Info: scene Control: ------ Start internal script: Test: Device: "Garaż (Dummy)", Index: 135
2020-10-22 12:19:46.625 Status: dzVents: Debug: scene Control: Processing device-adapter for Alarm noc: Switch device adapter
2020-10-22 12:19:46.627 Status: dzVents: Debug: scene Control: Processing device-adapter for Włamanie noc: Switch device adapter
2020-10-22 12:19:46.627 Status: dzVents: Debug: scene Control: Garaż state: 21.63;48.62;1; active state: false
2020-10-22 12:19:46.628 Status: dzVents: Info: scene Control: ------ Finished Test
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to set timer - need to have active two sensors in specific time.  [Solved]

Post by waaren »

nowy wrote: Thursday 22 October 2020 12:20 Here you are logs:
It looks like the devices that you put in the sensors table are temperature / humidity devices. The script expect motion sensors...
can you use this modified script (with an extra logline to identify the deviceType and deviceSubType)

Code: Select all

local sensors =           -- table with names of all your motion sensors
{
    'Salon',
    'Garaż',
    'Kuchnia',
}

local theSwitch = 'Włamanie noc' -- change to name of your switch
local myAlarm = 'Alarm noc' -- change to name of your alarm

return
{
    on =
    {
        devices = sensors,
    },

    data =
    {
        motionSensors =
        {
            initial = {},
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
        marker = 'scene Control',
    },

    execute = function(dz, item)

        local alarm = dz.devices(myAlarm)
        local switch = dz.devices(theSwitch)

        local function saveSensorActivationTime(str)
            dz.data.motionSensors[str] = os.time()
            dz.log('os.time() is now: ' .. os.time(), dz.LOG_DEBUG )
            if  _G.logLevel == dz.LOG_DEBUG then
                dz.utils.dumpTable(dz.data) -- show the content of persistent data
            end
        end

        local function twoOutOfTen()
            local active = 0

            for _, sensorName in ipairs(sensors) do
                if dz.data.motionSensors[sensorName] ~= nil then
                    local seconds = os.time() -  dz.data.motionSensors[sensorName]
                    dz.log(sensorName .. ' was activated ' .. seconds .. ' seconds ago.', dz.LOG_DEBUG )
                    if seconds < 300  then
                        active = active + 1
                    end
                 end
            end
            
            return active > 1 
        end

        dz.log(item.name .. ' state: ' .. tostring(item.state) .. '; active state: ' .. tostring(item.active),  dz.LOG_DEBUG )
        dz.log(item.name .. ' (' .. item.idx ..');  deviceType: ' .. tostring(item.deviceType) .. '; deviceSubType: ' .. tostring(item.deviceSubType), dz.LOG_DEBUG )

        if item.active then
            saveSensorActivationTime(item.name)
            dz.log(alarm.name .. ' state: ' .. tostring(alarm.state), dz.LOG_DEBUG)
            if twoOutOfTen() and alarm.state == 'On' then
                 dz.log('Switch ' .. switch.name .. ' will be activated',dz.LOG_DEBUG)
                 switch.switchOn()
            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
nowy
Posts: 7
Joined: Wednesday 21 October 2020 19:26
Target OS: -
Domoticz version:
Contact:

Re: How to set timer - need to have active two sensors in specific time.

Post by nowy »

Sorry my fault I have also the temp+hum sensors behind the motion sensors - I disabled them to not confused log output.

Motion sensors are Light/Switch type (wireless 433MHz) with Sonoff RF Bridge.

Code: Select all

2020-10-22 14:14:26.580 Status: dzVents: Info: scene Control: ------ Finished Test
2020-10-22 14:14:30.212 MQTT: Topic: domoticz/in, Message: {"idx":10,"nvalue":0,"svalue":"15621550","Battery":59,"RSSI":6}
2020-10-22 14:14:30.579 (Dummy) Light/Switch (Salon)
2020-10-22 14:14:30.539 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-10-22 14:14:31.368 MQTT: Topic: domoticz/in, Message: {"idx":59,"nvalue":0,"svalue":"15621550","Battery":57,"RSSI":9}
2020-10-22 14:14:39.083 MQTT: Topic: domoticz/in, Message: {"idx":10,"nvalue":0,"svalue":"15683422","Battery":59,"RSSI":6}
2020-10-22 14:14:39.206 MQTT: Topic: domoticz/in, Message: {"idx":59,"nvalue":0,"svalue":"15683422","Battery":57,"RSSI":9}
2020-10-22 14:14:43.750 MQTT: Topic: domoticz/in, Message: {"idx":10,"nvalue":0,"svalue":"7799975","Battery":59,"RSSI":6}
2020-10-22 14:14:44.522 MQTT: Topic: domoticz/in, Message: {"idx":59,"nvalue":0,"svalue":"15599950","Battery":57,"RSSI":9}
2020-10-22 14:14:50.174 MQTT: Topic: domoticz/in, Message: {"idx":10,"nvalue":0,"svalue":"15603710","Battery":60,"RSSI":6}
2020-10-22 14:14:51.431 MQTT: Topic: domoticz/in, Message: {"idx":10,"nvalue":0,"svalue":"15599950","Battery":59,"RSSI":6}
2020-10-22 14:14:51.556 MQTT: Topic: domoticz/in, Message: {"idx":59,"nvalue":0,"svalue":"15599950","Battery":57,"RSSI":9}
2020-10-22 14:14:57.082 MQTT: Topic: domoticz/in, Message: {"idx":59,"nvalue":0,"svalue":"15634430","Battery":57,"RSSI":9}
2020-10-22 14:14:57.205 MQTT: Topic: domoticz/in, Message: {"idx":10,"nvalue":0,"svalue":"15634430","Battery":59,"RSSI":7}
2020-10-22 14:15:04.637 MQTT: Topic: domoticz/in, Message: {"idx":10,"nvalue":0,"svalue":"14761994","Battery":59,"RSSI":5}
2020-10-22 14:15:04.961 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-10-22 14:15:05.012 (Dummy) Light/Switch (Drzwi garaż)
2020-10-22 14:15:06.238 MQTT: Topic: domoticz/in, Message: {"idx":59,"nvalue":0,"svalue":"15506830","Battery":57,"RSSI":9}
2020-10-22 14:15:06.581 (Dummy) Light/Switch (Garaż)
2020-10-22 14:15:06.560 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-10-22 14:15:07.131 MQTT: Topic: domoticz/in, Message: {"idx":10,"nvalue":0,"svalue":"15506830","Battery":59,"RSSI":5}
2020-10-22 14:15:11.861 MQTT: Topic: domoticz/in, Message: {"idx":59,"nvalue":0,"svalue":"14761994","Battery":57,"RSSI":9}
2020-10-22 14:15:12.221 (Dummy) Light/Switch (Drzwi garaż)
2020-10-22 14:15:12.508 MQTT: Topic: domoticz/in, Message: {"idx":1
2020-10-22 14_19_03-Domoticz.png
2020-10-22 14_19_03-Domoticz.png (95.75 KiB) Viewed 708 times
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests