Shell_Command Output

Hi all,

I’m currently trying to figure out if it’s possible to read the output of a shell_command call and check for errors.

My use case:

  • I have a script that is triggered when I flick a switch on my HA, which backs up my Raspberry Pi.
  • I also have an MQTT sensor that I use as an ‘info box’ to publish messages to to alert me of successes/failures/things in progress.

At present, my script updates the sensor to say it’s running, run the shell command, and then updates the sensor to say it is complete.
However I am now wanting to improve this process by tailoring an output message depending on success or failure of the backup.

So my question is, how would I determine the outcome of the shell command script, and tailor the message to suit?

Here is what I have so far:

backup_rpi:
  alias: Backup RPi
  mode: single # if the script is called again while still running, don't do anything
  sequence:
    - service: mqtt.publish
      data:
        topic: backup/message
        payload: "RUNNING: RPi Backup"
        qos: 2
        retain: true
    - service: shell_command.backup_rpi
    - service: mqtt.publish
      data:
        topic: backup/message
        payload: "COMPLETE: RPi Backup"
        qos: 2
        retain: true

Thanks in advance! :slight_smile:

Check out a command line sensor: https://www.home-assistant.io/integrations/sensor.command_line/

You’ll want to always return 0 (success) from whatever you call because HA will only update the state if the call succeeds, but the output can be whatever you want.

Thanks for the suggestion. Unfortunately I don’t think that quite fits with what I’m after.

Command line sensor is a sensor, and therefore has a poll interval. I’m not looking for a timed sensor, as I want this to only be triggered manually.
Also, the output is templated to update the sensor state - when I need to be able to send a contextual MQTT message :slight_smile:

Could you not add a command at the end of your backup script which publishes to MQTT directly? I’d do it like this, the same when the script starts. So HA only sends the command to execute the backup and the backup script itself publishes that it has started and when it has ended.