Problems executing shell command in script

This has been a very deep rabbit hole but I think I’m close to the end. I’m trying to send a camera snapshot via the SMTP integration. However the jpg images generated by camera.snapshot cause the SMTP integration to log the following error:

Logger: homeassistant.components.smtp.notify
Source: components/smtp/notify.py:285
integration: SMTP (documentation, issues)
First occurred: 8:51:10 PM (1 occurrences)
Last logged: 8:51:10 PM

Attachment ./www/snapshots/frontyard_snapshot2.jpg has an unknown MIME type. Falling back to file

Further digging uncovers the suggestion to use ImageMagick to “convert” the jpg to a jpg file - SMTP - unknown MIME type - #4 by ladefoged81

After a few hours spent getting that running, I can run magick to convert the file from the command line and successfully send the converted file using the SMTP integration. The problems setting this up were due to the package installer not installing the needed JPEG support. In addition to the

apk --update add imagemagick && apk --no-cache add msttcorefonts-installer fontconfig && update-ms-fonts && fc-cache -f

suggested in the thread, I had to also run

apk add imagemagick-jpeg

to actually add jpeg support to magick.

After all that, I’m now trying to automate that process using a shell command. I have the following in my configuration.yaml file:


shell_command:
  fixup_jpeg: "magick /config/www/snapshots/frontyard_snapshot2.jpg /config/www/snapshots/frontyard_snapshot2.jpg"

And the following in my script (huge thanks to Aussie_Adam for Guide for CCTV Snapshot on motion, send to Google Generative AI & get notification with description & snapshot):

alias: Camera - Frontyard - Snapshot, AI & Notification
sequence:
  - metadata: {}
    data:
      filename: ./www/snapshots/frontyard_snapshot1.jpg
    target:
      device_id: 0a7bd481b9f24aaa85a2be44f5392912
    enabled: true
    action: camera.snapshot
  - delay:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 500
    enabled: true
  - metadata: {}
    data:
      filename: ./www/snapshots/frontyard_snapshot2.jpg
    target:
      device_id: 0a7bd481b9f24aaa85a2be44f5392912
    enabled: true
    action: camera.snapshot
  - delay:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 500
    enabled: true
  - metadata: {}
    data:
      filename: ./www/snapshots/frontyard_snapshot3.jpg
    target:
      device_id: 0a7bd481b9f24aaa85a2be44f5392912
    enabled: true
    action: camera.snapshot
  - metadata: {}
    data:
      prompt: >-
        Motion has been detected, compare and very briefly describe what you see
        in the following sequence of images from my driveway camera number 1.
        What do you think caused the motion alarm? If a person or car is
        present, describe them in detail. Do not describe stationary objects or
        buildings. If you see no obvious causes of motion, reply with "No
        Obvious Motion Detected." Your message needs to be short enough to fit
        in a phone notification. 
      image_filename:
        - ./www/snapshots/frontyard_snapshot1.jpg
        - ./www/snapshots/frontyard_snapshot2.jpg
        - ./www/snapshots/frontyard_snapshot3.jpg
    response_variable: generated_content
    action: google_generative_ai_conversation.generate_content
  - if:
      - condition: template
        value_template: "{{ 'No Obvious Motion Detected.' in generated_content.text }}"
    then:
      - stop: ""
    else:
      - action: shell_command.fixup_jpeg
        data: {}
      - delay:
          hours: 0
          minutes: 0
          seconds: 0
          milliseconds: 500
      - metadata: {}
        data:
          title: Frontyard Motion Detected
          message: "{{generated_content['text'] }}"
          data:
            images:
              - ./www/snapshots/frontyard_snapshot2.jpg
        action: notify.family_sms
mode: single
description: ""

The Google AI has no problems using the three snapshots created, and I can display them in my browser without problems, but the SMTP integration doesn’t like the MIME type so the attempt to use ImageMagick towards the end of the script to convert the one snapshot being send.

Unfortunately I end up with the following log error:

Logger: homeassistant.components.shell_command
Source: /usr/src/homeassistant/homeassistant/components/shell_command/__init__.py:129
integration: Shell Command (documentation, issues)
First occurred: 8:44:46 PM (2 occurrences)
Last logged: 8:51:10 PM

Error running command: `magick /config/www/snapshots/frontyard_snapshot2.jpg /config/www/snapshots/frontyard_snapshot2.jpg`, return code: 127
NoneType: None

Any ideas?

The only thing I can think of offhand is that when running the script from the shell_command section is that it might not have any environment variables to know where to find executables. You might try putting the complete path to magick in the shell_command section.

Adding the path to MAgick doesn’t seem to make a difference:

Logger: homeassistant.components.shell_command
Source: /usr/src/homeassistant/homeassistant/components/shell_command/__init__.py:129
integration: Shell Command (documentation, issues)
First occurred: 8:00:32 AM (1 occurrences)
Last logged: 8:00:32 AM

Error running command: `/usr/bin/magick /config/www/snapshots/frontyard_snapshot2.jpg /config/www/snapshots/frontyard_snapshot2.jpg`, return code: 127

Did you add “/config/www/snapshots/” directory to allowlist_external_dirs ?
allowlist_external_dirs

try (use local instead of www) in the path only in the if statement (leave the rest). ie:

    else:
      - action: shell_command.fixup_jpeg
        data: {}
      - delay:
          hours: 0
          minutes: 0
          seconds: 0
          milliseconds: 500
      - metadata: {}
        data:
          title: Frontyard Motion Detected
          message: "{{generated_content['text'] }}"
          data:
            images:
              - ./local/snapshots/frontyard_snapshot2.jpg
        action: notify.family_sms

@Vlad makes a valid point. Are you storing the initial snapshot in an available external dir?

homeassistant:
   allowlist_external_dirs:
    - /config/www/snapshots
 

snapshots being your own filename

sequence:
  - action: camera.snapshot
    metadata: {}
    data:
      filename: /config/www/snapshots/frontyard.jpg
    target:
      entity_id: camera.xxx
alias: Snapshot
description: ""

All, it’s not a path to the image file that is the problem. I have confirmed that if I run ImageMagic on the image from the terminal:

I can then run the action <Notifications ‘Send a notification with family_sms’> in the script from visual editor and get a message sent with the image, and no errors logged.