Return code 127 - trapped in a container?

I’m running HA on a Raspberry Pi in a IoT project where I measure sea water and air temperature with a sensor that sends data regularly to a sensor (MQTT).
I have set up sensors that shows real time values on meters and generates historical data. So far so good.
I also need the temperature value to be exported as a text file to an other server on internet where date will be further used. I have got help an ideas from this community to come to where I’m now. But still missing the very last step.

- id: '1701867996210'
  alias: Save Temperature to Local File
  description: ''
  trigger:
  - platform: state
    entity_id: sensor.vattentemperatur_1
  action:
  - service: notify.Vattentemperatur
    data_template:
      message: ' {{ states.sensor.vattentemperatur_1.state }}'
notify:
  - platform: file
    name: Vattentemperatur
    filename: /config/Vattentemperatur.txt
    timestamp: False
    
shell_command:
  upload_script: bash /config/upload.sh

upload.sh

#!/bin/bash
tail -n -1 Vattentemperatur.txt > last_temp.txt
echo "$(date +'%d/%m')" > datum.txt
sshpass -p ****** scp -P 22 last_temp.txt datum.txt [email protected]:/path/to/t2
- id: '1702997888249'
  alias: Upload script
  description: Upload to server
  trigger:
  - platform: time_pattern
    minutes: /10
  condition: []
  action:
  - service: shell_command.upload_script
    data: {}
  mode: single
  1. Automation “Save Temperature to Local File” adds value via “notify” when sensor value is changed.
  2. Vattentemperatur.txt is updated.
  3. Running upload.sh manually from terminal uploads .txt files to server.
  4. Running automation “Upload to server” does NOT work.

I have read SSH’ing from a command line sensor or shell command

“Since we’ll be running these commands from sensors or service calls, interactive SSH isn’t an option so we must go password-less (plus its a security best practice). So to start we need to generate an identity…”

I have chosen to use (yes, not so secure sshpass…) to send files to the server.

Considering this I don’t understand why I can’t send data as in the script upload.sh.
Directly from the terminal it works perfectly but in automation - no.

sshpass installed in docker homeassistant

I would be happy if someone could help me with this very last step so I could get my temperature web page to work.

From the terminal shell into the home assistant docker container (docker -exec it ) Try your command from within there.

#!/bin/bash
tail -n -1 /config/Vattentemperatur.txt > /config/last_temp.txt
echo "$(date +'%d/%m')" > /config/datum.txt
sshpass -p ****** scp -P 22 /config/last_temp.txt /config/datum.txt [email protected]:/path/to/t2
notify:
  - platform: file
    name: Vattentemperatur
    filename: /config/Vattentemperatur.txt
    timestamp: False
    
shell_command:
  upload_script: /bin/bash /config/upload.sh

That work better?

Unfortunately not:

Log Details (ERROR)

Logger: homeassistant.components.shell_command
Source: /usr/src/homeassistant/homeassistant/components/shell_command/__init__.py:122
Integration: Shell Command ([documentation](https://www.home-assistant.io/integrations/shell_command), [issues](https://github.com/home-assistant/core/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+shell_command%22))
First occurred: 21:29:53 (1 occurrences)
Last logged: 21:29:53

Error running command: `/bin/bash /config/upload.sh`, return code: 127

NoneType: None

I had to del sshpass from docker otherwise I also got return code:6

Can you be a little more detailed? I don’t follow… :anguished:

I’ve just been in to the Home Assistant container on my network.

homeassistant:# which tail
/usr/bin/tail

homeassistant:# which sshpass

homeassistant:# which scp
/usr/bin/scp

there we go - sshpass is NOT in the homeassistant container.

1 Like

Yes, that’s usually the issue there are limited items installed in the container. While you could install it there, when you rebuild or upgrade the container anything you install will need to be reinstalled.

As an alternative design consider running a script on the destination system that uses the HA REST API to pull the data.

Look at the api/states endpoint

Please explain a couple of things to me that I can’t get into my brain :face_with_spiral_eyes:

If I go into the terminal in HA and run ./upload.sh, by typing on my keyboard, the files are uploaded to the external server. Exactly as I want.

If I let the automation function run ./upload.sh it doesn’t work!

If it’s like that, does it mean that it is not possible to PUSH anything out of a HA container?

The terminal is running on the host. HA is running in a docker container on the host.

When you execute a command from the terminal that is occurring within the host. When you have HA execute a command via an automation it executes within the container.

Think of the container as a separate environment from the Host with its own set of software So in this case rhe sshpass program is installed on the host but not in the container.

To upload a file from the containers you’ll need to do one of these

A) find a utility that is installed that you can use.
B) install the utility in the container that you need
C) use a different design

This solved the uploading:

Shell command: in configuration.yaml

shell_command:
  upload_script: apk add --quiet sshpass && /bin/bash /config/upload.sh

upload_script is run in “automation”.

upload.sh

#!/bin/bash

# Set variables
REMOTE_USER="******"
REMOTE_HOST="*****"
REMOTE_PATH="*****"
REMOTE_PORT=22
PASSWORD="*****"

# Get the last line of temp_color.txt and save it to last.color_temp.txt
tail -n -1 temp_black.txt > last.black_temp.txt
tail -n -1 temp_red.txt > last.red_temp.txt
tail -n -1 temp_white.txt > last.white_temp.txt

# Save the current date in the desired format to datum.txt
echo "$(date +'%e/%-m %R')" > datum.txt

# Copy last_temp.txt and datum.txt to the remote server
sshpass -p "$PASSWORD" scp -P "$REMOTE_PORT" -o StrictHostKeyChecking=no last.black_temp.txt last.red_temp.txt last.white_temp.txt datum.txt "$REMOTE_USER"@"$REMOTE_HOST":"$REMOTE_PATH">

# Also upload to raabadarna
source ./upload_raabad.sh

Core has been updated twice and OS once. Uploading runs as desired without any interaction from me.

OK but - don’t do that.

shell_command:
  addapk: apk add --quiet sshpass
  upload_script: sshpass && /bin/bash /config/upload.sh

Use an automation on Home Assistant start to run the addapk shell command. You don’t need to be installing it every time the upload script runs. Just once at Home Assistant start.

Ok, i understand the idea. But after a core update does HA always restart?

Yes after a core update HA always restarts, and more importantly after a core update the supervisor completely rebuilds the container, and any customisations (eg packages you have added) will be lost.

1 Like