Page 1 of 1

Run every time if switch on

Posted: Tuesday 25 July 2017 8:04
by maglo18
I trying to write scrip with will be switch on and off my CWU pomp. i need condition: if switch on switch on another switch for 1 min every 10 min. My script working only when switch on but when time is triger i have info "'device' (a nil value)"

Code: Select all

return {

	-- 'active' controls if this entire script is considered or not
	active = true, -- set to false to disable this script

	-- trigger
	-- can be a combination:
	on = {
		devices = {
			-- scripts is executed if the device that was updated matches with one of these triggers
			'Testsw', -- device name
		},

		timer = {
			-- timer triggers.. if one matches with the current time then the script is executed
			'every 10 minutes'
		},
	},

	-- actual event code
	-- in case of a timer event or security event, device == nil
	execute = function(domoticz,device)
	    domoticz.log('Alarm zmienił stan')
--	   local testsw = domoticz.devices('Testsw')
		
		if (device.state == 'On') then
		    local pompaCWU = domoticz.devices('Pompa CWU')
		    pompaCWU.switchOn()
		    end
		
	end
}

Re: Run every time if switch on

Posted: Tuesday 25 July 2017 11:39
by dannybloe
Yes, the thing is, when a device triggers your script then dzVents knows which device it is and passes that to your execute script as the second parameter. If a timer triggers your script, then there is no device triggered so dzVents doesn't know what device you need. That's why the second parameter is nil when your timer is triggered. See the documentation.

So, it's an easy fix. Just get the device from the devices collection and don't use the second parameter:

Code: Select all

local switch = domoticz.devices('Testsw')
if (switch.state == 'On') then
  ...

Re: Run every time if switch on

Posted: Tuesday 25 July 2017 11:44
by mivo
Hi,

if I understanding correctly:

- you want check status of switch 'Testsw' every 10 minutes
- if 'Testsw' is On, switch On another switch 'Pompa CWU' for 1 minute

Try this:

Code: Select all

return {

   -- 'active' controls if this entire script is considered or not
   active = true, -- set to false to disable this script

   -- trigger
   -- can be a combination:
   on = {
         -- Dont check for device, time is main trigger for your action

         timer = {
         -- timer triggers.. if one matches with the current time then the script is executed
         'every 10 minutes'
      },
   },

   -- actual event code
   -- in case of a timer event or security event, device == nil
   execute = function(domoticz,device)
      domoticz.log('Alarm zmienił stan')
      
      -- master switch device
      local testsw = domoticz.devices('Testsw')
      
      -- test status of master switch
      if (testsw.state == 'On') then

          -- slave switch device
          local pompaCWU = domoticz.devices('Pompa CWU')

          -- switch on slave device for 1 minute
          pompaCWU.switchOn().forMin(1)

          -- optionally turn master switch off, if needed
          testsw.switchOff()
      end
    
   end
}

Re: Run every time if switch on

Posted: Tuesday 25 July 2017 23:52
by maglo18
mivo wrote:Hi,

if I understanding correctly:

- you want check status of switch 'Testsw' every 10 minutes
- if 'Testsw' is On, switch On another switch 'Pompa CWU' for 1 minute
Yes correctly. I will try your code. Thanks.