Page 1 of 1

Switching on severall devices

Posted: Saturday 29 February 2020 13:18
by janpeetoom
Hello.
I have a small problem. I'm trying to write a script to switch on 3 lights outside for 1 minute if a detector "bewegings melder' detectes movement. Thescript only needs to be activated 10 minutes after sunset. Also one of the lights does'nt have to be switched on if a switch "sauna bezoek' is switched on. I am struggeling to write this script. Is there anyone who can help me?
This is what I have so far. I know it's full of erros :)

Code: Select all

return {
    on = {
      timer = {'10 minutes after sunset'},
      devices = { 'bewegings melder' }
    },
    execute = function(domoticz, item)
      if (item.isTimer) then
         -- the timer was triggered
         domoticz.devices('Fitting').switchOff()
      elseif (item.isDevice and item.active) then
         -- it must be the detector
         domoticz.devices('Fitting').switchOn().forMin(1)
         domoticz.devices('Licht steeg').switchOn().forMin(1)
         domoticz.devices('Licht blokhut').switchOn().forMin(1)
      end
    end
}

Re: Switching on severall devices

Posted: Saturday 29 February 2020 17:11
by waaren
janpeetoom wrote: Saturday 29 February 2020 13:18 I have a small problem. I'm trying to write a script to switch on 3 lights outside for 1 minute if a detector "bewegings melder' detectes movement.
could be something like

Code: Select all

return 
{
    on = 
    {
        devices =   
        {  
        	['bewegings melder'] = { 'between 10 minutes after sunset and sunrise' },
    	},
    },
    
    logging =   
    {
        level = domoticz.LOG_DEBUG, -- set to LOG_ERROR when tested and OK
        marker = 'myLight',
    },
    
    execute = function(dz, item)
        if item.active then
            local fitting = dz.devices('Fitting')
            local steeg = dz.devices('Licht steeg')
            local blokhut = dz.devices('Licht blokhut')
            local sauna = dz.devices('sauna bezoek')
            
            local function onFor(device, duration, exception)
                if exception then return end
                device.cancelQueuedCommands()
                device.switchOn()
                device.switchOff().afterSec(duration)
            end

            onFor(fitting, 60, sauna.state == 'On') -- if sauna bezoek 'On' then exception will be true and onFor will return immediate without any action
            onFor(steeg, 60)
            onFor(blokhut, 60)

        end
    end
}

Re: Switching on severall devices

Posted: Sunday 01 March 2020 16:34
by janpeetoom
Thanks Waaren for the quick reply.
I have copied your script into domoticz
Now I get this message:
2020-03-01 16:31:59.531 ...domoticz/scripts/dzVents/generated_scripts/Script #3.lua:5: unexpected symbol near '['
2020-03-01 16:32:00.423 Status: dzVents: Error (2.4.19): error loading module 'Script #3' from file '/home/pi/domoticz/scripts/dzVents/generated_scripts/Script #3.lua':

Any idea?

Re: Switching on severall devices

Posted: Sunday 01 March 2020 19:01
by boum
A pair of curly brackets is missing for the devices value:

Code: Select all

    on = 
    {
        devices =  {
           ['bewegings melder'] = { 'between 10 minutes after sunset and sunrise' },
        }
    },
 

Re: Switching on severall devices

Posted: Monday 02 March 2020 18:06
by janpeetoom
Thanks for your responce. The following error occurs now: 2020-03-02 18:03:42.215 Status: dzVents: Error (2.4.19): error loading module 'Script #3' from file '/home/pi/domoticz/scripts/dzVents/generated_scripts/Script #3.lua':
2020-03-02 18:03:42.215 ...domoticz/scripts/dzVents/generated_scripts/Script #3.lua:8: unexpected symbol near '='
If I remove the loggig part en proceed with the execute = function part the same erro occurs.

Re: Switching on severall devices  [Solved]

Posted: Monday 02 March 2020 20:34
by janpeetoom
Allready figured it out after some tweaking.
This is the final sctipt:

Code: Select all

return
{
    on = 
        {
        devices =  {
           ['bewegings melder'] = { 'between 10 minutes after sunset and sunrise' },
        }
    },
    

    execute = function(dz, item)
    if item.active then
        local fitting = dz.devices('Fitting')
        local steeg = dz.devices('Licht steeg')
        local blokhut = dz.devices('Licht blokhut')
        local sauna = dz.devices('sauna bezoek')
            
        local function onFor(device, duration, exception)
            if exception then return end
            device.cancelQueuedCommands()
            device.switchOn()
            device.switchOff().afterSec(duration)
        end

        onFor(fitting, 60, sauna.state == 'On') -- if sauna bezoek 'On' then exception will be true and onFor will return immediate without any action
        onFor(steeg, 60)
        onFor(blokhut, 60)

        end
    end
}
Thanks for all the help.