Using a variable (attribute) as a url for downloading

hi folks,
I have what I hope will be a simple request for help.
I would like to be able to use the downloader.download_file function within HA with a url which is different each time I call it…
The url variable is available through an attribute for an entity I have.
So I can retrieve the correct (and variable) attribute here:

'{{state_attr('camera.aarlo_arlo4', 'last_thumbnail')}}' 

I’ve tried a few things without success.
I’m not particularly strong with scripting so not sure where to start with that…

Something to the effect of below..
filename: aarlosnapshot.jpg
overwrite: true
subdir: arlo4
url: >-
  '{{state_attr('camera.aarlo_arlo4', 'last_thumbnail')}}' 

Any clues?
Thanks so much in advance,

Your template is not correct, don’t quote multi-line templates, and don’t use the same type of quotes inside and outside the template. So this:

url: >-
  {{state_attr('camera.aarlo_arlo4', 'last_thumbnail')}}

or this:

url: "{{state_attr('camera.aarlo_arlo4', 'last_thumbnail')}}"

However I’m not sure if the URL option accepts templates.

Thanks for the excellent tips.

You’re right I can’t reference a template in a url (just tried)…
I can extract the attribute as a state, could that help at all?
Not sure where to start. Or am I best to abandon this route?

I’m wanting to extract the generated arlo thumbnail to use it for deepstack processing.
Each thumbnail has a unique url…

State or attribute - you would still need a template, which is unsupported.

Perhaps a shell command?

They can be templated.

Okay, makes sense.
I don’t suppose you might know of an example shell command that I could use to reverse engineer?

Cheers,

No, sorry.

:smiley:
bummer.

downloader.download_file is a service call, right? You should be able to template that in a script:

script:
  get_last_thumbnail:
    sequence:
      - service: downloader.download_file
        data_template:
          url: "{{ state_attr('camera.aarlo_arlo4', 'last_thumbnail') }}"
          filename: aarlosnapshot.jpg
          overwrite: true
          subdir: arlo4
1 Like

Hi @Troon, I finally got a chance to try it out.
This is my script:

download_arlo4_thumbnail:
  alias: Download Arlo4 Thumbnail
  mode: single
  sequence:
  - data:
      filename: aarlo4snapshot.jpg
      overwrite: true
      subdir: arlo4
      url: "{{ state_attr('camera.aarlo_arlo4', 'last_thumbnail') }}"
    service: downloader.download_file
  icon: mdi:camera-front-variant

Unfortunately it didn’t work, got the following error in the log:

Download Arlo4 Thumbnail: Error executing script. Invalid data for call_service at pos 1: invalid url for dictionary value @ data['url']```

I used the script editor via the front end..

data_template: not data:. See Rule 1.

You may not be able to do that via the UI.

You’re right, no luck through the UI.
thanks for the tip, I’ve seen those before and didn’t understand really what was being instructed. Makes sense now.

This is what I have now:

download_arlo4_thumbnail:
  alias: Download Arlo4 Thumbnail
  mode: single
  sequence:
    - service: downloader.download_file
      data_template:
        filename: aarlo4snapshot.jpg
        overwrite: true
        subdir: arlo4
        url: "{{ state_attr('camera.aarlo_arlo4', 'last_thumbnail') }}"

I get the same error in the log.

Download Arlo4 Thumbnail: Error executing script. Invalid data for call_service at pos 1: invalid url for dictionary value @ data['url']

Did you restart HA or at least reload scripts (under Configuration / Server Controls)? The UI editor automatically does this (I think) but if you edit scripts offline, you need to get HA to reload them.

If you did, please let us know what output you get when you put your template in Developer Tools / Templates:

{{ state_attr('camera.aarlo_arlo4', 'last_thumbnail') }}

@troon! you’re awesome! That worked, all I needed was to restart HA. Another great tip!
I’m quite curious, the UI script editor produced something quite different from your recommended script. What the heck!?
At least I have yours to learn from now.
Once again this community has come to my rescue, wonderful.

1 Like

I just tried the script mentioned above to use it as starting point for my file download and I get: Message malformed: extra keys not allowed @ data['download_arlo4_thumbnail']

Did anything change since 2020 with the file downloader? The parameters from above are still the same.

I used the YAML editor in the script section:

download_arlo4_thumbnail:
  alias: Download Arlo4 Thumbnail
  mode: single
  sequence:
    - service: downloader.download_file
      data_template:
        filename: menueplan.pdf
        overwrite: true
        subdir: menueplan
        url: "https://www.senevita.ch/wAssets/docs/00_standorte/dorfmatt/gastronomie/Menueplan-{{ (now().timestamp()+(0-now().weekday()+7)*86400) | int | timestamp_custom('%d.%m') }}"