Timer script to turn off/on device [Solved]
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
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?
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?
- 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
Something like this?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.
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
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
Wow,that is quick, thanx!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 }
I guess that 6:00 Must be 0.6:30?
Code: Select all
if dz.time.matchesRule('at 02:00-06:00') thenCode: Select all
dz.time.matchesRule('at 02:00-06:30')- 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
No; according to you description is should be 06:00 but you can change it to whatever you like.
the at 02:00 and at 06:30 will trigger the script only at those moments.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 ['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
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
You are right I mistyped it in my description it should be 06:30...waaren wrote: Saturday 17 October 2020 14:31No; according to you description is should be 06:00 but you can change it to whatever you like.
the at 02:00 and at 06:30 will trigger the script only at those moments.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 ['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.
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
One more thing, this switch is turning on/off an electrical cat fence.waaren wrote: Saturday 17 October 2020 14:31No; according to you description is should be 06:00 but you can change it to whatever you like.
the at 02:00 and at 06:30 will trigger the script only at those moments.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 ['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.
What happens if I turn off the switch manually (for maintenance), will this script turn it back on?
- 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
Yes. If the switch iDetect is updated to On between 6:00 and 2:00 (next day)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?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
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
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?waaren wrote: Saturday 17 October 2020 19:34Yes. If the switch iDetect is updated to On between 6:00 and 2:00 (next day)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?
- 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
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.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?
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
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
Hi waaren,waaren wrote: Sunday 18 October 2020 15:14I 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.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?
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 }
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')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
}- 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
The correction was already in my previous post.gschmidt wrote: Wednesday 21 October 2020 7:21 The script was triggered last night and it produced an error, probably the same:
Change
Code: Select all
local switch = dz.device('Stopcontacten') -- change to name of switch
local iDetect = dz.device('iDetect-Anyone')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
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
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
I tested it but found another error:waaren wrote: Wednesday 21 October 2020 8:09The correction was already in my previous post.gschmidt wrote: Wednesday 21 October 2020 7:21 The script was triggered last night and it produced an error, probably the same:
ChangetoCode: Select all
local switch = dz.device('Stopcontacten') -- change to name of switch local iDetect = dz.device('iDetect-Anyone')Code: Select all
local switch = dz.devices('Stopcontacten') -- change to name of switch local iDetect = dz.devices('iDetect-Anyone')
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
- 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
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()
From the dzVents wikiWhat kind of function is: switch.cancelQueuedCommands()?
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
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
Hi waaren,waaren wrote: Thursday 22 October 2020 19:20gschmidt 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
From the dzVents wikiWhat kind of function is: switch.cancelQueuedCommands()?
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().
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: StopcontactenCode: 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
}- 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
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.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
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
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
Thanx, I will try tonight!waaren wrote: Friday 23 October 2020 20:19 It's not completely clear to me in which cases you want to receive a notification.
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
- 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
Like this?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
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
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
Hi waaren,waaren wrote: Saturday 24 October 2020 10:21Like this?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
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 }
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.luaTelegram: 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)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_OffHowever 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- 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
can you try again after changinggschmidt 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?
Code: Select all
execute = function(dz)
Code: Select all
execute = function(dz, item)Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Who is online
Users browsing this forum: No registered users and 1 guest