Blink lights e.g. Mini alarm  [Solved]

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

Moderator: leecollings

Post Reply
deluccio
Posts: 7
Joined: Saturday 03 October 2020 10:58
Target OS: NAS (Synology & others)
Domoticz version: 2020.2
Contact:

Blink lights e.g. Mini alarm

Post by deluccio »

I want to flash some lights and play a sound via my Google Nest Mini if the door is left open (or when there is a burglar who broke in to my garage) when i am not at home.
In addition and to be complete for the script, I want to get notified on my phone as well. For this i was thinking of sending me an e-mail, else it makes no sense to blink the lights :lol:

So this script is a kind of mini alarm, but suggestions are welcome to improve.

So far nothing special i assume, besides the fact that im just a beginner with dzEvents and still learning. I made a start, with help of other scripts on this forum, but it still doesn't do what i needs to do.. and I hope that someone can help me out on this.

In the past I normally worked with Blockly for all of my scripts and i'm known to Domoticz for several years, so i'm not a total noob. For that moment Blockly was sufficient but now I also want to make me more comfortable with dzEvents.

I'm using the following sensors:
  • Door sensor - Doorsensor13
  • Phone status (based on Wifi ping) - Phone1
  • Phone status (based on Wifi ping) - Phone2
  • Lamp1 - YeeLight_Roomname1
  • Lamp2 - YeeLight_Roomname2
  • Dummy Switch (details below) - Dummy
In Blockly i made the script as simple as:

Code: Select all

If 
Doorsensor = Open and Phone1 = off and Phone2 = off

Do 
Write to log "Door open"
Send email with subject: Door Open and message: Lorem Ipsum to: [email protected]
Set scene: Alarm = On (To change to color to red)
Set Yeelight_Roomname off after 1 seconds
Set Yeelight_Roomname on after 2 seconds
Set Yeelight_Roomname off after 3 seconds
Set Yeelight_Roomname on after 4 seconds
Set Yeelight_Roomname off after 5 seconds
(And so on)

Else if
Deursensor = Closed
Set scene: Alarm = Off (To change to color to back to neutral)
Set Yeelight_Roomname off
The problem in Blockly is that you need to program every step manually and that you need to think, what Blockly needs to do.

As you can understand i don't want to flash to lights only 3 times (as the scripts above describes) but i want to scare the bruglar (if any..).
In addition, i also want to play a sound on my Google Nest Mini (for example an Siren sound).

To make the script neighbourhood proof, I don't want to play the sound 24/7 when i'm for example at holiday. To make it clear what i want:
  • When the door is open and i'm not at home
  • I want to flash the lights based on a scene for 5 minuts and play a sound for 5 minuts
  • After 5 minuts the lights goes off and sounds goes off
  • If door is still open after 10 minuts
  • Then flash the lights based on a scene for 5 minuts (no sound this time!)
  • After 5 minuts the lights goes off
  • Then flash the lights based on a scene for 5 minuts
and so on until the door is closed, or when i'm at home, or when the Dummy Switch is turned (manually) on.

The Dummy Switch is for example in case of a false alarm so i can turn i manually off myself from any location or when my parants/friends or anyone needs to enter the garage when im not at home.

Ok let's stop talking and let me show you the script so far... (what is far from complete, yet)

Code: Select all

return 
{
    on = 
    {
        timer = 
        {
            'every minute',              
       },
    },
 
    logging =
    {
        level = domoticz.LOG_DEBUG,    
        marker = 'Door alert',
    },
    
    execute = function(dz, timer)
        
        local flashRepeats = 5
        
        dz.log('======================== ', dz.LOG_DEBUG)
        dz.log('======================== ', dz.LOG_DEBUG)
        dz.log('======================== ', dz.LOG_DEBUG)
        dz.log('Door trigger started ' .. timer.trigger, dz.LOG_DEBUG)
        
        local Door sensor     = dz.devices('Doorsensor13')
        local Lamp1      = dz.devices('YeeLight_roomname1')
        local Lamp2  = dz.devices('YeeLight_roomname2')
		local Phone1  = dz.devices('Phone1')
		local Phone2  = dz.devices('Phone2')
		local Dummy Switch  = dz.devices('Dummy')
		

        
        if (Doorsensor13.state == 'Open' and Doorsensor13.lastUpdate.minutesAgo > 1) then 
            dz.log('Door trigger: Door is open, blink lights... ', dz.LOG_DEBUG)
            --dz.notify('Door alert', 'Doorsensor13 is more then 5 minuts open!', dz.PRIORITY_HIGH)
        
            --first turn off the lights if they are on
            YeeLight_roomname1.switchOff().checkFirst()
            YeeLight_Roomname2.switchOff().checkFirst()
            
            for i = 1, (flashRepeats * 2), 2 do
                dz.log('Now scheduling lights on after ' .. i .. ' seconds... ', dz.LOG_DEBUG)
                YeeLight_Roomname1.switchOn().afterSec(i)       
                YeeLight_Roomname2.switchOn().afterSec(i)

                dz.log('Now scheduling lights off after ' ..( i + 1) .. ' seconds... ', dz.LOG_DEBUG)
                YeeLight_Roomname1.switchOff().afterSec(i + 1)
                YeeLight_Roomname2.switchOff().afterSec(i+1)
            end
            
             -- restore On state when state was active before script started
            if YeeLight_Roomname1.active then YeeLight_Roomname1.switchOn().afterSec(flashRepeats * 2 + 2) end 
            if YeeLight_Roomname2.active then YeeLight_Roomname2.switchOn().afterSec(flashRepeats * 2 + 2) end
            
        end
    end
}
The things that does not work so far:
  • There is no connection between phone status or dummy switch
  • The lights does not flash for 5 minuts, but only 5 times ofc i can count the 5 times = 5 seconds, but isn't there an easier way? Or is it better to count the timing and set amount of flash to 300 times (5 minutes x 60 seconds) :oops:
  • The lights does not goes to a specific scene
  • There is no sound option (I found this topic (https://domoticz.com/forum/viewtopic.php?t=24698) but i don't get the sound working at all, but skip the sound in that case for now to make the progress easier, perhaps i need to make a new topic for that)
Help would be appreciated.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Blink lights e.g. Mini alarm  [Solved]

Post by waaren »

deluccio wrote: Saturday 03 October 2020 12:06 I want to flash some lights and play a sound via my Google Nest Mini if the door is left open (or when there is a burglar who broke in to my garage) when i am not at home.
Help would be appreciated.
You trigger the script every minute but only act when a device changed state (the door) Why not triggering on device door ?

My approach would be to code the requirements step by step and test them separately. Add to the final script when tested OK.

the Phone part could look like this

Code: Select all

		local Phone1  = dz.devices('Phone1').state == 'On' -- assuming 'On' means Phone is in the house
		local Phone2  = dz.devices('Phone2').state == 'On'
		if Phone1 or Phone2 then return end                     -- If one of them have state 'On' end the execution of this script


The flickering of the light 5 times every minute during 5 minutes could look like

Code: Select all

flashRepeats = { 5, 5 } -- minutes , seconds

for repeatMinutes = 0, flashRepeats[1] - 1  do
    for repeatSeconds = 0, ( flashRepeats[2] - 1 ) * 2, 2 do
        local delayOn =  repeatMinutes * 60 + repeatSeconds
        local delayOff = delayOn + 1
    
        dz.log('Now scheduling lights on after ' .. delayOn .. ' seconds.. ', dz.LOG_DEBUG)
        YeeLight_Roomname1.switchOn().afterSec(delayOn)       
        YeeLight_Roomname2.switchOn().afterSec(delayOn)
        
        dz.log('Now scheduling lights off after ' .. delayOff .. ' seconds..  ', dz.LOG_DEBUG)
        YeeLight_Roomname1.switchOff().afterSec(delayOff)
        YeeLight_Roomname2.switchOff().afterSec(delayOff)
    end
end
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest