dzvents and z-wave siren/strobe  [Solved]

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

Moderator: leecollings

Post Reply
imdos
Posts: 34
Joined: Thursday 03 August 2017 21:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

dzvents and z-wave siren/strobe

Post by imdos »

Hi all,

I want to change my current dzvents scripts to the following scenario. And I could really use some help to get the logic in order. Since I geuss my current script doesn't function the way I designed it and my use case has changed a bit.

Scenario
  • zwave - 30 seconds blink strobe only
  • turn off zwave switch
  • change zwave config
  • wait a second (or maybe more) until the config has changed and been acknowledged by domoticz
  • turn on zwave (with strobe and alarm siren) max 2 minutes.
Some caveats:
  1. The limit of 2 minutes has to do with jurisdiction. You're only allowed to have a siren on the outside for a short period of time.
  • I should be able to turn off the alarm at any moment in time.
Current

Code: Select all

-- local alarm_status = 'ALARMSTATUS'
-- local alarm_switch = 'Z-wave Alarm'
-- local alarm_state = 'woonveilig - Bedieningspaneel'
-- # Zet het buitenalarm aan als er een alarm actie is
-- was .forMin(5).afterSec(10)
-- dzVents 2.2.0 versie
-- help
-- https://github.com/domoticz/domoticz/blob/9f75e45f994f87c8d8ce9cb39eaab85886df0be4/scripts/dzVents/documentation/README.md
-- domoticz.devices(123).switchOn().forMin(5).afterSec(10)
-- if lastUpdate.secondsAgo() then
--         local led1 = domoticz.devices('$Vloer-LED-R')
    return {
        active = true,
        on = {
                devices = {
                        'ALARMSTATUS'
                }
        },
        execute = function(domoticz, alarmSwitch)
        local alarm_status = domoticz.devices('ALARMSTATUS')
        local alarm_switch = domoticz.devices('Z-wave Alarm')
        local alarm_state = domoticz.devices('woonveilig - Bedieningspaneel')
                if (alarmSwitch.state == 'On' and alarm_state.state == 'On' ) then
                        alarm_state.switchOn().forMin(10).afterSec(15)
                        domoticz.log('Buitenalarm gaat over 10 seconden aan', domoticz.LOG_DEBUG)
                                -- notify(subject, message, priority, sound, extra, subsystem)
                                domoticz.notify('Alarm', 'Buitenalarm gaat over 10 seconden aan', domoticz.PRIORITY_EMERGENCY, domoticz.SOUND_NONE, domoticz.EXTRA_NONE,domoticz.NSS_PUSHOVER)
                                domoticz.notify('Alarm', 'Buitenalarm gaat over 10 seconden aan', domoticz.PRIORITY_EMERGENCY, domoticz.SOUND_NONE, domoticz.EXTRA_NONE,domoticz.NSS_PUSHBULLET)
                        -- domoticz.devices('Z-wave Alarm').switchOff().afterSec(130)
                        -- domoticz.devices('Z-wave Alarm').switchOn().afterSec(10)
                end
        end
    }
My solution in bash

Code: Select all

#!/bin/bash
#### Laat het buitenalarm afgaan.
## Eerst 30 seconden LED's flikkeren (strobe)
## Dan stop zetten
##
#       32      rflink433       008A562A        1       Stroom z-wave alarm buiten      Light/Switch    TriState        On      -       -               2019-04-08 18:31:26
#       35      zwave   00000B01        1       Z-wave Alarm    Light/Switch    Switch  Off
IDX_alarm=35
IDX_power=32
aanzetten()
{
#### TODO: # Check of de instellingen goed staan.
# We zetten de instellingen goed
#Strobe only - will not automatically stop
curl 'http://127.0.0.1:8080/json.htm?type=command&param=applyzwavenodeconfig&idx=9&valuelist=1_U3Ryb2JlIE9ubHk%3D_2_V2lsbCBOb3QgQXV0b21hdGljYWxseSBTdG9w_'
# Alarm aan; alleen strobe
curl -s 'http://127.0.0.1:8080/json.htm?type=command&param=switchlight&idx=$IDX_alarm&switchcmd=On'
sleep 30
curl -s 'http://127.0.0.1:8080/json.htm?type=command&param=switchlight&idx=$IDX_alarm&switchcmd=Off'
sleep 1
#All enabled - 120s
curl 'http://127.0.0.1:8080/json.htm?type=command&param=applyzwavenodeconfig&idx=9&valuelist=1_QWxsIEVuYWJsZQ%3D%3D_2_MTIwcw%3D%3D_'
sleep 1
curl -s 'http://127.0.0.1:8080/json.htm?type=command&param=switchlight&idx=$IDX_alarm&switchcmd=On'
}
uitzetten()
{
curl -s 'http://127.0.0.1:8080/json.htm?type=command&param=switchlight&idx=$IDX_alarm&switchcmd=Off'
sleep 1
curl -s 'http://127.0.0.1:8080/json.htm?type=command&param=switchlight&idx=$IDX_power&switchcmd=Off'
}
case "${1}" in
        start)
        aanzetten
        stop)
        uitzetten
        *)
                echo "Usage: ${0} {start|stop}" >&2
                exit 1
                ;;
esac

exit 0
Making use of: Raspbian(SSD), dz Beta, Woonveilig, Z-Wave(alarm+multi-sensor), RFLink(blinds), P1, Yeelight, Xiaomi temp sensors, Tasmota(SonoFF, Blitzwolf SHP2, Shelly1)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzvents and z-wave siren/strobe

Post by waaren »

imdos wrote: Saturday 20 April 2019 15:46 I want to change my current dzvents scripts to the following scenario. And I could really use some help to get the logic in order. Since I guess my current script doesn't function the way I designed it and my use case has changed a bit.
Maybe I can help but before that I have a couple of questions and some remarks.
In your profile I see you are on the beta channel. Current dzVents version in latest Beta is 2.4.18 but you are referring to dzVents 2.2.0. Which version is active ?

In line

Code: Select all

if (alarmSwitch.state == 'On' and alarm_state.state == 'On' ) then 
you check if the woonveilig panel is active and in the line following that you will switch the woonveilig panel to On for some time. Is that what you intend or should the check be to see if the panel is not 'On' ?

domoticz.EXTRA_NONE does not exists; use nil.
The notify command can handle multiple notification subsystems. The command would then become

Code: Select all

domoticz.notify('Alarm', 'Buitenalarm gaat over 10 seconden aan', domoticz.PRIORITY_EMERGENCY, domoticz.SOUND_NONE, nil,{domoticz.NSS_PUSHOVER,domoticz.NSS_PUSHBULLET})
The wiki for latest version can be found here
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
imdos
Posts: 34
Joined: Thursday 03 August 2017 21:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: dzvents and z-wave siren/strobe

Post by imdos »

waaren wrote: Saturday 20 April 2019 17:15
imdos wrote: Saturday 20 April 2019 15:46 I want to change my current dzvents scripts to the following scenario. And I could really use some help to get the logic in order. Since I guess my current script doesn't function the way I designed it and my use case has changed a bit.
Maybe I can help but before that I have a couple of questions and some remarks.
In your profile I see you are on the beta channel. Current dzVents version in latest Beta is 2.4.18 but you are referring to dzVents 2.2.0. Which version is active ?
That is some leftover code from my previous installation. I used to be on 3.9722 or something which could only handle up until 2.2. I've done a new installation of raspbian and I'm running on stretch lite. The domoticz version is 4.10602; one minor before the latest beta.
waaren wrote: Saturday 20 April 2019 17:15 In line

Code: Select all

if (alarmSwitch.state == 'On' and alarm_state.state == 'On' ) then 
you check if the woonveilig panel is active and in the line following that you will switch the woonveilig panel to On for some time. Is that what you intend or should the check be to see if the panel is not 'On' ?
I'm not sure if the check should stay the same; but it's working is correct. The "woonveilig" alarm system is designed to trigger an event (push to servicecenter) when you have an active subscription with "woonveilig".

I've found some PHP scripts to simulate that servicecenter and have modified the code to my likings to switch a virtual switch which I have defined with local alarm_status = 'ALARMSTATUS'. With that use case I make sure that the alarm is still active and it hasn't been turned off during the grace period of 20 seconds as defined in the alarm system.
waaren wrote: Saturday 20 April 2019 17:15 domoticz.EXTRA_NONE does not exists; use nil.
Right; that may be part of the leftover code which was required in 2.1.x or 2.2.0. I'll modify it.
waaren wrote: Saturday 20 April 2019 17:15 The notify command can handle multiple notification subsystems. The command would then become

Code: Select all

domoticz.notify('Alarm', 'Buitenalarm gaat over 10 seconden aan', domoticz.PRIORITY_EMERGENCY, domoticz.SOUND_NONE, nil,{domoticz.NSS_PUSHOVER,domoticz.NSS_PUSHBULLET})
That's a bit more convenient; I like the suggestion.
waaren wrote: Saturday 20 April 2019 17:15 The wiki for latest version can be found here
That's my second source of information. I usually borrow some code from the forum or the examples and then adjust to my likings with some help from the wiki. I'm not a real coder and more someone who breaks code to gain access. I should invest more time in learning to writing code; but can't find the time in my current job nor personal life.
Making use of: Raspbian(SSD), dz Beta, Woonveilig, Z-Wave(alarm+multi-sensor), RFLink(blinds), P1, Yeelight, Xiaomi temp sensors, Tasmota(SonoFF, Blitzwolf SHP2, Shelly1)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzvents and z-wave siren/strobe

Post by waaren »

imdos wrote: Saturday 20 April 2019 15:46 I want to change my current dzvents scripts to the following scenario. And I could really use some help to get the logic in order. Since I guess my current script doesn't function the way I designed it and my use case has changed a bit.
Kind of hard to test without these Zwave devices. But can you have a look at the below script and report back what's working and what needs to be amended ? Also please feel free to ask for clarification / and comment.

Code: Select all

local scriptVar = "makeNoise"

return
{
        on = { 
                devices = { 'ALARMSTATUS' },
                httpResponses = { scriptVar } 
             },

             logging =   { 
                        level   =   domoticz.LOG_DEBUG,    -- change to LOG_ERROR when script executes OK
                        marker  =   scriptVar,
                    },

    execute = function(dz, item)
        local alarmStatus = dz.devices('ALARMSTATUS')
        local alarmSwitch = dz.devices('Z-wave Alarm')
        local alarmState = dz.devices('woonveilig - Bedieningspaneel')
        
        local baseURL = dz.settings['Domoticz url'] .. "/json.htm?type=command&param=applyzwavenodeconfig&idx=" .. alarmSwitch.id .. "&valuelist="
        local strobe = "1_U3Ryb2JlIE9ubHk%3D_2_V2lsbCBOb3QgQXV0b21hdGljYWxseSBTdG9w"
        local strobeAndSirene = '1_QWxsIEVuYWJsZQ%3D%3D_2_MTIwcw%3D%3D_'
        
        local function triggerJSON(url, response, delay)
            local delay = delay or 0
            dz.openURL({    url = url,
                            method = "GET",
                            callback = response}).afterSec(delay)
        end
        
        if item.isDevice and alarmSwitch.active and alarmState.active then
                    -- set alarmSwitch to Strobe only 
                    dz.openURL(baseURL .. strobe)
                    
                    alarmState.cancelQueuedCommands()
                    alarmState.switchOn().afterSec(15)
                    alarmState.switchOff().afterMin(10)
                    
                    dz.log('Buitenalarm (strobe only) gaat over 10 seconden aan', dz.LOG_DEBUG)
                    alarmSwitch.switchOn().afterSec(10) -- Ensure Strobe only is active
                    
                    triggerJSON(baseURL .. strobeAndSirene, scriptVar, 45)
        elseif alarmSwitch.active and alarmState.active then    -- strobeAndSirene now active
            dz.log('Buitenalarm gaat over 10 seconden aan', dz.LOG_DEBUG)
            dz.notify('Alarm', 'Buitenalarm gaat 10 seconden na ' .. dz.time.rawTime .. ' aan',dz.PRIORITY_EMERGENCY,dz.SOUND_NONE,nil,{dz.NSS_PUSHOVER,dz.NSS_PUSHBULLET})
            alarmSwitch.switchOn().afterSec(10)
            alarmSwitch.switchOff().afterSec(130)
        else
            dz.log(alarmSwitch.name .. 'and / or ' .. alarmState.name .. ' not active; no action needed', dz.LOG_DEBUG)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
imdos
Posts: 34
Joined: Thursday 03 August 2017 21:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: dzvents and z-wave siren/strobe

Post by imdos »

Wow; such a quick and great response. Thanks; I owe you a beer right now.

I'm a bit puzzled how this is working.

Code: Select all

if item.isDevice and alarmSwitch.active and alarmState.active then
And how to distinct between state one and two (e.g. with or without a siren). Should I use a variable or something?
Making use of: Raspbian(SSD), dz Beta, Woonveilig, Z-Wave(alarm+multi-sensor), RFLink(blinds), P1, Yeelight, Xiaomi temp sensors, Tasmota(SonoFF, Blitzwolf SHP2, Shelly1)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzvents and z-wave siren/strobe

Post by waaren »

imdos wrote: Monday 22 April 2019 17:13 Wow; such a quick and great response. Thanks; I owe you a beer right now.

I'm a bit puzzled how this is working.

Code: Select all

if item.isDevice and alarmSwitch.active and alarmState.active then
And how to distinct between state one and two (e.g. with or without a siren). Should I use a variable or something?
The distinction is made based on what triggered the script.
It can be triggered by the ALARMSTATUS device (causing item.isDevice to be true) and by the response to the domoticz API (causing item.isDevice to be false)
In both cases you only want the script to only do something if both the alarmSwitch - and the alarmState states are "On" (=> device.active == true)
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
imdos
Posts: 34
Joined: Thursday 03 August 2017 21:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: dzvents and z-wave siren/strobe

Post by imdos »

Okay; had some time to look at the script again and made some modifications. Furthermore I have commented it a bit inside the code.

The script should be triggered only when the dummy switch (local alarmStatus = dz.devices('ALARMSTATUS')) is active. Furthermore the alarm (local alarmState = dz.devices('woonveilig - Bedieningspaneel')) should still be on.

Then the alarm should start strobing within 3 seconds for 20 seconds. Afterwards it should change to strobe and siren and do that for 120seconds (the maximum allowed value).

Does this work as I have in mind? :?:

Code: Select all

local scriptVar = "makeNoise"

return
{
        on = { 
                devices = { 'ALARMSTATUS' },
                httpResponses = { scriptVar } 
             },

             logging =   { 
                        level   =   domoticz.LOG_DEBUG,    -- change to LOG_ERROR when script executes OK
                        -- level   =   domoticz.LOG_DEBUG,    -- change to LOG_ERROR when script executes OK
                        marker  =   scriptVar,
                    },

    execute = function(dz, item)
        local alarmStatus = dz.devices('ALARMSTATUS')
        local alarmSwitch = dz.devices('Z-wave Alarm')
        local alarmState = dz.devices('woonveilig - Bedieningspaneel')
	local zwaveIDX = 9 
        local baseURL = dz.settings['Domoticz url'] .. "/json.htm?type=command&param=applyzwavenodeconfig&idx=" .. zwaveIDX .. "&valuelist="
        local strobe = "1_U3Ryb2JlIE9ubHk%3D_2_V2lsbCBOb3QgQXV0b21hdGljYWxseSBTdG9w_"
        local strobeAndSirene = '1_QWxsIEVuYWJsZQ%3D%3D_2_MTIwcw%3D%3D_'
        
        local function triggerJSON(url, response, delay)
            local delay = delay or 0
            dz.openURL({    url = url,
                            method = "GET",
                            callback = response}).afterSec(delay)
        end
        
        if item.isDevice and alarmStatus.active and alarmState.active then
		-- set alarmSwitch to Strobe only 
		dz.openURL(baseURL .. strobe)
		-- how does the script get past this point and turn on the strobe? 
		-- cancelQueuedCommands should be used to supress errors while changing values and using the device.
		alarmSwitch.cancelQueuedCommands()
		-- alarmSwitch.switchOn() -- shouldn't this be deleted?
		-- turn on strobe only for roughly 20 seconds
		alarmSwitch.switchOff().afterSec(25)
		dz.log('Buitenalarm (strobe only) gaat over 3 seconden aan', dz.LOG_DEBUG)
		dz.notify('Alarm', 'Buitenalarm (strobe only) gaat 3 seconden na ' .. dz.time.rawTime .. ' aan',dz.PRIORITY_EMERGENCY,dz.SOUND_NONE,nil,{dz.NSS_PUSHOVER,dz.NSS_PUSHBULLET})
		alarmSwitch.switchOn().withinSec(3) -- Ensure Strobe only is active
		-- setup new trigger. This should trigger the other loop. Does this depends on the delay only?
		triggerJSON(baseURL .. strobeAndSirene, scriptVar, 28)
        elseif alarmStatus.active and alarmState.active then    -- strobeAndSirene now active
            dz.log('Buitenalarm gaat over 3 seconden aan', dz.LOG_DEBUG)
            dz.notify('Alarm', 'Buitenalarm gaat 3 seconden na ' .. dz.time.rawTime .. ' aan',dz.PRIORITY_EMERGENCY,dz.SOUND_NONE,nil,{dz.NSS_PUSHOVER,dz.NSS_PUSHBULLET})
	    alarmSwitch.cancelQueuedCommands()
            alarmSwitch.switchOn().afterSec(3)
            alarmSwitch.switchOff().afterSec(130)
        else	-- will this ever be triggered? 
            dz.log(alarmSwitch.name .. 'and / or ' .. alarmState.name .. ' not active; no action needed', dz.LOG_DEBUG)
        end
    end
}
Making use of: Raspbian(SSD), dz Beta, Woonveilig, Z-Wave(alarm+multi-sensor), RFLink(blinds), P1, Yeelight, Xiaomi temp sensors, Tasmota(SonoFF, Blitzwolf SHP2, Shelly1)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzvents and z-wave siren/strobe

Post by waaren »

imdos wrote: Tuesday 02 July 2019 13:55 Okay; had some time to look at the script again and made some modifications. Furthermore I have commented it a bit inside the code.
Does this work as I have in mind? :?:
I don't see anything strange in the script that could cause dzVents to complain. Question about the within statement: within is to be used for random periods. Do you really want this to be random or are you looking for afterSec which is to be used for fixed periods ?
Only way to be reasonably sure is test, test, TEST.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
imdos
Posts: 34
Joined: Thursday 03 August 2017 21:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: dzvents and z-wave siren/strobe  [Solved]

Post by imdos »

Great; the script works as expected and I have learned a bit more.

I have changed some triggers and disabled some commands to not actually trigger the alarm; but still sending the Z-wave commands.

But totally missed the nuance between these two calls:
dz.openURL(baseURL .. strobe)
triggerJSON(baseURL .. strobeAndSirene, scriptVar, 28)

the withinSec was just for fun and really doesn't matter since the time is too short. But I know the deviation between within and after. Last week I also learned that both are case sensitive and withinmin won't work but withinMin does.
Making use of: Raspbian(SSD), dz Beta, Woonveilig, Z-Wave(alarm+multi-sensor), RFLink(blinds), P1, Yeelight, Xiaomi temp sensors, Tasmota(SonoFF, Blitzwolf SHP2, Shelly1)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzvents and z-wave siren/strobe

Post by waaren »

imdos wrote: Wednesday 03 July 2019 16:44 But totally missed the nuance between these two calls:
dz.openURL(baseURL .. strobe)
triggerJSON(baseURL .. strobeAndSirene, scriptVar, 28)
the dz.openURL is just sending the URL. the triggerJSON does send a URL after 28 seconds and include a callback string. When the url in this function returns this callback string, the script is triggered again (on = httpResonses)
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: Google [Bot] and 1 guest