dzVentz Alarm Script starts here..

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

Moderator: leecollings

Post Reply
User avatar
remb0
Posts: 499
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

dzVentz Alarm Script starts here..

Post by remb0 »

I have Several virtual switches, some scripts and blockies to create an alarm.
The problem is that normal lua scripts and blocks are quite long and not easy to read.
I think with the options of dzVentz we can create a flexible alarm script that is easy to adopt for everyone.

Features:
  • - Send notifications (silent alarm)
    - Sounds siren for intruders
    - Set alarm only when every door and windows are closed.
    - Audio feedback through speakers
    - Auto alarm (when everyone is away, and the vsSwitch: AutoAlarm=On set alarm armed after 20 min
    - Saving option: If alarm is ‘armed away’ some or all light must off
Goals:
  • - flexible and easy to read/control.
    - less code / virtual devices / uservariables as possible (The key of dzVentz)
    - good documentation (in script, and wiki with visio diagrams)
I Have some Visio schemas but they are based on my current scripts (from the wiki) and there is a lot of Dutch in it. So I want to start collecting Ideas, information, Icons, samples, scenarios etc. for starting over. Please reply with questions or ideas you have.

Start situation:
I will start simple with this:
set alarm though zipato keypad or internal domoticz panel (they must in sync)
speaker told me if there are door* open otherwise alarm will armed after 30 sec.

when alarm is armed:
and a door will opened speaker told you that you get 60 seconds to disarm the alarm
after 60 seconds notifications will send (if silent alarm mode is on (handy during testing) or the siren wil go off for 5 x 1 min

when disarming the alarm:
speaker will welcome you
all virtual switches will be resetted.

all devices for alarm will in 1 room plan, this is handy during testing.
User avatar
heggink
Posts: 977
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: dzVentz Alarm Script starte here..

Post by heggink »

Interesting. I started the original wiki and, as if yesterday have begun completely rewriting my alarm system due to dzvents being much more flexible. One objective was to reduce all the dummy devices that were needed before, the other is to reduce current complexity. Unfortunately I did find some unexpected behaviour in dzvents (armaway function switches domoticz.security straight to 'armed away' instead of after the timer) and, until that is resolved, still need a dummy.
Docker in Truenas scale, close to latest beta
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: dzVentz Alarm Script starte here..

Post by dannybloe »

But is that a domoticz issue or dzvents? I can't test it myself right now.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
User avatar
heggink
Posts: 977
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: dzVentz Alarm Script starte here..

Post by heggink »

Good question. It's different from the original lua behaviour. It would be helpful to have a state between disarmed and armed called arming.
Docker in Truenas scale, close to latest beta
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: dzVentz Alarm Script starte here..

Post by dannybloe »

Ah. I see it. I haven't enabled the timed support for those commands. That's an easy fix. I'll fix it after the weekend.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
User avatar
heggink
Posts: 977
Joined: Tuesday 08 September 2015 21:44
Target OS: Raspberry Pi / ODroid
Domoticz version: 12451
Location: NL
Contact:

Re: dzVentz Alarm Script starts here..

Post by heggink »

Watch it: I found that arming DOES use the built-in timer but whilst it is between disarmed and armed away, it already sets domoticz.security to armed away even though its not armed yet. The downside is that you cannot check against that to raise a PIR based alarm. That's where an interim state ("arming") would be very helpful.
Docker in Truenas scale, close to latest beta
DASHTICZ 🙃
RFXCOM, zwavejs2mqtt, zigbee2mqtt,
P1 meter & solar panel
Google home, Wifi Cams motion detection
Geofence iCloud, Bluetooth & Wifi ping
Harmony hub, Nest, lots more :-)
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: dzVentz Alarm Script starts here..

Post by dannybloe »

Ok, but that is not a dzVents issue but a Domoticz issue. Perhaps file a request for this.
For now I'll make the timed options available for the security commands.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
User avatar
remb0
Posts: 499
Joined: Thursday 11 July 2013 22:21
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: dzVentz Alarm Script starts here..

Post by remb0 »

heggink and I are worked on a new script but because his work is more next level (with doorlocks and auto arming) I started to make something as simple as possible (as start point) I want to post it here and get some feedback.

also a big thanks to dannybloe for his work and comments.

requirements:
2 user variables:
  • AlarmSirenTime, integer (example: 3)
    AlarmOnDelay, integer (example: 60)
3 virtual switches:
  • vsAlarm_dooropen
    vsAlarm_SoundSiren
    vsAlarm_Detected
    test door switch (testdeur) to test script without walking to the door everytime.
    these switches have I placed in a roomplan: Alarm. handy to test. when script is finished I will place $ in front of the name.
2 dzVents scripts:
global_data

Code: Select all

local SECURITY_PANEL = 'Security Panel'
local ALARM_DETECTED = 'vsAlarm_Detected'
local ALARM_DOOR_OPEN = 'vsAlarm_dooropen'
local SIREN = 'vsAlarm_SoundSiren'
local delay = 60
return {
	helpers = {

		PlaySound = function(msg)
			os.execute("sudo killall mplayer")
			os.execute("sh /home/pi/domoticz/scripts/bash/Play_sound.sh '" .. msg .. "' ")
		end,

		alarmOn = function(dz, notify, delay)

			dz.log('In alarmOn: ' .. dz.security, dz.LOG_DEBUG)

			if (dz.security == dz.SECURITY_DISARMED) then 
				if (dz.devices(ALARM_DOOR_OPEN).state == 'Off') then
					dz.log('Arming domoticz', dz.LOG_INFO) 
					--domoticz.devices('AlarmActive').switchOn()  -- we will use this later as override
					dz.devices(SECURITY_PANEL).armAway().afterSec(delay) 
					if (notify) then -- DB: use param notify
						dz.notify('SECURITY', 'Arming Alarm', dz.PRIORITY_HIGH) 
					end
					--dz.devices('Home Temp').updateSetPoint(15)
					--dz.devices('Thermostat Downstairs Away').switchOn()
					dz.helpers.playSound('Alarm Geactiveerd') 
				else
					dz.log('Failed Arming domoticz, door/window still open', dz.LOG_ERROR)
					if (notify) then
						dz.notify('SECURITY', 'FAILED Arming Alarm, door/window open', dz.PRIORITY_HIGH) -- DB: const
					end
					dz.helpers.playSound('Alarm niet geactiveerd, er staat nog iets open') -- DB: camelCase
				end
			end
		end,

		alarmOff = function(dz)
			dz.log('In alarmOff: ' .. dz.security, dz.LOG_DEBUG) 
			if (dz.security == dz.SECURITY_ARMED_AWAY) then
				-- Reset al states
				dz.devices(SIREN).switchOff()
				dz.devices(ALARM_DOOR_OPEN).switchOff()
				dz.devices(ALARM_DETECTED).switchOff()
				--dz.devices('$AlarmActive').switchOff()
				dz.devices('Sirene').switchOff() -- real siren!!
				dz.devices(SECURITY_PANEL).disarm()
				dz.log('Disarming domoticz', dz.LOG_INFO) -- DB: info?
				if (notify) then
					dz.notify('SECURITY', 'Disarming Alarm', dz.PRIORITY_HIGH)
				end
				dz.helpers.playSound('Alarm Uitgeschakeld')
			end
		end

	}
}
AlarmScript

Code: Select all

local CUSTOM_KEYPAD = 'Keypad Access Control'
local ALARM_DETECTED = 'vsAlarm_Detected'
local ALARM_DOOR_OPEN = 'vsAlarm_dooropen'
local SIREN = 'vsAlarm_SoundSiren'
local DELAY = 60 --seconds
local TEST_DOOR = 'deurtest'
local NOTIFY_ALARM = true

return {
	active = true,
	on = {
		devices = {
			CUSTOM_KEYPAD,
			'deur*', 'Schuifpui',
			ALARM_DETECTED, ALARM_DOOR_OPEN, SIREN
		}
	},
	logging = {
		level = domoticz.LOG_DEBUG,
		marker = "Alarm script: " -- prefix added to every log message
	},
	-- actual event code
	execute = function(dz, dv)
		local devices = dz.devices 
		local delay = dz.variables(DELAY).value

		--CUSTOM_KEYPAD
		if (dv.name == CUSTOM_KEYPAD) then
			dz.log('Status bericht: ' .. dv.name .. dv.state, dz.LOG_DEBUG) -- DB: moeten er geen spaties oid tussen name en state? add loglevel, debug of info?
			if (dv.state == 'On') then
				if (dz.security == dz.SECURITY_DISARMED) then -- DB: gebruik dz constants
					dz.log('CUSTOM_KEYPAD switches alarm ON')
					dz.helpers.alarmOn(dz, NOTIFY_ALARM, delay) --DB: geef NOTIFY_ALARM ook mee, denk dat je hier je delay wil meegeven
				end
			else
				if (dz.security == dz.SECURITY_ARMED_AWAY) then -- DB: constatns
					dz.log('CUSTOM_KEYPAD switches alarm OFF', dz.LOG_INFO) --DB: loglevel?
					dz.helpers.alarmOff(dz, NOTIFY_ALARM, delay) -- DB: NOTIFY_ALARM meegeven
				end
			end
			return 
		end

		-- Notify that alarm is triggered and siren will go off
		if (dv.name == ALARM_DETECTED
			and dv.state == 'On'
			and dz.security == dz.SECURITY_ARMEDAWAY) then 

			dz.log('Alarm detected during Armed Away status, report it!')
			dz.notify('SECURITY', 'ALARM: Sensor ging af', dz.PRIORITY_EMERGENCY)

			if (NOTIFY_ALARM) then
				local notifySub = '[ALARM] ' .. dv.name .. ' - device triggered'
				local notifyMsg = '[ALARM] ' .. dv.name .. ' - ' .. dv.state .. ' - device triggered' 
				--DB: wel een lange subject.. zou je die niet korter houden en meer in het bericht stoppen?
				dz.notify(notifySub, notifyMsg, dz.PRIORITY_EMERGENCY) 
				dz.log(notifyMsg, dz.LOG_DEBUG) --DB: dit is debug info lijkt me? dus debug level gebruiken?
			end

			devices(ALARM_DETECTED).switchOff().afterSec(20)
			devices(SIREN).switchOn().afterSec(90)
			dz.helpers.PlaySound('Beweging gedetecteerd, alarm gaat over 90 seconden af.', NOTIFY_ALARM)
			dz.notify('SECURITY', 'Switching Siren on in 90 seconds', dz.PRIORITY_EMERGENCY)
			return
		end

		-- Siren make somenoise!
		if (dv.name == SIREN
			and dv.state == 'On'
			and dz.security == dz.SECURITY_ARMEDAWAY) then
			--   devices('Siren').switchOn().forMin(duration)
			dz.helpers.PlaySound('Herrie, wordt vervangen door werkelijke sirene!!!', NOTIFY_ALARM) 
			dz.notify('ALARM!!!', 'Siren switched on due to Alarm going off', dz.PRIORITY_EMERGENCY)
			return
		end

		-- This point in script it will always be a door..

		if (dv.name == TEST_DOOR) then
			-- Motion detected
			if (dv.bState) then
				--local message -- DB: deze gebruik je alleen in de forEach dus dan kan je die daar ook declaren
				-- first filter on name starting with Door/window
				devices().filter(function(device)
					local name = device.name
					return (string.sub(name, 1, 4) == 'deur')
				end).forEach(function(sensor)

					local message = 'DSWStatus: ' .. 'Device ' .. sensor.name .. ' has status (' .. sensor.state .. '), '
					dz.log(message, dz.LOG_DEBUG)

					if (sensor.bState) then
						dz.log(message, dz.LOG_DEBUG)
						-- if something is open but alarm is not armed vsAlarm_dooropen will switched because the alarm can't be activated
						devices(ALARM_DOOR_OPEN).switchOn()
                        if (dz.security == dz.SECURITY_ARMEDAWAY) then
                            devices(ALARM_DETECTED).switchOn()
                        end
                        
					end

				end)
			end
		end
	end
}
sh script for voice speech on
/home/pi/domoticz/scripts/bash

Code: Select all

#!/bin/sh
killall mplayer
izsynth -e google -v nl "$1"
please shoot! reply/pm with ideas, faults typos whatever you like.
swilting
Posts: 9
Joined: Wednesday 13 June 2018 10:33
Target OS: Raspberry Pi / ODroid
Domoticz version: Last Sta
Location: Netherlands
Contact:

Re: dzVentz Alarm Script starts here..

Post by swilting »

Hi, is it possible to ad some shutters to this script, something like: if alarm is armed close shutter1 ?
And instead of alarm on / off I would like four states, full off /ground floor doors only / ground floor and doors / full on
Is this possible?

Thanx in advance
Robert
Posts: 16
Joined: Saturday 01 March 2014 18:48
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVentz Alarm Script starts here..

Post by Robert »

swilting wrote:Hi, is it possible to ad some shutters to this script, something like: if alarm is armed close shutter1 ?
And instead of alarm on / off I would like four states, full off /ground floor doors only / ground floor and doors / full on
Is this possible?

Thanx in advance
Be aware that opening shutters would be a better option in case of escaping. Same for fire alarms. Make escape route free of obstacles. When burglar alarm is activated, they are already in your house. If you wanna leave your house to avoid a meet and greet, open everything Image


Verzonden vanaf mijn iPhone met Tapatalk
swilting
Posts: 9
Joined: Wednesday 13 June 2018 10:33
Target OS: Raspberry Pi / ODroid
Domoticz version: Last Sta
Location: Netherlands
Contact:

Re: dzVentz Alarm Script starts here..

Post by swilting »

Robert,
That is a good option indeed,but my question was, "when I switch the alarm on, then automatically close the shutters, not when the alarm is triggerd :-) that would be to late :-)

Reading more about programming this should be possible
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVentz Alarm Script starts here..

Post by waaren »

swilting wrote: Friday 04 January 2019 11:22 Robert,
That is a good option indeed,but my question was, "when I switch the alarm on, then automatically close the shutters, not when the alarm is triggerd :-) that would be to late :-)

Reading more about programming this should be possible
I would not combine code triggered by setting the alarm states with code that is supposed to do stuff based on an actual alarm. These are quite different type of events.
Example of script reacting to security state change below

Code: Select all

-- secTrigger

return { 
        on =        {   
                        security = { domoticz.SECURITY_ARMEDAWAY, 
                                     domoticz.SECURITY_ARMEDHOME,
                                     domoticz.SECURITY_DISARMED },
                    },
         
    execute = function(dz,item)
        local shutter   = dz.devices(nnn) 			-- change nnn to ID of your shutterDevice
        
        dz.log("Event has been triggered by securityPanel. State is now: " .. item.trigger,dz.LOG_FORCE)    

        if item.trigger == dz.SECURITY_ARMEDAWAY or item.trigger == dz.SECURITY_ARMEDHOME then
            shutter.close()
            							-- add more actions here when needed on ARMED*
        else        						-- Disarmed
          							-- actions needed on DISARMED here 
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Opus
Posts: 21
Joined: Monday 05 October 2015 9:31
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVentz Alarm Script starts here..

Post by Opus »

Has anybody this idea completed? Sitting here over my old LUA script an find no way for nice working.
dressie
Posts: 156
Joined: Monday 25 May 2015 22:44
Target OS: Raspberry Pi / ODroid
Domoticz version: V2020.2
Location: Netherlands
Contact:

Re: dzVentz Alarm Script starte here..

Post by dressie »

heggink wrote:Interesting. I started the original wiki and, as if yesterday have begun completely rewriting my alarm system due to dzvents being much more flexible. One objective was to reduce all the dummy devices that were needed before, the other is to reduce current complexity. Unfortunately I did find some unexpected behaviour in dzvents (armaway function switches domoticz.security straight to 'armed away' instead of after the timer) and, until that is resolved, still need a dummy.
Let me know when it's done. I'm still using the old dzvents "ideAlarm", but since the developer left it hasn't been updated or worked on.
I'm curious about a new one.

Verstuurd vanaf mijn SM-G970F met Tapatalk

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest