Switching on severall devices  [Solved]

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

Moderator: leecollings

Post Reply
janpeetoom
Posts: 12
Joined: Sunday 29 December 2019 12:07
Target OS: Linux
Domoticz version:
Contact:

Switching on severall devices

Post 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
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Switching on severall devices

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
janpeetoom
Posts: 12
Joined: Sunday 29 December 2019 12:07
Target OS: Linux
Domoticz version:
Contact:

Re: Switching on severall devices

Post 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?
User avatar
boum
Posts: 135
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Switching on severall devices

Post 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' },
        }
    },
 
janpeetoom
Posts: 12
Joined: Sunday 29 December 2019 12:07
Target OS: Linux
Domoticz version:
Contact:

Re: Switching on severall devices

Post 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.
janpeetoom
Posts: 12
Joined: Sunday 29 December 2019 12:07
Target OS: Linux
Domoticz version:
Contact:

Re: Switching on severall devices  [Solved]

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest