Game Countdown Switch

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:

Game Countdown Switch

Post by AlleyCat »

Hi,
After unsuccessful long research, I am going to the community for help. I set switches in Domoticz to Parental Control my kid's internet. The post is here: https://www.domoticz.com/forum/viewtopi ... ol#p209453

The system works great, and my son is learning responsibility by telling me himself when he gets off his games. Nevertheless, there are days when he would not get off and I still need to cut off the internet manually.

There are a few issues with my current system:

- The current Internet scheduling is not flexible. It goes on and off on a pre-configured schedule using Dummy Switches and scripts.
- Sometimes I turn the internet outside of the "regular" schedule and it goes on unscheduled.
- For unscheduled access, I have a reminder script that sends notifications every 10min that the internet is still on.

I am looking to improve my system by implementing a smart count-down switch. Preferably, a User variable to set how long the Internet will be on (e.g. 30min, 90min, etc.) It will allow me to keep my scheduling system, but expand the flexibility and turn-on or extend the time by a pre-set number of minutes. Can anyone provide references for suggestions on how to implement this kind of a count-down?

The count-down needs to detect the following:
1) Is the Internet on (Dummy Switch)? If yes, keep the Dummy on for add additional time (User Variable)
2) Is the internet is off (again, just checking the status of the Dummy switch): If off, turn the switch on for the duration of the timer.

If a User variable is too complex, I was thinking to have a simple solution with multiple count-down timers for 15min, 30min, 60min, 90min, 120min.

Thank you in advance,

AlleyCat
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Game Countdown Switch

Post by waaren »

AlleyCat wrote: Thursday 30 May 2019 21:48 The count-down needs to detect the following:
1) Is the Internet on (Dummy Switch)? If yes, keep the Dummy on for add additional time (User Variable)
2) Is the internet is off (again, just checking the status of the Dummy switch): If off, turn the switch on for the duration of the timer.
Maybe this dzVents script combined with selector switch and uservar can get you started.
Selector.png
Selector.png (69.09 KiB) Viewed 1594 times

Code: Select all

local scriptVar = 'Parental control'
local selector = 'Internet access' -- new virtual selector switch See attached image
local switch =  'Parental internet access control' -- your existing dummy switch
local variable = 'Internet access minutes set' -- define as type integer; initial value 0

return {
    on = 
    {  
            devices =   
            {   
                selector, -- Selector switch ( set timer )   
                switch    -- Control switch (switching access on / off)
            }, 
            variables = 
            {   
                variable
            },
    },
    
    logging =   
    {   
        level   = domoticz.LOG_DEBUG,
        marker  = scriptVar,
    },
    
    execute = function(dz, item)
        selector = dz.devices(selector)
        switch = dz.devices(switch)
        variable = dz.variables(variable)
        
        local function stop(reason)
            dz.log("Stopping internet access triggered by " .. reason,dz.LOG_DEBUG)
            variable.cancelQueuedCommands()
            variable.set(0).silent()
            switch.switchOff().silent()
            selector.switchSelector(0).silent()
        end
        
        if item.isVariable then
            if item.value > 0 then
                item.cancelQueuedCommands()
                item.set(0).afterMin(item.value) -- This will trigger script again after the set minutes
                dz.log("Internet access activated for " .. item.value .. " minutes")
                switch.switchOn().silent()
            else
                stop("variable =< =")  -- var 0 ==> close internet access 
            end
        elseif item == selector then
            if item.level == 0 then stop("selector set to 0") 
            elseif item.level < 70 then variable.set(item.level)
            elseif item.level == 70 then variable.set(120)
            elseif item.level == 80 then variable.set(240)
            elseif item.level == 90 then variable.set(variable.value - variable.lastUpdate.minutesAgo + 10)
            end
            item.switchSelector(0).silent()
        elseif not(item.active) then
            stop("manually (or timer) closed control switch") -- selector or switch set to Off ==> close internet access    
        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: Game Countdown Switch

Post by AlleyCat »

Thank you. Please let me test and provide feedback.

AlleyCat
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: Game Countdown Switch

Post by emme »

to detect if internet i son or off use a PING hardware and add a ping to 8.8.8.8...
that will create a switch on and of if the ping response is true or false

;)
The most dangerous phrase in any language is:
"We always done this way"
AlleyCat
Posts: 22
Joined: Tuesday 07 February 2017 21:19
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Game Countdown Switch

Post by AlleyCat »

The script is working great. I want to improve the UI and usability.

1) How can I turn the Selector status to show On, and it will go off when the timer is out? When I set the Selector the script runs well, but the selector status remains Off.

2) How can I change the script to control a Scene instead of a switch? There are multiple devices connecting to the internet under one scene. It will be useful to control the scene.

3) How can I show a count-down in the Selector Switch Title bar? If the selector was set to 20 min, can I show the count-count value to zero in the Right top bar?

Thank you very much for a great script.

AlleyCat
Last edited by AlleyCat on Wednesday 12 June 2019 14:12, edited 1 time in total.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Game Countdown Switch

Post by waaren »

AlleyCat wrote: Wednesday 12 June 2019 13:12 I want to improve the UI and usability.
1) How can I turn the Selector status to show On, and it will go off when the timer is out? When I set the Selector the script runs well, but the selector status remains Off.

change line 55 from

Code: Select all

item.switchSelector(0).silent()
to

Code: Select all

-- item.switchSelector(0).silent()
2) How can I change the script to control a Scene instead of a switch? There are multiple devices connecting to the internet under one scene. It will be useful to control the scene.
There is no scene.switchOff() so you want probably a group to do this.
if you change line 28 from

Code: Select all

 switch = dz.devices(switch)
to

Code: Select all

 switch = dz.groups('your groupname') 
it will probably work.
3) How can I show a count-down in the Selector Switch Title bar? If the selector was set to 20 min, can I show the count-count value to zero in the Right top bar?

You could force this with the new dz.devices(nnn).rename() method that will become available in dzVents 2.4.24 but the GUI is probably not able to deal with this as the refresh rate of the GUI screen is at least 10 seconds.
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: Game Countdown Switch

Post by AlleyCat »

Thank you.
snetekombinerke
Posts: 1
Joined: Saturday 24 July 2021 11:33
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Game Countdown Switch

Post by snetekombinerke »

Hi,

I want to use this script for my garden irrigation system.
Please someone help me with this.
If i press image of a switch (when it is on), off option by pressing on image doesn't work.
I try adding
elseif item.state == "Off" then
stop("manual stop") -- selector or switch set to Off ==> close internet access
before the last two "end"
I have low understanding of scripting.

Also is it now supported in domoticz -> I would like to add count down value to zero functionality as mentioned by "AlleyCat"

Thanks in advance
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest