Automation update Home assistant core

Hello everybody. Can you create a script to update Home assistant from a button? These would be the commands that I have to execute. Thank you so much

`
sudo -u homeassistant -H -s
cd /srv/homeassistant
python3.8 -m venv .
source bin/activate
pip install Homeassistant

`

Just put this commands in a new file upgrade_ha.sh, make it executable with chmod +x upgrade_ha.sh and runt it via shell_command

Are you confident with this approach? I think you should always read the release note before upgrade with closed eyes…

Perfect. I’ll try. Thank you

These are commands to install homeassistant in a python venv.
For upgrade you only need to:

cd /srv/homeassistant
source bin/activate
pip install --upgrade homeassistant

Not needed:
as the script alraedy runs as the homeassistant user when started from a shell_command

sudo -u homeassistant -H -s

and as the venv is already created

python3.8 -m venv .

I tried various things but I keep getting this error

Logger: homeassistant.components.shell_command
Source: /srv/homeassistant/lib/python3.8/site-packages/homeassistant/components/shell_command/__init__.py:114
Integration: Shell Command (documentation, issues)
First occurred: 20:31:11 (1 occurrences)
Last logged: 20:31:11

Error running command: `chmod +x /home/homeassistant/.homeassistant/script/upgrade_ha.sh`, return code: 1
NoneType: None

I don’t know if it’s a permission problem.
To be safe apply all 777 permissions and configure pi: homeassistant as user and group

Change the folder user to homeassistant: homeassistant and now there is no error but it does not update and does not restart either.

the script

cd /srv/homeassistant
source bin/activate
pip install --upgrade homeassistant
/usr/bin/sudo /bin/systemctl restart home-assistant@homeassistant

Hello. I still can’t run the script correctly

Error running command: `/usr/bin/sudo sh /home/homeassistant/.homeassistant/script/upgrade_ha.sh`, return code: 1

It tells me this error.
But still I can’t run the bash properly.
When it is executing the bash it says source not found

/usr/bin/sudo /bin/systemctl stop home-assistant@homeassistant
cd /srv/homeassistant
source bin/activate
/srv/homeassistant/bin/python3.8 -m pip install --upgrade pip
/usr/bin/sudo /bin/systemctl restart home-assistant@homeassistant

Try it with:

. bin/activate

OR
add the bash shebang on top of your script

#!/bin/bash
...

make it executable with

chmod a+x /home/homeassistant/.homeassistant/script/upgrade_ha.sh

and remove

/usr/bin/sudo sh 

from your HA shell_command

BTW: If the script is called from HA, i don’t think it’s a good idea to stop HA in the script. Not sure, but i think running scripts are canceled when HA stops.

And like dapuzz said:

If something went wrong with the update, your HA will not restart.
I always update my instance manualy in a terminal to see the logs.

From what I understand this folder should be after home assistant is working.
So put the script in this folder. If I run it by console it works but I have to execute it like this: sh upgrade_ha.sh

Console (here script executable)

(homeassistant) homeassistant@raspberrypi:/srv/homeassistant $

Script

#!/bin/bash
cd /srv/homeassistant
. bin/activate
pip3 install --upgrade homeassistant
shell_command: upgrade_ha.sh

and

shell_command: upgrade_ha.sh

Error

Logger: homeassistant.components.shell_command
Source: /srv/homeassistant/lib/python3.8/site-packages/homeassistant/components/shell_command/__init__.py:114
Integration: Shell Command (documentation, issues)
First occurred: 15:02:05 (1 occurrences)
Last logged: 15:02:05

Error running command: `upgrade_ha.sh`, return code: 127
NoneType: None

Works.
The problem was some lines of the bash.
I started with the empty bash file to rule out the problem and started adding de to a command.

Bash File

#!/bin/bash
cd /srv/homeassistant
. bin/activate
pip3 install --upgrade homeassistant

ShellCommand

ha_update: /home/homeassistant/.homeassistant/script/upgrade_ha.sh

@dapuzz @VDRainer thank you very much

1 Like

You don’t need to source activate if you just run pip3 with the full path. Try to use full paths whenever using a script for safety/security. You can likely just use the following as a shellcommand

/srv/homeassistant/bin/pip3 install --upgrade homeassistant

ok. Thanks friend