Page 1 of 1

switch with variable and delay

Posted: Sunday 06 October 2019 10:43
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

Re: switch with variable and delay

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

Now the delay that can i not fixed :-(

Re: switch with variable and delay

Posted: Sunday 06 October 2019 15:56
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]