How to switch of PC that's running HA linux via terminal command

Hi

For the moment my PC running, Debian & HA supervisor is in a small upstairs room , second house, 400 kms away

Now while all works well, in the latest heat wave that particulier room and the PC get extremely hot and the PC shuts down because it’s overheating. Tiny PC no fans :frowning:

Now I written the following script, shutting down the PC before it’s gets really to hot which works, but it doesn’t shut down the PC cleanly. Is there a way of sending an instruction to the Debian’s terminal giving it the shutdown command when the temp sensor reaches a certain figure, so the its shuts down cleanly ? rather than just turning of the plug ?

alias: Turn PC of if too hot
description: turn PC of if too hot
trigger:
  - platform: numeric_state
    entity_id: sensor.ewelink_bureau_temperature
    above: "30"
condition:
  - condition: device
    type: is_on
    device_id: bd5f25bcce1f69bbd82fac448dbc14c5
    entity_id: 4f17c06d0995d34dd2c34dace954a09d
    domain: switch
action:
  - type: turn_off
    device_id: bd5f25bcce1f69bbd82fac448dbc14c5
    entity_id: 4f17c06d0995d34dd2c34dace954a09d
    domain: switch
    enabled: false
mode: single

PS I could send myself a mail with warnings about the heat and shut it down at a distance, but as I’m not always at home this isn’t optimum

Firstly, please format you post correctly for the forum: https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

Secondly, yes. Use a shell command to turn off the PC:

Many thanks

I’ve edited the yaml text, so that should read better for everone

Thanks for pointing that out

I’ll read up tomorrow on using shell commands
Trevor

So I’ve been trying to do this all morning, sorry I’m pretty hopeless

I’ve created a sh file that works if I run it using my local terminal

I created using your examples in my main yaml file

shell_command:
  #shutdown_PC: "sudo shutdown"
  #call_remote: curl http://example.com/ping
  my_script: /usr/local/bin/ha_shutdown.sh

that saves Okay so I presume that’s correct,

In automation I’m trying

alias: test shutdown
description: test shutdown
trigger:
  - platform: numeric_state
    entity_id: sensor.ewelink_bureau_temperature
    above: "30"
condition: []
action: 
  - service: shell_command.my_script
shell_command:
    my_script: /usr/local/bin/ha_shutdown.sh
mode: single

But whatever I try I can’t get past an error message Message malformed: extra keys not allowed @ data[‘shell_command’]

I’ve tried adding, deleting spaces to no avail, also adding [] after action, again to no avail, also trying with " beginning/end of /usr/local/bin/ha_shutdown.sh

So to be honest I’m lost
What am I missing ?

There are a few things that you need to change. Please look at the automation example for using shell_command here: Shell Command - Home Assistant

First, the my_script should start with ‘bash’. Only then will your script be executed. Something like:

my_script: bash /usr/local/bin/ha_shutdown.sh

Secondly, the shell_command block should be outside the automation in your configuration file.

Hope these two changes get it to work.

Thanks

I can get this to save without any problems now

alias: test shutdown
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.ewelink_bureau_temperature
    above: "30"
condition: []
action:
  - service: shell_command.my_script
    data: {}
mode: single

But nothing runs, If I add the two remaining lines, wherever I place them

shell_command:
my_script: bash /usr/local/bin/ha_shutdown.sh 

it gives me the same error
Message malformed: extra keys not allowed @ data[‘shell_command’]

and I’m not sure what you mean by
“Secondly, the shell_command block should be outside the automation in your configuration file.”

I spent most of today checking Google, and the forums, but not found one single yaml exemple with a shell_command using what i’m trying to so

So back again

I’ve changed quite a few things , but still not there

1:
I created a /config/shell_commands.yaml
with :

# shell_commands.yaml
my_script: bash /usr/local/bin/ha_shutdown.sh
#my_script: bash /usr/share/hassio/homeassistant/ha_shutdown.sh

as you can see tried differnet places ofr the sh as well, even in My Document folder
all scripst have had chmod +x done

2:
In the /config/configuration.yaml I added the following

shell_command: !include shell_commands.yaml

3
Created a Automation

alias: test shutdown
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.ewelink_bureau_temperature
    above: "30"
condition: []
action:
  - service: shell_command.my_script
    data: {}
mode: single

The above script seems good, just :

4
When I test ‘Run’ with the ‘Call a service’ or ‘Execute shell command’, I’m getting the following in the logs, though no errors are shown

Command executed: bash /usr/local/bin/ha_shutdown.sh: 127
bash: /usr/local/bin/ha_shutdown.sh: No such file or directory

The sh files runs perfectly if I use my Linux terminal,

#!/bin/bash
(sudo shutdown)

So it seems that HA doesn’t want to ‘see’ and run my bash file
Anyone got any ideas ?

Where is your ha_shutdown.sh file stored? It seems its not in /usr/local/bin since HA cannot find it. Just put it into /config for example and change your shell command to my_script: bash /config/ha_shutdown.sh

Thanks

But it is in /usr/local/bin/
I also tried it in /usr/share/hassio/homeassisant/ (running Debian with Docker)

I can physically see them and open them running Nano

Neither work with HA, both give me the error message No such file or directory

Again both run perfectly if I run the bash script using the Debian Terminal

Because you run them from inside the Core docker container, and they are outside the docker mapped directory, so can’t be seen.

Hi

Now that’s what I thought, so that why I tried it in the hassio/homeassistant folder, obviously still not the right place

so the question is where is the correct folder that I could place the sh file in ?

Put it into the HA config directory. I believe that in your installation you will find it in

/home/homeassistant/.homeassistant

This is where the configuration.yaml, automations.yaml etc are stored.

Tried that as well, same error message
in Debian 12 it’s /usr/share/hassio.homeassistant

That’s where all the yaml are stored for instance

I also tried changing the root owner to me chown trevor didn’t change anything, so set it back to root

Maybe try this:

Ahh, yes HA is a docker container. You need to ssh into the host and execute the command. Try this:

  my_srcipt: 'ssh username@ip_address_of_host sudo /usr/local/bin/ha_shutdown.sh'

You need to add user homeassistant to the sudo group and edit the sudoers file to allow user homeassistant to execute the command.

Thanks

It’s ‘better’, but not there yet
added user homeassistant and added it to sudoers

tried
#!/bin/bash
(sudo shutdown)
or
ssh [email protected] ‘sudo shutdown -h now’

both give me an 255 error rather than the 127,

this error I seen on the forums , but haven’t found the answer yet

Sorry my fault. Create 1 user, eg. homeassistant or use an existing one and add it to the sudo group and sudoers file. Then use this user for the ssh command. For user homeassistant you would use:

shell_command:
  my_script: 'ssh [email protected] sudo /usr/local/bin/ha_shutdown.sh'

Put the whole thing in quotes as shown above. Then execute the shell_command:

action:
  - service: shell_command.my_script

You could also try:

shell_command:
  my_script: 'ssh [email protected] sudo shutdown -h now'

no shell script file needed any longer

sadly still no luck

still error 255

what is strange is that “” does not exist, there is nothing in the usr/src folder

Logger: homeassistant.components.shell_command
Source: /usr/src/homeassistant/homeassistant/components/shell_command/init.py:122
Integration: Shell Command (documentation, issues)
First occurred: 13:47:45 (1 occurrences)
Last logged: 13:47:45

Error running command: ssh [email protected] sudo shutdown -h now, return code: 255

NoneType: None

So it seems that the host is not accepting the ssh connection. Can you please enable debugging for shell_command in configuration.yaml, restart HA, try again and check the logs.

logger:
  default: critical #or whatever you use
  logs:
    homeassistant.components.shell_command: debug