Notification timer every 10min

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

Moderator: leecollings

Post Reply
AlleyCat
Posts: 22
Joined: Tuesday 07 February 2017 21:19
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Notification timer every 10min

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

Re: Notification timer every 10min

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
AlleyCat
Posts: 22
Joined: Tuesday 07 February 2017 21:19
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Notification timer every 10min

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

Re: Notification timer every 10min

Post 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.
Last edited by waaren on Thursday 16 August 2018 16:04, edited 1 time in total.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Notification timer every 10min

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
AlleyCat
Posts: 22
Joined: Tuesday 07 February 2017 21:19
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Notification timer every 10min

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

Re: Notification timer every 10min

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