Timer script to turn off/on device  [Solved]

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

Moderator: leecollings

gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Timer script to turn off/on device

Post by gschmidt »

Hi,

I want to create a dzvents script that only turnes off a switch beween a time frame (02:00 - 06:30) and also If all smartphones are offline for 20 minutes (away) outside the time frame. I have installed iDetect plug-in, which also creates an on/off switch iDetect-home if all devices are online.

IF time is 02:00, turn off switch, and stays off even if iDetect-home switch is ON between 02:00-06:00
ELSEIF iDetect-home switch is off for 20 minutes, turn off switch
IF time is 06:30 and/or iDetect-home switch is on, turn on Switch.

Does anyone has an example script?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Timer script to turn off/on device

Post by waaren »

gschmidt wrote: Saturday 17 October 2020 10:22 IF time is 02:00, turn off switch, and stays off even if iDetect-home switch is ON between 02:00-06:00
ELSEIF iDetect-home switch is off for 20 minutes, turn off switch
IF time is 06:30 and/or iDetect-home switch is on, turn on Switch.
Something like this?

Code: Select all

return 
{
    on = 
    {
        timer =
        {
            'at 02:00',
            'at 06:30',
        },
        
        devices = 
        {
            ['iDetect-home'] = 
            { 
                'at 06:00-02:00',
            },
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
        marker = 'Time/Phones controlled switch',
    },

    execute = function(dz, item)
        local switch = dz.device('switch') -- change to name of switch
        local iDetect = dz.device('iDetect-home')

        if dz.time.matchesRule('at 02:00-06:00') then
           switch.switchOff.checkFirst()
        elseif iDetect.state == 'Off' then
           switch.switchOff.afterMin(20)
        elseif iDetect.state == 'On' then 
            switch.cancelQueuedCommands()
            if dz.time.matchesRule('at 06:30-01:59') then
                switch.switchOn().checkFirst()
            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
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Timer script to turn off/on device

Post by gschmidt »

waaren wrote: Saturday 17 October 2020 11:29
Something like this?

Code: Select all

return 
{
    on = 
    {
        timer =
        {
            'at 02:00',
            'at 06:30',
        },
        
        devices = 
        {
            ['iDetect-home'] = 
            { 
                'at 06:00-02:00',
            },
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
        marker = 'Time/Phones controlled switch',
    },

    execute = function(dz, item)
        local switch = dz.devices('switch') -- change to name of switch
        local iDetect = dz.devices('iDetect-home')

        if dz.time.matchesRule('at 02:00-06:00') then
           switch.switchOff.checkFirst()
        elseif iDetect.state == 'Off' then
           switch.switchOff.afterMin(20)
        elseif iDetect.state == 'On' then 
            switch.cancelQueuedCommands()
            if dz.time.matchesRule('at 06:30-01:59') then
                switch.switchOn().checkFirst()
            end
        end
            
    end
}
Wow,that is quick, thanx!

I guess that 6:00 Must be 0.6:30?

Code: Select all

if dz.time.matchesRule('at 02:00-06:00') then
I did not understand how to set up the timer, just add the start and end times and the code below does the trick?:

Code: Select all

dz.time.matchesRule('at 02:00-06:30')
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Timer script to turn off/on device

Post by waaren »

gschmidt wrote: Saturday 17 October 2020 13:49 I guess that 6:00 Must be 06:30?
No; according to you description is should be 06:00 but you can change it to whatever you like.
I did not understand how to set up the timer, just add the start and end times and the code below does the trick?:
the at 02:00 and at 06:30 will trigger the script only at those moments.

The ['iDetect-home'] = { 'at 06:00-02:00', }, will trigger the script if the iDetect-home is switched between 06:00 and 02:00 (next day)

btw. noticed that I forgot the at on some places in my original post. Corrected it there now.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Timer script to turn off/on device

Post by gschmidt »

waaren wrote: Saturday 17 October 2020 14:31
gschmidt wrote: Saturday 17 October 2020 13:49 I guess that 6:00 Must be 06:30?
No; according to you description is should be 06:00 but you can change it to whatever you like.
I did not understand how to set up the timer, just add the start and end times and the code below does the trick?:
the at 02:00 and at 06:30 will trigger the script only at those moments.

The ['iDetect-home'] = { 'at 06:00-02:00', }, will trigger the script if the iDetect-home is switched between 06:00 and 02:00 (next day)

btw. noticed that I forgot the at on some places in my original post. Corrected it there now.
You are right I mistyped it in my description it should be 06:30...
I have changed it and also added the 'at ' at those places thanx!
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Timer script to turn off/on device

Post by gschmidt »

waaren wrote: Saturday 17 October 2020 14:31
gschmidt wrote: Saturday 17 October 2020 13:49 I guess that 6:00 Must be 06:30?
No; according to you description is should be 06:00 but you can change it to whatever you like.
I did not understand how to set up the timer, just add the start and end times and the code below does the trick?:
the at 02:00 and at 06:30 will trigger the script only at those moments.

The ['iDetect-home'] = { 'at 06:00-02:00', }, will trigger the script if the iDetect-home is switched between 06:00 and 02:00 (next day)

btw. noticed that I forgot the at on some places in my original post. Corrected it there now.
One more thing, this switch is turning on/off an electrical cat fence.
What happens if I turn off the switch manually (for maintenance), will this script turn it back on?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Timer script to turn off/on device

Post by waaren »

gschmidt wrote: Saturday 17 October 2020 15:45 One more thing, this switch is turning on/off an electrical cat fence.
What happens if I turn off the switch manually (for maintenance), will this script turn it back on?
Yes. If the switch iDetect is updated to On between 6:00 and 2:00 (next day)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Timer script to turn off/on device

Post by gschmidt »

waaren wrote: Saturday 17 October 2020 19:34
gschmidt wrote: Saturday 17 October 2020 15:45 One more thing, this switch is turning on/off an electrical cat fence.
What happens if I turn off the switch manually (for maintenance), will this script turn it back on?
Yes. If the switch iDetect is updated to On between 6:00 and 2:00 (next day)
Well I have tested this and the script is not even triggered if I turn off the switch manually, the iDetect-home switch state was already On. Or is the script only triggered if the iDetect switch went from off to on or on to off?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Timer script to turn off/on device

Post by waaren »

gschmidt wrote: Sunday 18 October 2020 9:18 Well I have tested this and the script is not even triggered if I turn off the switch manually, the iDetect-home switch state was already On. Or is the script only triggered if the iDetect switch went from off to on or on to off?
I also tested it and the script is triggered (and produced and error that I corrected below. If the script is not triggered at all, something else is causing it.

Code: Select all

return 
{
    on = 
    {
        timer =
        {
            'at 02:00',
            'at 06:30',
        },

        devices = 
        {
            ['iDetect-home'] = 
            { 
                'at 06:00-02:00',
            },
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
        marker = 'Time/Phones controlled switch',
    },

    execute = function(dz, item)
        local switch = dz.devices('switch') -- change to name of switch
        local iDetect = dz.devices('iDetect-home')

        if dz.time.matchesRule('at 02:00-06:00') then
           switch.switchOff.checkFirst()
        elseif iDetect.state == 'Off' then
           switch.switchOff.afterMin(20)
        elseif iDetect.state == 'On' then 
            switch.cancelQueuedCommands()
            if dz.time.matchesRule('at 06:30-01:59') then
                switch.switchOn().checkFirst()
            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
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Timer script to turn off/on device

Post by gschmidt »

waaren wrote: Sunday 18 October 2020 15:14
gschmidt wrote: Sunday 18 October 2020 9:18 Well I have tested this and the script is not even triggered if I turn off the switch manually, the iDetect-home switch state was already On. Or is the script only triggered if the iDetect switch went from off to on or on to off?
I also tested it and the script is triggered (and produced and error that I corrected below. If the script is not triggered at all, something else is causing it.

Code: Select all

return 
{
    on = 
    {
        timer =
        {
            'at 02:00',
            'at 06:30',
        },

        devices = 
        {
            ['iDetect-home'] = 
            { 
                'at 06:00-02:00',
            },
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
        marker = 'Time/Phones controlled switch',
    },

    execute = function(dz, item)
        local switch = dz.devices('switch') -- change to name of switch
        local iDetect = dz.devices('iDetect-home')

        if dz.time.matchesRule('at 02:00-06:00') then
           switch.switchOff.checkFirst()
        elseif iDetect.state == 'Off' then
           switch.switchOff.afterMin(20)
        elseif iDetect.state == 'On' then 
            switch.cancelQueuedCommands()
            if dz.time.matchesRule('at 06:30-01:59') then
                switch.switchOn().checkFirst()
            end
        end
            
    end
}
Hi waaren,

The script was triggered last night and it produced an error, probably the same:

Code: Select all

2020-10-21 02:00:00.395 Error: dzVents: Error: (3.0.11) Time/Phones controlled switch: An error occurred when calling event handler Electric_Fence_On_Off
2020-10-21 02:00:00.395 Error: dzVents: Error: (3.0.11) Time/Phones controlled switch: ...ipts/dzVents/generated_scripts/Electric_Fence_On_Off.lua:29: attempt to call a nil value (field 'device')
What part did you modify, because I can’t find the difference? Where is item used in the script as in function(dz, item)?

This is my current script:

Code: Select all

return
{
    on = 
    {
        timer =
        {
            'at 02:00',
            'at 06:30',
        },
        
        devices = 
        {
            ['iDetect-Anyone'] = 
            { 
                'at 06:30-02:00',
            },
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
        marker = 'Time/Phones controlled switch',
    },

    execute = function(dz, item)
        local switch = dz.device('Stopcontacten') -- change to name of switch
        local iDetect = dz.device('iDetect-Anyone')

        if dz.time.matchesRule('at 02:00-06:30') then
           switch.switchOff.checkFirst()
        elseif iDetect.state == 'Off' then
           switch.switchOff.afterMin(20)
        elseif iDetect.state == 'On' then 
            switch.cancelQueuedCommands()
            if dz.time.matchesRule('at 06:30-01:59') then
                switch.switchOn().checkFirst()
            end
        end
        dz.notify(switch.name,'Cat fence (' .. switch.name .. ') is turned: '.. switch.state ..'!', dz.PRIORITY_NORMAL,'','',dz.NSS_TELEGRAM)            
    end
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Timer script to turn off/on device

Post by waaren »

gschmidt wrote: Wednesday 21 October 2020 7:21 The script was triggered last night and it produced an error, probably the same:
The correction was already in my previous post.

Change

Code: Select all

local switch = dz.device('Stopcontacten') -- change to name of switch
local iDetect = dz.device('iDetect-Anyone')
to

Code: Select all

local switch = dz.devices('Stopcontacten') -- change to name of switch
local iDetect = dz.devices('iDetect-Anyone')
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Timer script to turn off/on device

Post by gschmidt »

Ah....devices instead of device....thanx, will try tonight
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Timer script to turn off/on device

Post by gschmidt »

waaren wrote: Wednesday 21 October 2020 8:09
gschmidt wrote: Wednesday 21 October 2020 7:21 The script was triggered last night and it produced an error, probably the same:
The correction was already in my previous post.

Change

Code: Select all

local switch = dz.device('Stopcontacten') -- change to name of switch
local iDetect = dz.device('iDetect-Anyone')
to

Code: Select all

local switch = dz.devices('Stopcontacten') -- change to name of switch
local iDetect = dz.devices('iDetect-Anyone')
I tested it but found another error:

Code: Select all

 if dz.time.matchesRule('at 02:00-06:00') then
           switch.switchOff.checkFirst() — should be switch.switchOff().checkFirst()
        elseif iDetect.state == 'Off' then
           switch.switchOff.afterMin(20) — should be switch.switchOff().afterMin(20)
        elseif iDetect.state == 'On' then 
            switch.cancelQueuedCommands()
            if dz.time.matchesRule('at 06:30-01:59') then
                switch.switchOn().checkFirst()
            end
        end
What kind of function is: switch.cancelQueuedCommands()?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Timer script to turn off/on device

Post by waaren »

gschmidt wrote: Thursday 22 October 2020 18:55 I tested it but found another error:

Code: Select all

           switch.switchOff.checkFirst() — should be switch.switchOff().checkFirst()
👍Good catch
What kind of function is: switch.cancelQueuedCommands()?
From the dzVents wiki

cancelQueuedCommands(): Function. Cancels queued commands. E.g. you switch on a device after 10 minutes: myDevice.switchOn().afterMin(10). Within those 10 minutes you can cancel that command by calling: myDevice.cancelQueuedCommands().
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Timer script to turn off/on device

Post by gschmidt »

waaren wrote: Thursday 22 October 2020 19:20
gschmidt wrote: Thursday 22 October 2020 18:55 I tested it but found another error:

Code: Select all

           switch.switchOff.checkFirst() — should be switch.switchOff().checkFirst()
👍Good catch
What kind of function is: switch.cancelQueuedCommands()?
From the dzVents wiki

cancelQueuedCommands(): Function. Cancels queued commands. E.g. you switch on a device after 10 minutes: myDevice.switchOn().afterMin(10). Within those 10 minutes you can cancel that command by calling: myDevice.cancelQueuedCommands().
Hi waaren,

I am testing the script, something is not working correct.

Case 1: "Stopcontacten" is turned OFF at 02:00 which is correct, but also OFF at 06:30 ?? it should be turned ON at 06:30
Case 2: with the "iDetect-Anyone" switch the following happens:

1. After "iDetect-Anyone" is OFF, "Stopcontacten" is turned ON.... I also instantly get a telegram message that "Stopcontacten" is ON.....but also after 20m an ON message??? While "Stopcontacten" should be turned OFF.

2. When "iDetect-Anyone" is ON, "Stopcontacten" is turned ON....only one telegram message

This is the LOG for Case 2:

Code: Select all

2020-10-23 17:36:19.155 Status: dzVents: Info: Handling events for: "iDetect-Anyone", value: "Off"
2020-10-23 17:36:19.155 Status: dzVents: Info: Time/Phones controlled switch: ------ Start internal script: Electric_Fence_On_Off: Device: "iDetect-Anyone (iDetect)", Index: 198
2020-10-23 17:36:19.156 Status: dzVents: Debug: Time/Phones controlled switch: Processing device-adapter for Stopcontacten: Switch device adapter
2020-10-23 17:36:19.157 Status: dzVents: Debug: Time/Phones controlled switch: Constructed timed-command: Off
2020-10-23 17:36:19.157 Status: dzVents: Debug: Time/Phones controlled switch: Constructed timed-command: Off AFTER 1200 SECONDS
2020-10-23 17:36:19.157 Status: dzVents: Debug: Time/Phones controlled switch: Cat fence (Stopcontacten) is turned: On!
2020-10-23 17:36:19.157 Status: dzVents: Info: Time/Phones controlled switch: ------ Finished Electric_Fence_On_Off
2020-10-23 17:36:19.158 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-10-23 17:36:19.172 Status: Notification: Stopcontacten

2020-10-23 17:42:05.006 Status: dzVents: Info: Handling events for: "iDetect-Anyone", value: "On"
2020-10-23 17:42:05.007 Status: dzVents: Info: Time/Phones controlled switch: ------ Start internal script: Electric_Fence_On_Off: Device: "iDetect-Anyone (iDetect)", Index: 198
2020-10-23 17:42:05.008 Status: dzVents: Debug: Time/Phones controlled switch: Processing device-adapter for Stopcontacten: Switch device adapter
2020-10-23 17:42:05.008 Status: dzVents: Debug: Time/Phones controlled switch: Constructed timed-command: On
2020-10-23 17:42:05.008 Status: dzVents: Debug: Time/Phones controlled switch: Cat fence (Stopcontacten) is turned: On!
2020-10-23 17:42:05.008 Status: dzVents: Info: Time/Phones controlled switch: ------ Finished Electric_Fence_On_Off
2020-10-23 17:42:05.009 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-10-23 17:42:05.027 Status: Notification: Stopcontacten
This is my Current script:

Code: Select all

--Script to turn off the cat electric fence (Stopcontacten) between 02:00-06:30 or if nobody is at home

return 
{
    on = 
    {
        timer =
        {
            'at 02:00',
            'at 06:30',
        },
        
        devices = 
        {
            ['iDetect-Anyone'] = 
            { 
                'at 06:30-02:00',
            },
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
        marker = 'Time/Phones controlled switch',
    },

    execute = function(dz)
        local switch = dz.devices('Stopcontacten') -- change to name of switch
        local iDetect = dz.devices('iDetect-Anyone')

        if dz.time.matchesRule('at 02:00-06:30') then
           switch.switchOff().checkFirst()
        elseif iDetect.state == 'Off' then
           switch.switchOff().afterMin(20)
        elseif iDetect.state == 'On' then 
            switch.cancelQueuedCommands()
            if dz.time.matchesRule('at 06:30-01:59') then
                switch.switchOn().checkFirst()
            end
        end
        dz.notify(switch.name,'Cat fence (' .. switch.name .. ') is turned: '.. switch.state ..'!', dz.PRIORITY_NORMAL,'','',dz.NSS_TELEGRAM)
        dz.log('Cat fence (' .. switch.name .. ') is turned: '.. switch.state ..'!', dz.LOG_INFO)
    end
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Timer script to turn off/on device

Post by waaren »

gschmidt wrote: Friday 23 October 2020 17:29 I am testing the script, something is not working correct.
Case 1: "Stopcontacten" is turned OFF at 02:00 which is correct, but also OFF at 06:30 ?? it should be turned ON at 06:30
Below modified script should solve Case 1 issue but not yet the unexpected notifications. It's not completely clear to me in which cases you want to receive a notification.

Code: Select all

--Script to turn off the cat electric fence (Stopcontacten) between 02:00-06:30 or if nobody is at home

return 
{
    on = 
    {
        timer =
        {
            'at 02:00',
            'at 06:30',
        },
        
        devices = 
        {
            ['iDetect-Anyone'] = 
            { 
                'at 06:30-02:00',
            },
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
        marker = 'Time/Phones controlled switch',
    },

    execute = function(dz)
        local switch = dz.devices('Stopcontacten') -- change to name of switch
        local iDetect = dz.devices('iDetect-Anyone')

        if dz.time.matchesRule('at 02:00-06:29') then
           switch.switchOff().checkFirst()
        elseif iDetect.state == 'Off' then
           switch.switchOff().afterMin(20)
		   return
        elseif iDetect.state == 'On' then 
            switch.cancelQueuedCommands()
            if dz.time.matchesRule('at 06:30-01:59') then
                switch.switchOn().checkFirst()
            end
        end
        dz.notify(switch.name,'Cat fence (' .. switch.name .. ') is turned: '.. switch.state ..'!', dz.PRIORITY_NORMAL,'','',dz.NSS_TELEGRAM)
        dz.log('Cat fence (' .. switch.name .. ') is turned: '.. switch.state ..'!', dz.LOG_INFO)
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Timer script to turn off/on device

Post by gschmidt »

waaren wrote: Friday 23 October 2020 20:19 It's not completely clear to me in which cases you want to receive a notification.
Thanx, I will try tonight!

Regarding notifications:
dz & telegram notifications every ON and OFF.
In case of the iDetect-Anyone OFF after 20min, only notify OFF after 20m
But if iDetect-Home is ON again within those 20m, no notification is needed
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Timer script to turn off/on device

Post by waaren »

gschmidt wrote: Saturday 24 October 2020 9:05 Regarding notifications:
dz & telegram notifications every ON and OFF.
In case of the iDetect-Anyone OFF after 20min, only notify OFF after 20m
But if iDetect-Home is ON again within those 20m, no notification is needed
Like this?

Code: Select all

--Script to turn off the cat electric fence (Stopcontacten) between 02:00-06:30 or if nobody is at home

return
{
    on =
    {
        timer =
        {
            'at 02:00',
            'at 06:30',
        },

        devices =
        {
            ['iDetect-Anyone'] =
            {
                'at 06:30-02:00',
            },
            'Stopcontacten',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
        marker = 'Time/Phones controlled switch',
    },

    execute = function(dz)
        local switch = dz.devices('Stopcontacten') -- change to name of switch
        local iDetect = dz.devices('iDetect-Anyone')

        local function notify(targetState)
            local msg = 'Cat fence (' .. switch.name .. ') has been switched to ' .. targetState ..'!'
            dz.notify(switch.name,msg, dz.PRIORITY_NORMAL,'','',dz.NSS_TELEGRAM)
            dz.log(msg, dz.LOG_INFO)
        end

        dz.log(switch.name .. ' state: ' .. switch.state , dz.LOG_DEBUG)
        dz.log(iDetect.name .. ' state: ' .. iDetect.state , dz.LOG_DEBUG)

        if item == switch then
            notify(switch.state)
        elseif dz.time.matchesRule('at 02:00-06:29') then
            if switch.state ~= 'Off' then
                switch.switchOff().silent()
                notify('Off')
            end
        elseif iDetect.state == 'Off' and switch.state ~= 'Off' then
            switch.switchOff().afterMin(20)
        elseif iDetect.state == 'On' then
            switch.cancelQueuedCommands()
            if dz.time.matchesRule('at 06:30-01:59') and switch.state ~= 'On' then
                switch.switchOn().silent()
                notify('On')
            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
gschmidt
Posts: 200
Joined: Thursday 20 December 2018 11:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Timer script to turn off/on device

Post by gschmidt »

waaren wrote: Saturday 24 October 2020 10:21
gschmidt wrote: Saturday 24 October 2020 9:05 Regarding notifications:
dz & telegram notifications every ON and OFF.
In case of the iDetect-Anyone OFF after 20min, only notify OFF after 20m
But if iDetect-Home is ON again within those 20m, no notification is needed
Like this?

Code: Select all

--Script to turn off the cat electric fence (Stopcontacten) between 02:00-06:30 or if nobody is at home

return
{
    on =
    {
        timer =
        {
            'at 02:00',
            'at 06:30',
        },

        devices =
        {
            ['iDetect-Anyone'] =
            {
                'at 06:30-02:00',
            },
            'Stopcontacten',
        },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- change to domoticz.LOG_ERROR when all OK
        marker = 'Time/Phones controlled switch',
    },

    execute = function(dz)
        local switch = dz.devices('Stopcontacten') -- change to name of switch
        local iDetect = dz.devices('iDetect-Anyone')

        local function notify(targetState)
            local msg = 'Cat fence (' .. switch.name .. ') has been switched to ' .. targetState ..'!'
            dz.notify(switch.name,msg, dz.PRIORITY_NORMAL,'','',dz.NSS_TELEGRAM)
            dz.log(msg, dz.LOG_INFO)
        end

        dz.log(switch.name .. ' state: ' .. switch.state , dz.LOG_DEBUG)
        dz.log(iDetect.name .. ' state: ' .. iDetect.state , dz.LOG_DEBUG)

        if item == switch then
            notify(switch.state)
        elseif dz.time.matchesRule('at 02:00-06:29') then
            if switch.state ~= 'Off' then
                switch.switchOff().silent()
                notify('Off')
            end
        elseif iDetect.state == 'Off' and switch.state ~= 'Off' then
            switch.switchOff().afterMin(20)
        elseif iDetect.state == 'On' then
            switch.cancelQueuedCommands()
            if dz.time.matchesRule('at 06:30-01:59') and switch.state ~= 'On' then
                switch.switchOn().silent()
                notify('On')
            end
        end
    end
}
Hi waaren,

Tested your script by turning off wifi of all 3 phones...so iDetect-Anyone is OFF now.
20min has not been passed yet. This is the log:

Code: Select all

2020-10-24 11:13:48.453 Status: dzVents: Info: Handling events for: "iDetect-Anyone", value: "Off"
2020-10-24 11:13:48.453 Status: dzVents: Info: Time/Phones controlled switch: ------ Start internal script: Electric_Fence_On_Off: Device: "iDetect-Anyone (iDetect)", Index: 198
2020-10-24 11:13:48.454 Status: dzVents: Debug: Time/Phones controlled switch: Processing device-adapter for Stopcontacten: Switch device adapter
2020-10-24 11:13:48.454 Status: dzVents: Debug: Time/Phones controlled switch: Stopcontacten state: On
2020-10-24 11:13:48.454 Status: dzVents: Debug: Time/Phones controlled switch: iDetect-Anyone state: Off
2020-10-24 11:13:48.455 Status: dzVents: Debug: Time/Phones controlled switch: Constructed timed-command: Off
2020-10-24 11:13:48.455 Status: dzVents: Debug: Time/Phones controlled switch: Constructed timed-command: Off AFTER 1200 SECONDS
2020-10-24 11:13:48.455 Status: dzVents: Info: Time/Phones controlled switch: ------ Finished Electric_Fence_On_Off
2020-10-24 11:13:48.457 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
dz LOG produces "Stopcontacten state: On"....which is correct because it should be turned OFF after 20min
Telegram: no notification yet, which is correct because it should be send after 20min.

b.t.w. I also got a dz notification in the LOG which I don't recognize?:

Code: Select all

2020-10-24 11:18:20.760 Status: ::ffff:192.168.1.1 -> handle abandoned timeout (status=6)
this is a message from my main router...I always open domoticz in the browser with a safe hpps connection (duckdns)
Does that procduce this message because each command in dz passes the WAN connection?

OK after 20min the script turns "Stopcontacten state: OFF", but no Telegram message is send

Code: Select all

2020-10-24 11:33:48.723 Status: dzVents: Info: Handling events for: "Stopcontacten", value: "Off"
2020-10-24 11:33:48.724 Status: dzVents: Info: Time/Phones controlled switch: ------ Start internal script: Electric_Fence_On_Off: Device: "Stopcontacten (Zigbee2MQTT)", Index: 374
2020-10-24 11:33:48.725 Status: dzVents: Debug: Time/Phones controlled switch: Processing device-adapter for iDetect-Anyone: Switch device adapter
2020-10-24 11:33:48.725 Status: dzVents: Debug: Time/Phones controlled switch: Stopcontacten state: Off
2020-10-24 11:33:48.725 Status: dzVents: Debug: Time/Phones controlled switch: iDetect-Anyone state: Off
2020-10-24 11:33:48.725 Status: dzVents: Info: Time/Phones controlled switch: ------ Finished Electric_Fence_On_Off
Then I turned on the Wifi of the phones and iDetect-Anyone state went: ON. Both dz and telegram produce a Message.
However dz notifies: Stopcontacten state: Off, shoudn't that be ON?

Code: Select all

2020-10-24 11:36:51.676 Status: dzVents: Info: Handling events for: "iDetect-Anyone", value: "On"
2020-10-24 11:36:51.676 Status: dzVents: Info: Time/Phones controlled switch: ------ Start internal script: Electric_Fence_On_Off: Device: "iDetect-Anyone (iDetect)", Index: 198
2020-10-24 11:36:51.677 Status: dzVents: Debug: Time/Phones controlled switch: Processing device-adapter for Stopcontacten: Switch device adapter
2020-10-24 11:36:51.677 Status: dzVents: Debug: Time/Phones controlled switch: Stopcontacten state: Off
2020-10-24 11:36:51.677 Status: dzVents: Debug: Time/Phones controlled switch: iDetect-Anyone state: On
2020-10-24 11:36:51.678 Status: dzVents: Debug: Time/Phones controlled switch: Constructed timed-command: On
2020-10-24 11:36:51.678 Status: dzVents: Debug: Time/Phones controlled switch: Constructed timed-command: On NOTRIGGER
2020-10-24 11:36:51.678 Status: dzVents: Info: Time/Phones controlled switch: Cat fence (Stopcontacten) has been switched to On!
2020-10-24 11:36:51.678 Status: dzVents: Info: Time/Phones controlled switch: ------ Finished Electric_Fence_On_Off
2020-10-24 11:36:51.681 Status: EventSystem: Script event triggered: /home/pi/domoticz/dzVents/runtime/dzVents.lua
2020-10-24 11:36:51.705 Status: Notification: Stopcontacten
2020-10-24 11:36:52.147 Notification sent (telegram) => Success
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Timer script to turn off/on device

Post by waaren »

gschmidt wrote: Saturday 24 October 2020 11:46 Then I turned on the Wifi of the phones and iDetect-Anyone state went: ON. Both dz and telegram produce a Message.
However dz notifies: Stopcontacten state: Off, shoudn't that be ON?
can you try again after changing

Code: Select all

    execute = function(dz)
to

Code: Select all

    execute = function(dz, item)
If still not OK then please continue via PM.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest