Stuck with trying to save an MQTT image/jpg to a file

Hi,

I have following setup:

A jpg image being pushed into home/Mailbox/camera/image MQTT topic.
The HA is configured via auto discovery, which creates image.mailbox_mailbox_camera.
This all works fine (I can publish a jpg into the topic above and it is displayed on the Picture Card).

What I am trying to do with that image is to save it to disk when it is published.

I have tried to create an automation, but the problem is that I can’t understand how the notify.file meant to work at all.

I have set up the File Service in the UI and tried using it directly:

automation:                                                                                                         
  - id: save_mailbox_image                                                                                          
    alias: "Save Mailbox Image"                                                                                     
    trigger:                                                                                                        
      - platform: mqtt                                                                                              
        topic: "home/Mailbox/camera/image"                                                                          
    action:                                                                                                         
      - service: notify.file                                                                               
        data_template:                                                                                              
          message: "{{ trigger.payload }}"                                                                          
          filename: "/var/www/html/uploads/mailbox_{{ now().strftime('%Y-%m-%d_%H-%M-%S') }}.jpg"

The above doesn’t work and results in this error:

Error: Service notify.file not found

I found mention that I am supposed to use notify.send_message instead but there is absolutely no documentation how it meant to tie into the File service.

I have bumbled around until I got to this stage:

automation:                                                                                                         
  - id: save_mailbox_image                                                                                          
    alias: "Save Mailbox Image"                                                                                     
    trigger:                                                                                                        
      - platform: mqtt                                                                                              
        topic: "home/Mailbox/camera/image"                                                                          
    action:                                                                                                         
      - service: notify.send_message                                                                                
        target:                                                                                                     
          entity_id: notify.file_2                                                                                  
        data_template:                                                                                              
          message: "{{ trigger.payload }}"                                                                          

Where notify.file_2 service is configured with this template: /var/www/html/uploads/mailbox_{{ now().strftime('%Y-%m-%d_%H-%M-%S') }}.jpg except the template is taken literally and it doesn’t evaluate, I didn’t get the image anyway as it created a file:

cat /var/www/html/uploads/mailbox_\{\{\ now\(\).strftime\(\'%Y-%m-%d_%H-%M-%S\'\)\ \}\}.jpg 
Home Assistant notifications (Log started: 2025-03-27T10:06:42.691958+00:00)
--------------------------------------------------------------------------------

I am currently tempted to write a small python script and give up on HA…
The problem here (having 20+ years of programming experience) I do not understand the logic or structure, and documentation is very lacking.

I am sure the problem is trivial to solve and I am holding it wrong.

Thank you.

Create an MQTT camera entity:

Use the camera snapshot action to save the image to disk.

See: https://www.home-assistant.io/integrations/camera/#action-snapshot

1 Like

Thank you!
It seems simpler than my original idea, except the reason why I am using “image” as opposed to “camera” is because my attempts to setup MQTT autodiscovery for the “camera” were all in vain.

If only the documentation had an example of autodiscovery payload…

I figured out the auto discovery bit:

It kind of looks like this:

{
'topic': 'home/Mailbox/camera/snapshot', 
'unique_id': 'Mailbox_camera', 
'device': {
    'model': 'ESP32', 
    'identifiers': ['Mailbox'], 
    'manufacturer': 'Custom', 
    'name': 'Mailbox'
    }, 
'name': 'Mailbox Camera', 
'platform': 'mqtt'
}

It works!
Here is the automation:

- id: '1743137411847'
  alias: Save Mailbox Camera Snapshot
  description: ''
  trigger:
  - platform: state
    entity_id:
    - camera.mailbox_mailbox_camera
    enabled: false
  - platform: mqtt
    topic: home/Mailbox/camera/trigger
    payload: ACK
  condition: []
  action:
  - service: camera.snapshot
    target:
      entity_id: camera.mailbox_mailbox_camera
    data:
      filename: /var/www/html/uploads/mailbox_{{ now().strftime('%Y-%m-%d_%H-%M-%S')
        }}.jpg
  mode: single

files are there:

ls -la /var/www/html/uploads/
total 152
drwxr-xr-x 2 homeassistant homeassistant   123 Mar 28 18:09 .
drwxr-xr-x 3 root          root             52 Mar 27 19:39 ..
-rw-r--r-- 1 homeassistant homeassistant 49440 Mar 28 18:06 mailbox_2025-03-28_18-06-09.jpg
-rw-r--r-- 1 homeassistant homeassistant 49440 Mar 28 18:06 mailbox_2025-03-28_18-06-12.jpg
-rw-r--r-- 1 homeassistant homeassistant 48024 Mar 28 18:09 mailbox_2025-03-28_18-09-05.jpg

Thank You very much @tom_l !

1 Like