Page 2 of 2

Re: create DzVents script based on Smoke Alarm Fibaro Smoke 2

Posted: Thursday 04 June 2020 16:16
by waaren
Ragdag wrote: Thursday 04 June 2020 16:08 Yours also works but the lastupdate can be removed as that is no longer needed since that switch is only triggered once.
Thx for the feedback.

that means that

Code: Select all

if ( item.state == 'On' and item.lastUpdate.secondsAgo > 60 ) or item.state == 'Off'  then
            pushover(PushoverUsers.USER1, PushoverApps.Alarm, 'Brand Alarm', 'Brand Alarm ' .. item.alarmText .. '\nStatus: ' .. dz.devices(item.alarmDevice).state, 1, 'siren' )
end
can become

Code: Select all

pushover(PushoverUsers.USER1, PushoverApps.Alarm, 'Brand Alarm', 'Brand Alarm ' .. item.alarmText .. '\nStatus: ' .. dz.devices(item.alarmDevice).state, 1, 'siren' )

Re: create DzVents script based on Smoke Alarm Fibaro Smoke 2

Posted: Thursday 04 June 2020 16:26
by Ragdag
waaren wrote: Thursday 04 June 2020 16:16
Ragdag wrote: Thursday 04 June 2020 16:08 Yours also works but the lastupdate can be removed as that is no longer needed since that switch is only triggered once.
Thx for the feedback.

that means that

Code: Select all

if ( item.state == 'On' and item.lastUpdate.secondsAgo > 60 ) or item.state == 'Off'  then
            pushover(PushoverUsers.USER1, PushoverApps.Alarm, 'Brand Alarm', 'Brand Alarm ' .. item.alarmText .. '\nStatus: ' .. dz.devices(item.alarmDevice).state, 1, 'siren' )
end
can become

Code: Select all

pushover(PushoverUsers.USER1, PushoverApps.Alarm, 'Brand Alarm', 'Brand Alarm ' .. item.alarmText .. '\nStatus: ' .. dz.devices(item.alarmDevice).state, 1, 'siren' )
I guess so indeed :)

Re: create DzVents script based on Smoke Alarm Fibaro Smoke 2

Posted: Friday 05 June 2020 14:13
by Ragdag
As always Home Automation is never as straight forward as hoped :)
Got an Alarm today through Pushover but only for a Clear not an actual Smoke alert.
Image

It looks like the switch updated its status but did not change anything.
Switch log
Image

In Devices I do see a last update time of the switch in question
Image
I might need to move the trigger to idx 319 as that still has the same changed time as the actual Alarm tests

Domoticz Log

Code: Select all

2020-06-05 13:51:54.596 Status: dzVents: Info: FireAlarm: ------ Start internal script: DZ_FireAlarm: Device: "$Smoke Alarm - Overloop (Z-wave)", Index: 320
2020-06-05 13:51:54.597 Status: dzVents: Debug: FireAlarm: Processing device-adapter for $Smoke Alarm - Schuur: Switch device adapter
2020-06-05 13:51:54.598 Status: dzVents: Debug: FireAlarm: OpenURL: url = https://api.pushover.net/1/messages.json
2020-06-05 13:51:54.598 Status: dzVents: Debug: FireAlarm: OpenURL: method = POST
2020-06-05 13:51:54.598 Status: dzVents: Debug: FireAlarm: OpenURL: post data = {"message":"Brand Alarm Overloop\nStatus: Clear","priority":1,"sound":"siren","title":"Brand Alarm","token":"aaa","user":"bbb"}
2020-06-05 13:51:54.598 Status: dzVents: Debug: FireAlarm: OpenURL: headers = {["Content-Type"]="application/json"}
2020-06-05 13:51:54.598 Status: dzVents: Debug: FireAlarm: OpenURL: callback = FireAlarm
2020-06-05 13:51:54.599 Status: dzVents: Info: FireAlarm: ------ Finished DZ_FireAlarm
Also a weird log entry
2020-06-05 13:51:54.597 Status: dzVents: Debug: FireAlarm: Processing device-adapter for $Smoke Alarm - Schuur: Switch device adapter

The idx that triggered was the Overloop

Re: create DzVents script based on Smoke Alarm Fibaro Smoke 2

Posted: Saturday 06 June 2020 10:42
by Ragdag
waaren wrote: Thursday 04 June 2020 16:16
Ragdag wrote: Thursday 04 June 2020 16:08 Yours also works but the lastupdate can be removed as that is no longer needed since that switch is only triggered once.
Thx for the feedback.

that means that

Code: Select all

if ( item.state == 'On' and item.lastUpdate.secondsAgo > 60 ) or item.state == 'Off'  then
            pushover(PushoverUsers.USER1, PushoverApps.Alarm, 'Brand Alarm', 'Brand Alarm ' .. item.alarmText .. '\nStatus: ' .. dz.devices(item.alarmDevice).state, 1, 'siren' )
end
can become

Code: Select all

pushover(PushoverUsers.USER1, PushoverApps.Alarm, 'Brand Alarm', 'Brand Alarm ' .. item.alarmText .. '\nStatus: ' .. dz.devices(item.alarmDevice).state, 1, 'siren' )
Is there a way to check a switches previous state to see if it actually toggled from off to on?
I'll probably need to work with persistent variable for the 3 switches

Re: create DzVents script based on Smoke Alarm Fibaro Smoke 2

Posted: Saturday 06 June 2020 11:07
by waaren
Ragdag wrote: Saturday 06 June 2020 10:42 Is there a way to check a switches previous state to see if it actually toggled from off to on?
No
I'll probably need to work with persistent variable for the 3 switches
Yes persistent variables or use virtual devices in your script. In pseudo code something like:

Code: Select all

on =
{ actualswitches }

if virtualSwitch.state ~= actualSwitch.state then
    virtualSwitch.toggleSwitch()  
    otherActions based on new state
else
  noAction needed
end

Re: create DzVents script based on Smoke Alarm Fibaro Smoke 2

Posted: Saturday 06 June 2020 11:19
by Ragdag
waaren wrote: Saturday 06 June 2020 11:07
Ragdag wrote: Saturday 06 June 2020 10:42 Is there a way to check a switches previous state to see if it actually toggled from off to on?
No
I'll probably need to work with persistent variable for the 3 switches
Yes persistent variables or use virtual devices in your script. In pseudo code something like:

Code: Select all

on =
{ actualswitches }

if virtualSwitch.state ~= actualSwitch.state then
    virtualSwitch.toggleSwitch()  
    otherActions based on new state
else
  noAction needed
end
Man as always you are fast :D

I went with the Persistant variables
could you do a sanity check of my code :)

Code: Select all

local scriptVar = 'FireAlarm'

return 
{
    on = 
    {
        devices = 
        {
            303, -- Schuur
            320, -- Overloop
            330, -- Zolder
        }
    },
    
    data =  
    { 
        lastStateSchuur = { initial = '' },
        lastStateOverloop = { initial = '' },
        lastStateZolder = { initial = '' },
    }, ,    
    
    logging = 
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

	execute = function(dz, item)
	    
        if item.isHTTPResponse then
            dz.log(item.data,dz.LOG_DEBUG)
            return
        end
        
        local function pushover(PushoverUser, PushoverApp, title, message, priority, sound)
            dz.openURL({
                url = 'https://api.pushover.net/1/messages.json',
                method = 'POST',
                callback = scriptVar,
                postData = 
                {
                    token = PushoverApp.token,
                    user =  PushoverUser.key,
                    message = message,
                    title = title or 'Pushover from dzVents',
                    priority = priority or 0,
                    sound = sound or 'echo',
                },
            })
        end

        local PushoverUsers = 
        {
            ['User1'] = { ['key'] = 'aaa' },
            ['User2'] = { ['key'] = 'bbb'},
            ['Familie'] = { ['key'] = 'ccc'},
        }

        local PushoverApps = 
        {
            ['Domoticz'] = { ['token'] = 'ddd' },
            ['Huis'] = { ['token'] = 'eee' },
            ['Test'] = { ['token'] = 'fff' },
            ['Was'] = { ['token'] = 'ggg' },
            ['Alarm'] = { ['token'] = 'hhh' },
        }
	    
	    
        if item == dz.devices(303) then
            if dz.devices(303).state ~= dz.data.lastStateSchuur then
                item.alarmDevice = 336
                item.alarmText = 'Schuur'
                dz.log('Schuur current State: ' .. dz.devices(303).state, dz.LOG_DEBUG)
                dz.log('Schuur previous State: ' .. dz.data.lastStateSchuur, dz.LOG_DEBUG)
                dz.data.lastStateSchuur = dz.devices(303).state
            else
                dz.log('Nothing to do with Schuur', dz.LOG_DEBUG)
                return
            end
        elseif item == dz.devices(320) then
            if dz.devices(320).state ~= dz.data.lastStateOverloop then
                item.alarmDevice = 326
                item.alarmText = 'Overloop'
                dz.log('Overloop current State: ' .. dz.devices(303).state, dz.LOG_DEBUG)
                dz.log('Overloop previous State: ' .. dz.data.lastStateSchuur, dz.LOG_DEBUG)
                dz.data.lastStateOverloop = dz.devices(320).state
            else
                dz.log('Nothing to do with Overloop', dz.LOG_DEBUG)
                return
            end
        elseif item == dz.devices(330) then
            if dz.devices(330).state ~= dz.data.lastStateZolder then
                item.alarmDevice = 329
                item.alarmText = 'Zolder'
                dz.log('Zolder current State: ' .. dz.devices(303).state, dz.LOG_DEBUG)
                dz.log('Zolder previous State: ' .. dz.data.lastStateSchuur, dz.LOG_DEBUG)
                dz.data.lastStateZolder = dz.devices(330).state
            else
                dz.log('Nothing to do with Zolder', dz.LOG_DEBUG)
                return
            end
        else
            dz.log('Unknown device', dz.LOG_ERROR)
            return
        end

        pushover(PushoverUsers.User1, PushoverApps.Alarm, 'Brand Alarm', 'Brand Alarm ' .. item.alarmText .. '\nStatus: ' .. dz.devices(item.alarmDevice).state, 1, 'siren' )

    end
}

Re: create DzVents script based on Smoke Alarm Fibaro Smoke 2

Posted: Saturday 06 June 2020 11:53
by waaren
Ragdag wrote: Saturday 06 June 2020 11:19 I went with the Persistant variables
could you do a sanity check of my code :)
Looks good !

Not sure if the extra comma before logging is going to cause an error but you wil see it quickly enough..

Matter of personal preference but I don't like repeating (allmost) the same code lines when it can be prevented.
So I would do it like below.


Code: Select all

local scriptVar = 'FireAlarm'

return
{
    on =
    {
        devices =
        {
            303, -- Schuur
            320, -- Overloop
            330, -- Zolder
        },
    },

    data =
    {
        lastStateSchuur = { initial = '' },
        lastStateOverloop = { initial = '' },
        lastStateZolder = { initial = '' },
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = scriptVar,
    },

    execute = function(dz, item)

        local function pushover(PushoverUser, PushoverApp, title, message, priority, sound)
            dz.openURL({
                url = 'https://api.pushover.net/1/messages.json',
                method = 'POST',
                callback = scriptVar,
                postData =
                {
                    token = PushoverApp.token,
                    user =  PushoverUser.key,
                    message = message,
                    title = title or 'Pushover from dzVents',
                    priority = priority or 0,
                    sound = sound or 'echo',
                },
            })
        end

        local PushoverUsers =
        {
            ['User1'] = { ['key'] = 'aaa' },
            ['User2'] = { ['key'] = 'bbb'},
            ['Familie'] = { ['key'] = 'ccc'},
        }

        local PushoverApps =
        {
            ['Domoticz'] = { ['token'] = 'ddd' },
            ['Huis'] = { ['token'] = 'eee' },
            ['Test'] = { ['token'] = 'fff' },
            ['Was'] = { ['token'] = 'ggg' },
            ['Alarm'] = { ['token'] = 'hhh' },
        }

        local schuur = dz.devices(303)
        local overloop = dz.devices(320)
        local zolder = dz.devices(319)

        schuur.alarmDevice = 336
        schuur.alarmText = 'Schuur'
        overloop.alarmDevice = 326
        overloop.alarmText = 'Overloop'
        zolder.alarmDevice = 329
        zolder.alarmText = 'Zolder'

        local lastState = 'lastState' .. item.alarmText

        if item.state ~= dz.data[lastState] then
            dz.log(item.alarmText .. ' current State: ' .. item.state, dz.LOG_DEBUG)
            dz.log(item.alarmText .. ' previous State: ' .. dz.data[lastState], dz.LOG_DEBUG)
            dz.data[lastState] = item.state
            pushover(PushoverUsers.User1, PushoverApps.Alarm, 'Brand Alarm', 'Brand Alarm ' .. item.alarmText .. '\nStatus: ' .. dz.devices(item.alarmDevice).state, 1, 'siren' )
        else
            dz.log('Nothing to do with ' .. item.alarmText, dz.LOG_DEBUG)
        end

    end
}

Re: create DzVents script based on Smoke Alarm Fibaro Smoke 2

Posted: Saturday 06 June 2020 12:12
by Ragdag
Yours look again a lot more elegant.

What is the reason you don't define these as local?

Code: Select all

        schuur.alarmDevice = 336
        schuur.alarmText = 'Schuur'
        overloop.alarmDevice = 326
        overloop.alarmText = 'Overloop'
        zolder.alarmDevice = 329
        zolder.alarmText = 'Zolder'

Re: create DzVents script based on Smoke Alarm Fibaro Smoke 2

Posted: Saturday 06 June 2020 12:16
by waaren
Ragdag wrote: Saturday 06 June 2020 12:12 What is the reason you don't define these as local?
:oops: Forgot

Re: create DzVents script based on Smoke Alarm Fibaro Smoke 2

Posted: Saturday 06 June 2020 12:25
by Ragdag
waaren wrote: Saturday 06 June 2020 12:16
Ragdag wrote: Saturday 06 June 2020 12:12 What is the reason you don't define these as local?
:oops: Forgot
So they should be local :P
Will give yours a try

Re: create DzVents script based on Smoke Alarm Fibaro Smoke 2

Posted: Saturday 06 June 2020 15:09
by waaren
Ragdag wrote: Saturday 06 June 2020 12:25 So they should be local :P
Will give yours a try
No these are just attributes of already declared tables. They need to stay as they are !