Repeat a command

Moderator: leecollings

Post Reply
JuanUil
Posts: 498
Joined: Friday 22 May 2015 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11083
Location: Asten NB Nederland
Contact:

Repeat a command

Post by JuanUil »

Hi All,

I have a vision sirene coupled to my alarmsystem
the problem is that this siren only fires once for 1 minute.
I want it to fire 4 or 5 times for 1 minute.
Therefore I have made a LUA script to get this done.
I use a variable x which should be risen with 1 after 60 seconds.
Also after 60 sec the siren fires again.
When x=5 siren should be switchwed of.
I have tried a lot of coding but nothing works.
Could somebody help me?

Code: Select all

-- ~/domoticz/scripts/lua/script_device_AlarmBoven.lua
 -- deze functie berekend de verstreken tijd
function timedifference(s)
   year = string.sub(s, 1, 4)
   month = string.sub(s, 6, 7)
   day = string.sub(s, 9, 10)
   hour = string.sub(s, 12, 13)
   minutes = string.sub(s, 15, 16)
   seconds = string.sub(s, 18, 19)
   t1 = os.time()
   t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
   difference = os.difftime (t1, t2)
   return difference
end


commandArray = {}

    uur       = tonumber(os.date("%H"));
    min       = tonumber(os.date("%M"));

if (otherdevices['Alarm Boven']=='On' and (devicechanged['Logeerkamer']=='Open' or devicechanged['Behandelkamer']=='Open')) then
	commandArray ["Sirene"]='On'
	commandArray['SendEmail']='Alarm#Er is een raam boven open gegaan#[email protected]'
	commandArray['SendEmail']='Alarm#Er is een raam boven open gegaan#[email protected]'
end
   timeon2 = 60
   
if (otherdevices['Sirene']=='On' and x==nil) then
		difference = timedifference(otherdevices_lastupdate['Sirene'])
		print ("Verschil= " .. difference)
		if (difference > timeon2 and difference < (timeon2 + 60) ) then
			if (x==nil) then
				x=1
				print("x is " .. x)
			end
			commandArray ["Sirene"]='On'
		end
end	

return commandArray



Thanx in advance
Jan
Your mind is like a parachute,
It only works when it is opened!

RPI4 several Fibaro, KaKu, Neocoolcam switches, Z-Wave, Zigbee2Mqtt, Ikea bulbs and remote, Zigbee temp nodes
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Repeat a command

Post by simonrg »

I am having trouble to easliy follow your code without some comments, but a few observations which may help.

A single script will only run for a maximum of 10seconds so I assume your idea is that this script will fire every minute for 5 minutes.

In fact a device script will be called every time any device changes and then your if condition determines whether it executes the subsequent code.

I can't see how x ever gets any value apart from 1.

I would have thought you need to use a combination of device and time scripts to get your desired behaviour.
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: Repeat a command

Post by BakSeeDaa »

I use the following function to make a light blink. First argument is a device name and the second argument is how many times I want the light to blink. It can be modified to suit your sirene intervals.

Code: Select all

function blinkLight(light, times)
	times = times or 2
	local cmd1 = 'Off'
	local cmd2 = 'On'
	local pause = 0
	if (otherdevices[light] == 'Off') then
		cmd1 = 'On'
		cmd2 = 'Off'
	end	
	for i = 1, times do
		commandArray[#commandArray + 1]={[light]=cmd1..' AFTER '..pause }
		pause = pause + 3
		commandArray[#commandArray + 1]={[light]=cmd2..' AFTER '..pause }
		pause = pause + 3
	end
end
Keep in mind though that once you have ran the code, the commands are queued and can not be cancelled. ;)
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: Repeat a command

Post by BakSeeDaa »

Modified for your needs I believe it could look something like:

Code: Select all

function soundSirene(sirene, times)
	times = times or 5
	local pause = 0
	for i = 1, times do
		commandArray[#commandArray + 1]={[sirene]='On AFTER '..pause }
		pause = pause + 60
		commandArray[#commandArray + 1]={[sirene]='Off AFTER '..pause }
		pause = pause + 60
	end
end

soundSirene("Sirene") -- Make the sirene sound default number of times
--soundSirene("Sirene", 8) -- Make the sirene sound 8 times
If You want to be able to cancel the sirene, You can create a virtual switch that you switch on and off instead.

Code: Select all

soundSirene("My virtual switch")
then do something like

Code: Select all

if (devicechanged['My virtual switch'] == 'On' and otherdevices['MuteAlarm] == 'Off') then
	commandArray['Sirene']='On'
elseif (devicechanged['My virtual switch'] == 'Off') then
	commandArray['Sirene']='Off'
end
I did not test the code above but maybe it can give you an idea...
simonrg
Posts: 329
Joined: Tuesday 16 July 2013 22:54
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8807
Location: North East England
Contact:

Re: Repeat a command

Post by simonrg »

Nice, so your suggested script would sound the siren for 60 seconds, then silence for 60 seconds, repeated for times or 5 number of repeats - :shock:
Raspberry Pi 2 B - 2A@5V PSU - Raspbian + Domoticz + RFXtrx(89), LightwaveRF House(dimmers, sockets, wireless/mood switches), Owl CM113, 4 LaCross Temp / Humidity Sensors, 4 Siemens PIR, Smappee, Solaredge, ESP8266
JuanUil
Posts: 498
Joined: Friday 22 May 2015 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11083
Location: Asten NB Nederland
Contact:

Re: Repeat a command

Post by JuanUil »

Super guys!!
Thank you verry much. I will try it later this week and let you know if it works!

Jan
Your mind is like a parachute,
It only works when it is opened!

RPI4 several Fibaro, KaKu, Neocoolcam switches, Z-Wave, Zigbee2Mqtt, Ikea bulbs and remote, Zigbee temp nodes
JuanUil
Posts: 498
Joined: Friday 22 May 2015 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11083
Location: Asten NB Nederland
Contact:

Re: Repeat a command

Post by JuanUil »

Hi guys,
couldn't wait any longer to try.
It works like a firetruck!!!! (dutch saying :-) )
Thnx again for your help

Jan
Your mind is like a parachute,
It only works when it is opened!

RPI4 several Fibaro, KaKu, Neocoolcam switches, Z-Wave, Zigbee2Mqtt, Ikea bulbs and remote, Zigbee temp nodes
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: Repeat a command

Post by BakSeeDaa »

JuanUil wrote:Hi guys,
couldn't wait any longer to try.
It works like a firetruck!!!! (dutch saying :-) )
Thnx again for your help

Jan
I'm happy that I could help You Jan :D
BakSeeDaa
Posts: 485
Joined: Thursday 17 September 2015 10:13
Target OS: Raspberry Pi / ODroid
Domoticz version:

Re: Repeat a command

Post by BakSeeDaa »

simonrg wrote:Nice, so your suggested script would sound the siren for 60 seconds, then silence for 60 seconds, repeated for times or 5 number of repeats - :shock:
Yes :lol:
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest