Run every time if switch on

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

Moderator: leecollings

Post Reply
maglo18
Posts: 3
Joined: Sunday 01 June 2014 22:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Run every time if switch on

Post 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
}
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Run every time if switch on

Post 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
  ...
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
mivo
Posts: 80
Joined: Friday 21 April 2017 8:58
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Czechia
Contact:

Re: Run every time if switch on

Post 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
}
My toys:
Raspberry Pi 3 + UPS PIco HV3.0 A Stack
Minibian (Raspbian Jessie) + Domoticz beta
RFLink 433 Gateway, 1wire DS18B20 temp sensors (GPIO)
RaZberry module + 2x Comet Z-Wave + Z-wave socket
---
Plugins: WeMo Switch, UPS PIco HV3.0A on GitHub
maglo18
Posts: 3
Joined: Sunday 01 June 2014 22:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Run every time if switch on

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

Who is online

Users browsing this forum: No registered users and 1 guest