Home assistant behaves differently when started with systemctl

I have the following script to send SMS messages from a USB GSM stick:

#!/bin/bash

read count < /home/homeassistant/.homeassistant/smsCounter.txt

/bin/stty -F /dev/ttyUSB0 9600 min 100 time 2 -hupcl brkint ignpar -opost -onlcr -isig -icanon -echo

/usr/sbin/chat TIMEOUT 1 "" "AT+CMGF=1" "OK" > /dev/ttyUSB0
/usr/sbin/chat TIMEOUT 1 "" "AT+CMGS=\"0987654321\"" "OK" > /dev/ttyUSB0
/usr/sbin/chat TIMEOUT 1 "" "$1" "OK" > /dev/ttyUSB0
/usr/sbin/chat TIMEOUT 1 "" "^Z" "OK" > /dev/ttyUSB0

#sleep 2

#chat TIMEOUT 1 "" "AT+CMGS=\"0123456789\"" "OK" > /dev/ttyUSB0
#chat TIMEOUT 1 "" "$1" "OK" > /dev/ttyUSB0
#chat TIMEOUT 1 "" "^Z" "OK" > /dev/ttyUSB0

let 'count++'
echo $count > /home/homeassistant/.homeassistant/smsCounter.txt

The script works fine when logged in as homeasssistant user. It also works fine when called as a shell_command from home assistant, but ONLY IF home assistant is started from the command line as follows:
sudo -u homeassistant -H /srv/homeassistant/bin/hass.

When I start home assistant with systemctl:
sudo systemctl start [email protected]
the script does get called because the smsCounter.txt file gets updated, but no SMS message is sent.

Does anyone have any idea what I am doing wrong here?

It turns out that the command I use (chat) is not suitable for this type of script. I have now a new script that works fine, using a program called “gammu”.

Here is the new version:

#!/bin/bash

read count < /home/homeassistant/.homeassistant/smsCounter.txt

gammu sendsms TEXT 012345678 -text "$1"

let 'count++'
echo $count > /home/homeassistant/.homeassistant/smsCounter.txt

Can you please mark your thread as solved?