temporally store image, for sending notification telegram  [Solved]

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

Moderator: leecollings

Post Reply
hoeby
Posts: 531
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

temporally store image, for sending notification telegram

Post by hoeby »

I want to send a camera snapshot by telegram.
The link for the camera snapshot is http://XXX.XXX.XXX.XXX/jpg/1/image.jpg
Unfortunally i can't get this type of camera added in domoticz. Therefor i was hoping to get 1 scripts for DZvents.

What i have is this. But i got stuck how i can temporally save a snapshot by dzvents.
Googled some and found some, but those scripts are way over my head to get the idea how they exactly work

Code: Select all

local camera1 = http://XXX.XXX.XXX.XXX/jpg/1/image.jpg

return {
    on = { 
       devices = {'Backlight Dashboard'},
    },

    execute = function(domoticz, device)
        if(device.state == 'On') then
          --os.execute('/home/pi/domoticz/scripts/snapshot.sh')
	   domoticz.log('Foto gemaakt en verstuurd', domoticz.LOG_INFO)
	   domoticz.notify('snapshot', camera1, domoticz.PRIORITY_HIGH)
        end
end
}
I have this script, which works.
But was hoping everything could be run by dzvents and not 2 scripts

Code: Select all

#!/bin/sh
SnapFile="/var/tmp/camsnapshot.jpg"
# Krijg snapshot van camera.
# Voer IP-adres in op XXX.XXX.XXX.XXX (houd rekening met user/password, dat script ook snapshot kan maken)
wget -O $SnapFile "http://XXX.XXX.XXX.XXX/jpg/1/image.jpg"
# Send Telegram message with image
curl -s -X POST "https://api.telegram.org/bot"DEZE TEKST VERVANGEN VOOR TELEGRAM API SLEUTEL"/sendPhoto" -F chat_id=DEZE TEKST VERVANGEN VOOR CHAT_ID -F photo="@$SnapFile"
# Remove Image
/bin/rm $SnapFile
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
User avatar
emme
Posts: 909
Joined: Monday 27 June 2016 11:02
Target OS: Raspberry Pi / ODroid
Domoticz version: latest
Location: Milano, Italy
Contact:

Re: temporally store image, for sending notification telegram

Post by emme »

The most dangerous phrase in any language is:
"We always done this way"
hoeby
Posts: 531
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: temporally store image, for sending notification telegram  [Solved]

Post by hoeby »

Thanks Emme,
I got it working. Changed a few thinks and tested it in steps to see what happens (with domoticz.log)
And yes, it works.

Only need to test if it overwrites the made .jpg file. Or i need to delete the made .jpg file.
That's for later to test.

This is what i finished with

Code: Select all

return {
    on = { 
       devices = {'Backlight Dashboard'},
    },

    execute = function(domoticz, device)
        if(device.state == 'On') then
            local teleTok   = 'xxxxxxxxxxxxxxxxxxxxxx'
            local chatId    = 'xxxxxxxxxxxxxxxxxxxxxx'
            local snapFile  = '/home/pi/domoticz/scripts/camera_'
            local domoReq   = 'http://xxx.xxx.xxx.xxx/jpg/1/image.jpg'
            local camName   = 'Voordeur'
            os.execute('wget -O "'..snapFile..camName..'.jpg" "'..domoReq..'"')
            os.execute('curl -s -X POST "https://api.telegram.org/bot'..teleTok..'/sendPhoto?chat_id='..chatId..'" -F photo="@'..snapFile..camName..'.jpg"')
        end
    end
}
Last edited by hoeby on Sunday 27 January 2019 20:26, edited 1 time in total.
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
hoeby
Posts: 531
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: temporally store image, for sending notification telegram

Post by hoeby »

Added this code undernead the 2 other os.execute rows.
Be sure to add it undernead, otherwise the .jpg is deleted before it is sended with telegram

Code: Select all

os.execute("rm " ..snapFile..camName..'.jpg')

This removes the .jpg file. So i am sure that there are no problems with overwrite permisions.
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
User avatar
DieHappy
Posts: 10
Joined: Wednesday 10 January 2018 18:57
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.10717
Location: Netherlands
Contact:

Re: temporally store image, for sending notification telegram

Post by DieHappy »

Hi, can You explain how to set user/ and password.
I get the error 401 connection refused. Looks like it is used and password related.
wervisser
Posts: 29
Joined: Monday 28 November 2016 18:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.1
Contact:

Re: temporally store image, for sending notification telegram

Post by wervisser »

Works perfectly, also with a username/password protected camera.

For some reason it keeps sending two same pictures :?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: temporally store image, for sending notification telegram

Post by waaren »

wervisser wrote: Wednesday 20 May 2020 14:50 For some reason it keeps sending two same pictures :?
Can you switch on scriptlevel debug logging? It might reveal what is happening. Could it be that the script is triggered twice?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
ssk17051980
Posts: 112
Joined: Thursday 08 December 2022 22:15
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: ROMANIA
Contact:

Re: temporally store image, for sending notification telegram

Post by ssk17051980 »

this script still works ?
User avatar
habahabahaba
Posts: 233
Joined: Saturday 18 March 2023 14:44
Target OS: Windows
Domoticz version: 2024.4
Contact:

Re: temporally store image, for sending notification telegram

Post by habahabahaba »

Here is my way to send fotos from cams on motion detect.

But i'm on windows, so some code may not work.

Code: Select all

execute = function(domoticz, device)

	local tBot = '<your bot number>'
	local tChatId = '<your chat id>'
	    
	    local current_date_time = os.date("%Y-%m-%d_%H-%M-%S")

        local camera_url1 = '"http://<user>:<password>@192.168.0.36/webcapture.jpg?command=snap&amp;channel=1"' -- adress of camera snapshot
        
        local image_path1 = '"C:/Domoticz/screensfromcams/cam2floor/'.. current_date_time ..'.jpeg"' -- path to store snapshot
        
        local command1 = 'curl ' .. camera_url1 .. ' > ' ..image_path1  -- команда для выполнения 1
            
              
                os.execute(command1) -- saving snapshot to disk
                    
                --getting the name of last image in folder:
                local command1 = "dir /B /O-D /A:-D C:\\Domoticz\\screensfromcams\\cam2floor\\"
                local f = io.popen(command1)
                local last_file1 = string.sub( f:read("*a"):gsub("\n", ""), 1, 24)
                -- end
                
                -- sending image to telegram:
                domoticz.executeShellCommand('curl -s -X POST "https://api.telegram.org/bot'.. tBot  ..'/sendPhoto" -F chat_id='.. tChatId  ..' -F photo="@C:/Domoticz/screensfromcams/cam2floor/'..last_file1..'" -F disable_notification=false -F caption="Motion 2 floor"')

	end
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest