Automation with ssh script

Hi,

My goal is :
Turn on power on external USB disk (using aqara zigbee)
Run a backup script
Turn off power on external USB disk

My issue: Disk power works, then the script is running for 10 sec (not finished) and the power go down.
I don’t know why the automation is not waiting the end of the script before the power down.

My automation :

  • id: ‘1647470229028’
    alias: NS
    description: ‘’
    trigger: []
    condition: []
    action:
    • type: turn_on
      device_id: 8570f4019e4c5ae6248e91190ab2d1b6
      entity_id: switch.prise_aqara
      domain: switch
    • delay:
      hours: 0
      minutes: 0
      seconds: 15
      milliseconds: 0
    • service: shell_command.backup
      data: {}
    • type: turn_off
      device_id: 8570f4019e4c5ae6248e91190ab2d1b6
      entity_id: switch.prise_aqara
      domain: switch
      mode: single

Thanks for your help !

The automation doesn’t recognize the script is still running. If you know how long the backup takes you could add a delay so it waits before turning off. there is another way the script could tell HA it has run by say writing to a sensor or using MQTT but probably just as easy to use a delay.

Couldn’t you just use the exec node in nodered instead!

In a case like this, if the answer is nodered you have the wrong question

1 Like

Well if shell_command is forking which I presume it is then an automation like this is not fit for purpose.

Adding silly delays only compounds the issue as a process can be delayed by any number of factors, slow io. High load, network timeouts… I could go on

Encapsulate the exec properly. I’m not a node red fan boy but seems like it’s fit enough to catch the backup system call

it can be but a simple delay will suffice. I certainly wouldn’t be adding node-red just for this. The backup script can notify HA if you want to go to that trouble but a delay will do what he wants

It does not by itself. HA will wait up to 10 sec for the command to complete, then fail in timeout.
Obviously the command itself can (and must in this case) “fork” by using, e.g., nohup.

Hi Guys,

Thanks for replies.
The backup times is not fix so I can’t put a delay value.
I never test nodered so I don’t know how to do that.
Maybe a sensor but how ?

Thanks !

  • At the end of your backup script, write, e.g., the current date time to a file.
  • Have a File sensor on that file (File - Home Assistant)
  • Trigger on that file in an automation to do whatever post-backup actions.
1 Like

Yep, heard something about a 10 sec timeout and obviously this makes sense to ensure that the process is not blocking but the problem is obviously that there’s no tracking of the child

:+1:Nice and simple

Another option would be to put at the end of your script:

curl -X POST http://your-ha-ip:port/api/webhook/backup_done

Then make your post-backup automation with the trigger

  trigger:
    - platform: webhook
      webhook_id: "backup_done"

cf Automation Trigger - Home Assistant

1 Like

Yep, better :+1: