Can I save an image from a web URL as a files to HA?

I have an CCTV camera that supports motion detection but not snapshotting natively in HA.

I can set up an automation to email me if motion is dectected, but I cannot get it to automatically attach a snapshot of that motion.

The camera does give me a URL, and if you access it in a browser it returns a still image as a JPG file.

http://(IP Address)/webcapture.jpg?command=snap&channel=0&user=user&password=password

What code do I need to add to the automation to save the file form this URL to the folder " /share/snapshots/".

I know almost nothing about coding, so I would appreciate it greatly if someone could please type up some sample code, rather than directing me to the help page. I’ve read it but don’t understand most of it.

This is roughly what I have already, it’s just a modified version of what I use for other camera with a snapshot feature. The motion sensor is OK, the email is OK, I just don’t know how to save the image from that URL to my share folder:

alias: Send a picture when loading dock#1 camera#1 one senses motion
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.LD1C1_cell_motion_detection
    from: "off"
    to: "on"
condition: []

action:
 
get the image from this URL http://(IP Address)/webcapture.jpg?command=snap&channel=0&user=user&password=password and save it to the folder /share/snapshots/LD1C1.jpg (folder already set up with all permissions in place)

  - service: notify.email_notification
    data:
      message: There is Activity on Loading Dock#1
      title: There is Activity on Loading Dock#1
      target: email~address.com
      data:
        images:
          - /share/snapshots/LD1C1.jpg

mode: single

Please don’t just say that I need to read the help file, I’m not yet in a place where I understand it.

add in configuration.yaml

shell_command:
get_it: wget -P /share/snapshots/ http://(IP Address)/webcapture.jpg?com…

add an new automation to call the shell command with ever trigger you consider:
action:
- service: shell_command.get_it
That will download the immage. Verify the immage filename

See more:
Shell Command
wget

1 Like

use wget -O /share/snapshots/LD1C1.jpg http://…

Sorry for bumping, but i wanted to download a menu and feed it to Gemini and spent quite some time with figuring out how to pass parameters to wget and just simply download a file to the folder without specifying the filename. So enjoy my findings :slight_smile: Finally managed to do so and the only option was to create a .sh script and run it. Then it:

  • supports parameters
  • can call multiple commands
  • can save $variables

So just do it like that.

shell_command:
  download_image: bash /config/custom_shell/download_image.sh "{{url}}" "{{path}}"

File:

#!/bin/bash

URL="$1"
FILENAME=$(basename "$URL")
wget -O "$2$FILENAME" "$URL"
echo "$FILENAME"

Permissions for your file:

touch download_image.sh
chmod 0755 download_image.sh

Automation:

alias: Test. Generative Ai (Gemini)
description: ""
triggers: []
conditions: []
actions:
  - variables:
      download_response: ""
      response: ""
  - action: shell_command.download_image
    metadata: {}
    data:
      url: "{{ states('sensor.lunch_menu_link') }}"
      path: /config/www/images/menus/
    response_variable: download_response
  - action: google_generative_ai_conversation.generate_content
    data:
      prompt: >
        Today is {{ now().strftime('%A') }}. What's the menu for today? Use this
        format only (without any other text beforehand): **Restaurant
        name**{{"\r\n"}} - [Item X] (price if available) – English translation.
        {{"\r\n"}}
      filenames:
        - /config/www/images/menus/{{download_response.stdout}}
    response_variable: response
  - action: persistent_notification.create
    metadata: {}
    data:
      message: |
        {{response.text}}
mode: restart

This {{ download_response.stdout }} will contain the file name.