Page 1 of 1
Notification timer every 10min
Posted: Wednesday 15 August 2018 18:04
by AlleyCat
Hi there,
I am having a problem forgetting that my coffee maker machine is on and I want to send notifications every 10 minutes while the 'Coffee' switch is on.
My Coffee switch has a timer that goes on at 6:00 am and off at 8:00 am. I want to receive the notifications only if I turn the coffee on at any time other than 6:00 to 8:00.
I have been trying to adapt scripts but I am not successful. Perhaps my request could be useful for anyone else who wants to get notifications on a switch status outside of the standard (or timer) operating time.
Thank you,
Alley Cat
Re: Notification timer every 10min
Posted: Wednesday 15 August 2018 19:02
by waaren
AlleyCat wrote: ↑Wednesday 15 August 2018 18:04
..
I am having a problem forgetting that my coffee maker machine is on and I want to send notifications every 10 minutes while the 'Coffee' switch is on. My Coffee switch has a timer that goes on at 6:00 am and off at 8:00 am. I want to receive the notifications only if I turn the coffee on at any time other than 6:00 to 8:00.
..
Something like this dzVents script should work
Code: Select all
-- NotifyOutsidePeriod.lua
myCoffeeVar = "coffeeSwitchMinutes" -- This uservar must be created as type integer with initial value 0
myCoffeeSwitch = "coffeeSwitch" -- Name within quotes or index without quotes
return {
on = { variables = {myCoffeeVar}, -- type integer
devices = {myCoffeeSwitch}},
execute = function(dz)
local coffeeVar = dz.variables(myCoffeeVar)
local coffeeSwitch = dz.devices(myCoffeeSwitch)
if coffeeSwitch.state == "Off" then
coffeeVar.set(0).silent()
else
coffeeVar.set(coffeeVar.value + 10).afterMin(10)
if not( dz.time.matchesRule('at 06:00-08:00') ) and coffeeVar.value > 0 then
local message = "Coffee machine still on after " .. coffeeVar.value .. " minutes."
dz.notify(message)
end
end
end
}
Re: Notification timer every 10min
Posted: Thursday 16 August 2018 13:03
by AlleyCat
Fantastic. Thank you for an amazing script.
How can I improve the script to set the exception's base on the Coffee's timer value? Instead of hardcode matchesRule('at 06:00-08:00') to use the switch's Timer "on" values?
Thanks,
AlleyCat
Re: Notification timer every 10min
Posted: Thursday 16 August 2018 15:01
by waaren
AlleyCat wrote: ↑Thursday 16 August 2018 13:03
How can I improve the script to set the exception's base on the Coffee's timer value? Instead of hardcode matchesRule('at 06:00-08:00') to use the switch's Timer "on" values?
There are at least two methods to achieve that. The easy way is to add the timerstring in the description field of the switch (6:00-8:00) and use the modified script below.
Code: Select all
-- NotifyOutsidePerdiod.lua
myCoffeeVar = "coffeeSwitchMinutes" -- This uservar must be created as type integer with initial value 0
myCoffeeSwitch = "coffeeSwitch" -- Name within quotes or index without quotes
return {
on = { variables = {myCoffeeVar}, -- type integer
devices = {myCoffeeSwitch}},
execute = function(dz)
local coffeeVar = dz.variables(myCoffeeVar)
local coffeeSwitch = dz.devices(myCoffeeSwitch)
if coffeeSwitch.state == "Off" then
coffeeVar.set(0).silent()
else
coffeeVar.set(coffeeVar.value + 10).afterMin(10)
if not( dz.time.matchesRule("at " .. coffeeSwitch.description) ) and coffeeVar.value > 0 then
local message = "Coffee machine still on after " .. coffeeVar.value .. " minutes."
dz.notify(message)
end
end
end
}
the complicated way is to get the timersettings from domoticz and store them in persistent data. Problem is that there are many different timertypes but if you are only using 1 timer for On and 1 timer for Off, both of type "On time" it is doable. I will play a little bit with the script based on that assumption.
Re: Notification timer every 10min
Posted: Thursday 16 August 2018 16:04
by waaren
AlleyCat wrote: ↑Thursday 16 August 2018 13:03
How can I improve the script to set the exception's base on the Coffee's timer value? Instead of hardcode matchesRule('at 06:00-08:00') to use the switch's Timer "on" values?
and for the complicated way....
Code: Select all
-- NotifyOutsidePeriod.lua
--
-- This script will only works OK if one timer is set for "On" and 1 timer is set for "Off". Both of type "On time"
--
myCoffeeVar = "coffeeSwitchMinutes" -- This uservar must be created as type integer with initial value 0
myCoffeeSwitch = "coffeeSwitch" -- Name within quotes or index without quotes
myCallback = "coffeeTimerResponse"
return {
on = { variables = { myCoffeeVar }, -- type integer
devices = { myCoffeeSwitch },
httpResponses = { myCallback }}, -- Trigger the handle Json part
logging = { level = domoticz.LOG_DEBUG,
marker = "NotifyOutsidePeriod" },
data = { startTime = { initial = "6:00" },
endTime = { initial = "8:00" },
state = { initial = "Off" }},
execute = function(dz,triggerObject)
local coffeeVar = dz.variables(myCoffeeVar)
local coffeeSwitch = dz.devices(myCoffeeSwitch)
local function getTimers(device)
local urlString = dz.settings['Domoticz url'] .. "/json.htm?idx=" .. device.idx .. "&type=timers"
dz.openURL({ url = urlString,
method = "GET",
callback = myCallback
})
end
local function handleState()
if coffeeSwitch.state == "Off" then
coffeeVar.set(0).silent()
dz.data.state = "Off"
else
coffeeVar.set(coffeeVar.value + 10).afterMin(10)
if not( dz.time.matchesRule("at " .. dz.data.startTime .. "-" .. dz.data.endTime) ) and coffeeVar.value > 0 then
local message = "Coffee machine still on after " .. coffeeVar.value .. " minutes."
dz.notify(message)
dz.log(message,dz.LOG_DEBUG)
end
end
end
local function handleResponse()
rt = triggerObject.json -- dump json to lua table
for i = 1, #rt.result do -- loop over result
if rt.result[i].Type == 2 and rt.result[i].Cmd == 0 then -- Type 2 is "On Time", Cmd 0 is "On"
dz.data.startTime = rt.result[i].Time
elseif rt.result[i].Type == 2 and rt.result[i].Cmd == 1 then -- Type 2 is "On Time", Cmd 1 is "Off"
dz.data.endTime = rt.result[i].Time
end
end
end
if triggerObject.isHTTPResponse then
if triggerObject.ok then -- Response from domoticz OK
handleResponse()
handleState()
else
dz.log("Problem with response from domoticz",dz.LOG_ERROR)
end
elseif coffeeSwitch.state == "On" and dz.data.state == "Off" then
dz.data.state = "On"
getTimers(coffeeSwitch)
else
handleState()
end
end
}
Re: Notification timer every 10min
Posted: Thursday 16 August 2018 18:30
by AlleyCat
Thanks for the scripts. I will test the revised script later tonight.
In the case of my Coffee switch, there is only one (1) timer between 06:00 "On" and 09:30 "Off".
Would the "complex" script know how to handle multiple Timers?
I want to adapt this script to my Parental Control switches, which have multiple Timers. Here is the scenario:
My kid's internet access and privileges are controlled by a Dummy switch that sends ssh commands to the Router's iptables firewall to ACCEPT or REJECT access based on MAC address. The details of the scripts and switches are beyond the scope of this thread. (Nevertheless, I plan to share soon because I know I have a solution for a very painful problem with controlling internet access for kids).
I am managing the Internet privileges with multiple Timers to when the internet goes on or off. For example, the kid is allowed to play the XBOX between 10:00-11:00 and 16:00-17:00.
Now I have multiple Timers going on/off at two different times. On Saturdays my kid has Karate leasons, so the XBOX switch is on only in the afternoon. So, on Saturday the Timer is different than from the rest of the week. I think I made the point and explained the scenarion to why I want to handle multiple Timers.
Now, when my kid cut the lawn and earns more internet time. The problem is I tend to forget that the XBOX in on, and he plays for hours. LOL
If the script would handle multiple Times I will be able to receive notifications that the XBOX is on outside of the Timers operating cycles, and this will make me a better father. LOL
Thanks,
Alley Cat
Re: Notification timer every 10min
Posted: Thursday 16 August 2018 20:09
by waaren
AlleyCat wrote: ↑Thursday 16 August 2018 18:30
...
Would the "complex" script know how to handle multiple Timers?
...when my kid cut the lawn and earns more internet time. The problem is I tend to forget that the XBOX in on, and he plays for hours. LOL
the "complex" script cannot handle multiple timers in its current form. The call to domoticz do get all timers so it is possible to make the script work for multiple timers but that is beyond the scope of my examples. One of the challenges there is that there is no such thing as a defined set of On/Off timers. It will be quite cumbersome to code On/Off sets more so if they are distinguishing special days of the week.
What might be a far easier way of addressing your requirement is to let dzVents switch off your virtual devices one hour (or another defined timespan) after they are switched on or alerting you to do that manually.