switch with variable and delay

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

Moderator: leecollings

Post Reply
Gianni
Posts: 230
Joined: Saturday 21 July 2018 19:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Home@Belgium
Contact:

switch with variable and delay

Post by Gianni »

I have a switch that triggered a variable.
The switch works the variable go from 0 to 1
The only problem is that the switch is not working from another script

the variable switch

Code: Select all

return 
{
    active = true,
    
    on = { devices = { 'Alarm_On' }},
    
    execute = function(domoticz,switch)
        local varalarm = domoticz.variables('VarAlarm')
        if switch.state == "On" then domoticz.variables('VarAlarm').set(1)
          elseif switch.state == "Off" then domoticz.variables('VarAlarm').set(0)
        end
    end
}
The alarm switch

Code: Select all

return 
{
    active = true,
    
    on = { devices = { 'Voordeur' }},
    
    execute = function(domoticz,voordeur)
        local alarm = domoticz.devices('Alarm')
        local varalarm = domoticz.variables('VarAlarm')
        if voordeur.active and domoticz.time.matchesRule('at 23:30-06:00')  and varalarm.value == 1 then
            alarm.cancelQueuedCommands()
            alarm.switchOn().checkFirst()
            alarm.switchOff().afterSec(60)
        end
    end
}
and now the script that give a error on the switch on local alarmswitch = domoticz.devices('Alarm_On')

Code: Select all

return {
            on = { 
                devices = {"Alles_Uit_Living"}},
            
    execute = function(dz,item)
        local myScene = 1              
        local alarmswitch = domoticz.devices('Alarm_On')
        
             dz.log('Name of dz.scenes(myScene): "' .. dz.scenes(myScene).name .. '"',dz.LOG_FORCE )    
            
    if item.state == "On" then
           dz.scenes(myScene).switchOn()
              alarmswitch.switchOn()
                 item.switchOff().afterSec(3).silent()       
        else
            dz.scenes(myScene).switchOn()
        end
    end
}

Code: Select all

 2019-10-06 10:38:08.762 Status: User: Admin initiated a switch command (7/Alles_Uit_Living/On)
2019-10-06 10:38:08.861 Status: dzVents: Info: Handling events for: "Alles_Uit_Living", value: "On"
2019-10-06 10:38:08.861 Status: dzVents: Info: ------ Start internal script: Alles_Uit_Living: Device: "Alles_Uit_Living (Controller)", Index: 7
2019-10-06 10:38:08.861 Status: dzVents: Error (2.4.19): An error occured when calling event handler Alles_Uit_Living
2019-10-06 10:38:08.861 Status: dzVents: Error (2.4.19): ...z/scripts/dzVents/generated_scripts/Alles_Uit_Living.lua:7: attempt to index global 'domoticz' (a nil value)
2019-10-06 10:38:08.861 Status: dzVents: Info: ------ Finished Alles_Uit_Living 
So my second question is the alarm voordeur.
I want to put a delay of 5 sec before the script is started.
Let say we go out and set alles uit living at 18.00
then we come back at midnight the alarm will be triggerd.
Then i need 5 sec to put the switch off that set the variable to 0 so the alarm is disabled.
The we go to sleep set alles uit living and the variable will be set to 1
Gianni
Posts: 230
Joined: Saturday 21 July 2018 19:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Home@Belgium
Contact:

Re: switch with variable and delay

Post by Gianni »

ok my first on is fixed
domoticz.devices must be off course dz.devices

Now the delay that can i not fixed :-(
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: switch with variable and delay

Post by waaren »

Gianni wrote: Sunday 06 October 2019 11:00 ok my first on is fixed
domoticz.devices must be off course dz.devices

Now the delay that can i not fixed :-(
You could try this after creating a dummy switch "Voordeur vertraagd"

Code: Select all

local door         = 'Voordeur'
local delayed  =  'Voordeur_vertraagd' -- Create this as a virtual switch (dummy device)

return 
{
    active = true,
    
    on = { devices = { door, delayed }},
    
    execute = function(dz, item)
    
        local frontDoor = dz.devices(door)
        local delayed = dz.devices(delayed)
        local alarm = dz.devices('Alarm')
        local varalarm = dz.variables('VarAlarm')
        
        if item == frontDoor and item.active and varalarm.value == 1 and dz.time.matchesRule('at 23:30-06:00') then 
            delayed.switchOn().afterSec(5) -- This could be a bit short
        elseif item == delayed and item.active and varalarm.value == 1 then
            alarm.cancelQueuedCommands()
            alarm.switchOn().checkFirst()
            alarm.switchOff().afterSec(60)
        end
    end
}/code]
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