Shell command crashes my automations

I have had a Shell command to suspend my PC I created a year or so ago …

shell_command:
  suspend_pc: >-
    ssh -i /config/HA2Main -o 'StrictHostKeyChecking=no' [email protected]
    '[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms";
[System.Windows.Forms.Application]::SetSuspendState([System.Windows.Forms.PowerState]::Suspend, $false, $false);'

I’ve been calling from an Automation triggered by me going out. The automation does other stuff as well like turning lights and heating off; some before and some after the Shell Service call.

alias: Graham goes out
description: ""
trigger:
  - platform: state
    entity_id:
      - person.graham
    from: home
    to: not_home
    for:
      hours: 0
      minutes: 5
      seconds: 0
condition:
  - condition: state
    entity_id: binary_sensor.owl_away
    state: "off"
action:
  - service: light.turn_off
    data: {}
    target:
      entity_id: light.light_white_study_top
  - service: shell_command.suspend_pc
    data: {}
  - service: climate.set_hvac_mode
    data:
      hvac_mode: "off"
    target:
      entity_id: climate.hall_thermostat
mode: single

I’m pretty sure it used to work fine but I noticed the other day that things after the Shell Service call aren’t running. Checking the Automation trace I can see an error after or at the Shell Service call that stops the Automation running any further. The suspend_pc still works from the Automation and from Developer Tools.
I could put it last but I’m keen to know what is wrong as I am writing another automation for when I go to bed where I don’t want it to be last, and that one stops there as well.
The error log says…

Logger: homeassistant.components.shell_command
Source: components/shell_command/__init__.py:94
Integration: Shell Command (documentation, issues)
First occurred: 20 September 2023 at 08:30:05 (14 occurrences)
Last logged: 17:10:22

Timed out running command: `ssh -i /config/HA2Main -o 'StrictHostKeyChecking=no' [email protected] '[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms"); [System.Windows.Forms.Application]::SetSuspendState([System.Windows.Forms.PowerState]::Suspend, $false, $false);'`, after: 60s

Am I correct in inferring that this only started on the 20 September? That might be (not sure) when I upgraded to HA 2023.9.1 (haven’t gone for 2 or 3 yet)? Something to do with Service call responses perhaps?

Any ideas anyone?

I think so, but since you seem to get a timeout, I’m not so sure. It could be something else.

I’ve had some challenges running ssh commands before (it it would run fine, but there would be an error in the logs), so here’s something you can try: When you run an ssh command, the connection doesn’t terminate normally. This results (if I remember correctly) in a 127 error code. So, make a proper shell script, redirect stderr from that command to stdout or just /dev/null and do an explicit exit with code 0 (success). Call that script from your shell script.

So, it could be the new mechanism picks up on this error.

Also have a look at this: Script Syntax - Home Assistant.

Thanks Pieter. Don’t think for one moment that i know what I’m doing here; I’m just good at copying stuff. I get what you are saying but do not have the knowledge to implement your suggestion. I might just take the shell command out and put it into an automation of its own with the same trigger. Not so tidy though!

I just added a random argument to accept a return parameter and now it seems to work without stopping the ‘I go out’ automation. I’ll check in the morning if it also fixed the ‘I go to bed’ one too.

Edit: Spoke too soon. Sometimes it works sometimes it doesn’t! No ideas why.

You can append this to the end of a command. It redirects errout (2) to stdout (1) and sends all of it into the void (/dev/null). Doing this also means there will be no evidence of this running in your HA log (caveat).

> /dev/null 2>&1

Ending a script with exit 0 will tell the caller it ran successfully.

Thanks.
I’ve added ‘> /dev/null 2>&1 &’. The last ‘&’ i got from another thread; it seems to have done the trick. Time will tell as I was still getting random failures without it.
The Trace was telling me either a timeout error at just over 60s, or success at around 20s. Why is the successful shell script taking around 20s? The PC goes to sleep much quicker than that.

That & makes the process run in the background. It probably is only suppressing any errors.