Collecting and Sharing Media Files

Thanks @zodyking . This post is a collaboration between Zody and myself.

We both noticed that multiple users are having difficulty understanding the methods of collecting images and audio files and videos - General Media files - and using them in other functions in Home Assistant.

For instance, You have a sound file like 'You Got Mail" from AOL days that you want to play when the mail comes or you have a video generated by a camera and you want to play these things with media player. How do you save that from the outside in a place that Media player can find it?

Well between these examples and the Home Assistant Docs Page about this, you should be able to do this now.

Storing media in the /media folder so it can be easily left out of the backup

When I want to store media, I want to be able to easily not back it up, so I do this. The backup integration has a specific button to exclude the media folder. Helpful if you have big media files that you don’t want to save and you don’t want to backup.

For me in configuration.yaml

homeassistant:
  media_dirs:
local: /media 

Then the /media path becomes part of /local.
And put the mp3 in a folder in HA under

/media/mp3

(The media folder already exists, you would want to add a sub folder to that, or at least I did, to keep your OCD happy.)
When I want to use an MP3, the path is

media-source://media_source/local/mp3/Ships_Bell_4.mp3

Adjust as you desire, but that is a basic version of mine. Works for all media.

If you want to use the WWW folder to store media, this is the way

1) Map your folder in configuration.yaml

If your image is in /config/www/proxy/…, map that folder (or the whole /config/www) under media_dirs:

# configuration.yaml
homeassistant:
  media_dirs:
    local: /media                       # keep default
    www: /config/www                    # exposes ALL of /config/www
    # or, if you prefer just the proxy subfolder:
    # proxy: /config/www/proxy

Then Restart Home Assistant (or Developer Tools → YAML → Reload core configuration if offered). This makes your folder appear in the Media Browser and gives it a media-source name (www or proxy). Home Assistant

2) Reference the file in your AI Task

Now attach the file with a Media Source URI:

- action: ai_task.generate_data
  response_variable: doorbellimagechanged
  data:
    task_name: "doorbell snapshot description"
    instructions: >
      Very briefly describe what you see in this image.
      Focus on people/vehicles; skip background objects.
    attachments:
      # If you mapped "www: /config/www"
      - media_content_id: media-source://media_source/www/proxy/XXXXXXX.jpeg
        media_content_type: image/jpeg

      # If you mapped "proxy: /config/www/proxy" instead, use:
      # - media_content_id: media-source://media_source/proxy/XXXXXXX.jpeg
      #   media_content_type: image/jpeg

However id simply adjust my automation to snap a picture directly from the camera as its easy to do now. (Correct me if i wrong you use to capture snapshot then save it to www under the same file name every time then reference that in google generative ai?)


The Home Assistant Cookbook - Index.

4 Likes