Notification only when security is armed  [Solved]

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

Moderator: leecollings

Post Reply
johansson
Posts: 75
Joined: Sunday 27 September 2015 15:52
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Finland
Contact:

Notification only when security is armed  [Solved]

Post by johansson »

This is propably too simple, but can't figure it out on my own: I've a perfectly fine working (bash) script sending a snapshot from my camera to Telegram every time when motion is detected, but how on earth I do that in dzvents only when security is armed away or armed home?

Thanks in advance.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Notification only when security is armed

Post by waaren »

waaren wrote: Monday 15 July 2019 11:18
johansson wrote: Sunday 14 July 2019 22:36 This is propably too simple, but can't figure it out on my own: I've a perfectly fine working (bash) script sending a snapshot from my camera to Telegram every time when motion is detected, but how on earth I do that in dzvents only when security is armed away or armed home?
Please have a look at this.

Code: Select all

--[[ Conditional snapshot on motion

 uses: functions like the ones in telegram library from @emme as per the wiki:   https://www.domoticz.com/wiki/Telegram_lua_library
 and: some functions and Constants from dzVents: 

    values to enter in code below
    'motionSensor'
    
    <chatID> = 'your telegram Chat ID'
    <botToken> = 'your telegram bot Token'
    <cameraIDX> = your configured camera idx

]] --

return 
{ 
    on = 
    { 
        timer = {'at 3:31'}, -- housekeeping time
        devices = { 'MotionSensor' }, -- Change to name of your motion sensor
    },  

    logging = { level = domoticz.LOG_DEBUG, marker = 'conditional snaphshot'  },

    data = { snapshotCounter = { initial = 1 }},

    execute = function(dz, item)
        local chatID = ''  -- <chatID> (positive for single user negative for group) ( example: '-1001451683665')
        local cameraIDX = '' -- <camerIDX> (example: 1)
        local botToken = '' -- <botToken> (example: '709006412:AAH61H_vLsdP9stXzB2cW1wr_dLH2qD-xpU'
        
        -- 
        -- no config or other changes necessary below this line
        -- 
        local function snapShot()
            local snapshotFilename = '/tmp/' .. dz.time.rawDate .. '-' .. dz.data.snapshotCounter ..'.jpg'
            local url = dz.settings['Domoticz url']
            local domoticzAPI = '/camsnaphot.jpg?idx=' .. cameraIDX
            local wgetCommand = 'wget -O "' .. snapshotFilename .. '" ' .. ' "' .. domoticzAPI .. '" ' .. url 
            os.execute(wgetCommand)
            dz.log('wget Command: ' .. wgetCommand,dz.LOG_DEBUG)
            dz.data.snapshotCounter = dz.data.snapshotCounter + 1
            return snapshotFilename
        end

        local function sendTelegram(photo)
            local url = 'https://api.telegram.org/bot' .. botToken .. '/sendPhoto?chat_id=' .. chatID
            local curlCommand  = 'curl -s -X POST "' .. url .. '" -F photo=@"' .. photo .. '" &'
            os.execute(curlCommand)
            dz.log('curl Command: ' .. curlCommand,dz.LOG_DEBUG)
            os.execute('touch ' .. photo) -- correct for wget saving snapshot with wrong dateTime stamp
        end

        local function housekeeping()
            findAndRemoveCommand = 'bash -c " find /tmp/*jpg -mtime 1 -exec rm {} \\; " '
            os.execute(findAndRemoveCommand)
            dz.log('findAndRemoveCommand: ' .. findAndRemoveCommand,dz.LOG_DEBUG)
        end

        if item.isTimer then
            housekeeping()
            return
        end

        local securityArmed = ( dz.security == dz.SECURITY_ARMEDAWAY or dz.security == dz.SECURITY_ARMEDHOME )
        if item.active and securityArmed then 
            sendTelegram(snapShot())
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
johansson
Posts: 75
Joined: Sunday 27 September 2015 15:52
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Finland
Contact:

Re: Notification only when security is armed

Post by johansson »

Perfect, thanks! 🙏

Adjusted a bit, came up with the following which seems to work for me:

Code: Select all

return {
	on = {
		devices = {
			'Front door PIR',
		}
	},
	execute = function(dz, device)
 local securityArmed = (dz.security == dz.SECURITY_ARMEDAWAY or dz.security == dz.SECURITY_ARMEDHOME)
        if device.active and securityArmed then 
   		os.execute('sudo /home/xxxxxxxx/domoticz/scripts/frontdoorsnapshop.sh')

end
end
}
My main problem was how to check security status, couldn't find it from the dzwiki etc. Thanks again 👍

Obviously additional to snapshots a short video clip stored somewhere would be nice as well: any creative ideas for that as well?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Notification only when security is armed

Post by waaren »

johansson wrote: Monday 15 July 2019 11:49 Perfect, thanks! 🙏

Adjusted a bit, came up with the following which seems to work for me:

Code: Select all

   		os.execute('sudo /home/xxxxxxxx/domoticz/scripts/frontdoorsnapshop.sh')
A potential problem with this approach is that the curl command in the Shell script might hang the event system for the duration of the script. If the telegram url does not respond for some time no other events will be triggered (because the domoticz event system is single threaded). If you have not covered for that in your frontdoorsnapshop.sh then best to ask os.execute to run this script in background by adding a '&'

Code: Select all

   		os.execute('sudo /home/xxxxxxxx/domoticz/scripts/frontdoorsnapshop.sh &')
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
johansson
Posts: 75
Joined: Sunday 27 September 2015 15:52
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Finland
Contact:

Re: Notification only when security is armed

Post by johansson »

waaren wrote: Monday 15 July 2019 12:07 If you have not covered for that in your frontdoorsnapshop.sh then best to ask os.execute to run this script in background by adding a '&'

Code: Select all

   		os.execute('sudo /home/xxxxxxxx/domoticz/scripts/frontdoorsnapshop.sh &')
If that's not done by default, I'm quite sure I haven't :D Thanks for the pointer. In general, is there a difference when running the script with os.execute vs the way in your example, i.e. is one way better than the other?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Notification only when security is armed

Post by waaren »

johansson wrote: Monday 15 July 2019 13:52 In general, is there a difference when running the script with os.execute vs the way in your example, i.e. is one way better than the other?
It's more a matter of personal preference. I like to keep the amount of different script-types limited so if I can do it in one dzVents script, I will. Same goes for the the os.execute calls vs. dz.openURL calls. I prefer to use openURL where possible and -easy but this is not always doable.
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