Request: update variable B after variable has specific state for x time

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

Moderator: leecollings

Post Reply
kshesq
Posts: 12
Joined: Tuesday 05 January 2016 9:50
Target OS: Linux
Domoticz version: 2021.1
Location: Haarlem
Contact:

Request: update variable B after variable has specific state for x time

Post by kshesq »

Hi,
I have searched the forums, but I cannot find a topic for this and after trying out various options myself I have to admit that I am lacking the knowledge for this and ask my fellow domoticz users for help.

First i'd like to explain the use for this request. I have used a LUA arp scan script to detect my phone and when it does a variable A is set to 1 (instead of 0). Based on this variable I use some blockly scripts to automate stuff.
Unfortunately the arp scan does not always find my phone, probably due to energy saving, and during the day the variable A changes multiple times between 1 and 0. Making the blockly scripts not very reliable.

So what I thought of was creating a second variable B that only sets 0 after the "last updated" time of the variable A that detects my phone set 0 exceeds 600 seconds.

So this happens;
-phone detected triggeres variable A to 1 at 8:00 when I switch on my phone
-phone connection lost triggers variable A to 0 at 8:30 when I leave for work
-when the last state of variable A = 0 > 600 seconds, in this case 8:40, it triggeres variable B to set 0


If am still at home the above mentioned trigger of variable B will nog happen because the phone will be redetected by the lua script and the variable A will be set again at 1.

I guess the bold printed script request will help me out.
Cheers,
Kim
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Request: update variable B after variable has specific state for x time

Post by waaren »

kshesq wrote: Sunday 16 May 2021 13:47 ask my fellow domoticz users for help.
Is your profile up to date ? (build 4.11337)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
kshesq
Posts: 12
Joined: Tuesday 05 January 2016 9:50
Target OS: Linux
Domoticz version: 2021.1
Location: Haarlem
Contact:

Re: Request: update variable B after variable has specific state for x time

Post by kshesq »

no I am running Version 2021.1 (13252)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Request: update variable B after variable has specific state for x time

Post by waaren »

kshesq wrote: Sunday 16 May 2021 15:03 no I am running Version 2021.1 (13252)
Could look like below

Code: Select all

local scriptVar = 'timedVar'

return
{
    on =
    {
        variables =
        {
            'Variable A', -- change to real uservariable name
        },
        customEvents =
        {
            scriptVar,
        }
    },

    logging =
    {
        level = domoticz.LOG_DEBUG, -- set to domoticz.LOG_ERROR when all ok
        marker = scriptVar,
    },

    execute = function(dz, item)
        local delay = 600
        local varA = dz.variables('Variable A') -- change to real uservariable name
        local varB = dz.variables('Variable B') -- change to real uservariable name

        if item.isCustomEvent then
            if tonumber(item.data) >= varA.lastUpdate.dDate then
                dz.log('delay passed',dz.LOG_DEBUG)
                varB.set(0)
            else
                dz.log('Too soon',dz.LOG_DEBUG)
            end
        elseif varA.value == 0 then
            dz.emitEvent(scriptVar, os.time()).afterSec(delay)
            dz.log(item.lastUpdate.secondsAgo)
        elseif varA.value == 1 then
            varB.set(1)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
kshesq
Posts: 12
Joined: Tuesday 05 January 2016 9:50
Target OS: Linux
Domoticz version: 2021.1
Location: Haarlem
Contact:

Re: Request: update variable B after variable has specific state for x time

Post by kshesq »

Thank you very much. I have installed it but it seems like it's not running yet. I will let you know as soon as it runs.
kshesq
Posts: 12
Joined: Tuesday 05 January 2016 9:50
Target OS: Linux
Domoticz version: 2021.1
Location: Haarlem
Contact:

Re: Request: update variable B after variable has specific state for x time

Post by kshesq »

Hi Waaren,
Script works, thanks very much. BUT:

The only strange thing is that the script only works when I change the variable A manually of program it in a swich. If I let the variable A change with another script then your script doesn't seem to notice.
But this seems to be something more general since this is happing with more scripts and blocklys using variables.

It looks like domoticz handles variable changes diffently between; changes instigated by direct input and by changes made by blockly or other scripts.

eg: when I use a blockly to change Variable A to 0 the dzvents script above does not run. But when I add an on (http://ip:8080/json.htm?type=command&pa ... r&vvalue=1) and off (http://ip:8080/json.htm?type=command&pa ... r&vvalue=0) action to a switch then the dzvents script runs.

also the dzvents script runs when I manually change the variable.

Any ideas on this are welcome
Kim
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Request: update variable B after variable has specific state for x time

Post by waaren »

kshesq wrote: Wednesday 19 May 2021 20:51 Any ideas on this are welcome
When you set a domoticz uservariable from a classic Lua script or Blockly it will not trigger an event.
You will have to change the uservar manually, or via a JSON call or use a dzVents script to do that.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
kshesq
Posts: 12
Joined: Tuesday 05 January 2016 9:50
Target OS: Linux
Domoticz version: 2021.1
Location: Haarlem
Contact:

Re: Request: update variable B after variable has specific state for x time

Post by kshesq »

I got it working using the call url function in blockly. Thank you very much
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest