Esphome CLI from HA

The addon does not have a CLI, if you want that you will have to install it on your PC:

https://esphome.io/guides/installing_esphome.html

1 Like

Thanks! Still learning ESPHome, this looks like it’ll be handy to have.

Would this help setting up new boards, instead of going through the browser, which now requires SSL?

Yes it is an amazing project.

Requires SSL?

I’m using the addon without SSL.

Sorry to contradict you @tom_l , but you can enter the esphome container and run the commandline.

docker exec -it addon_15ef4d2f_esphome-dev bash
cd config/esphome
esphome logs test.yaml
3 Likes

Well there you go. That’s two new things I’ve learn’t in as many days. Does it require turning off protection mode in the SSH addon?

1 Like

I use web terminal & ssh and yes, the docker command does require protection mode off.

1 Like

I had to use:

docker exec -it addon_a0d7b954_esphome bash

I am a noob at Docker. Not sure this is identical throughout all HA installations.
If not, a list of Docker containers can be retrieved through:

docker container ls -a

The difference is that I am using the esphome-dev addon, it has a different docker id/name to the usual esphome addon. But you got there :slight_smile:

Hint - in most linux shells, the tab key will auto complete. IE type docker exec -it add<tab> you will be presented with the options to complete the line. You may need to hit <tab> twice. It is repeatable when you have typed a few more characters. (It is hard to explain until you actually do it).

1 Like

Thank @nickrout. I was aware of tab auto complete but nice explanation.
Now I am trying to compile / upload in a batch from the CLI.
More difficult than expected. Will see…

One problem is that…

esphome compile xyz.yaml

… does not compile into the same path as the HA addon does. Instead it uses a sub-path .pioenvs within that add-on path. So all downloads and compilations are run again even if they were already done from the web frontend. Any ideas how I can change that path?

1 Like

I opened an issue report on that problem (see below).
In that ticket @jesserockz comments with a solution using export ESPHOME_IS_HA_ADDON=true.
Unfortunately that does not completely solve the problem. But it is an improvement. See the ticket for details.

For those who are interested:

Here is an example of how I batch compile from a terminal connected to Home Assistant with the ESPHome add-on installed. (You also need the add-on SSH & Web Terminal installed so you can connect with a terminal from your PC/Mac/*nix.)

docker exec -it addon_5c53de3b_esphome bash
cd config/esphome
export ESPHOME_IS_HA_ADDON=true
for config in smoke-*.yaml; do echo "----- COMPILE ----- $config -----"; if esphome compile $config; then echo "----- UPLOAD ----- $config -----"; esphome upload $config --device ${config%.*}.local; fi; done

This example will batch compile all YAML files with names that start with “smoke-”.

1 Like

I receive a ‘command not found’ when trying to do so…

Tell us exactly what you did? Which command was not found?

I am using two scripts to compile and do other things for my esphomes, stored in homeassistant config/esphome directory.

First script is for single esphome entity, save this script as makesingle

type ./makesingle to see instructions on usage

#! /bin/bash

if [ "$HOSTNAME" != 5c53de3b-esphome ]; then

SCRIPT=$(realpath "$0")
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
SCRIPT_DIR="${SCRIPT_DIR/"/root"/""}"
echo $SCRIPT_DIR

COMMANDS="compile upload clean config logs run"

 if ! [[ " $COMMANDS " =~ .*\ $1\ .* && $# -eq 2 ]]; then
  echo
  echo "Usage:"
  for COMMAND in $COMMANDS
  do
   echo "$0 $COMMAND <filename.yaml>"
  done
  echo
  echo "Example:"
  echo "$0 "${COMMANDS%% *}" <filename.yaml>"
  echo
  exit 0
 fi
 if ! [[ -f $2 ]]; then
  echo "File not found $2"
  exit 0
 fi
 docker exec addon_5c53de3b_esphome /bin/sh $SCRIPT $1 $SCRIPT_DIR/$2
else
 export ESPHOME_IS_HA_ADDON=true
 pio_cache_base=/data/cache/platformio
 export PLATFORMIO_PLATFORMS_DIR="${pio_cache_base}/platforms"
 export PLATFORMIO_PACKAGES_DIR="${pio_cache_base}/packages"
 export PLATFORMIO_CACHE_DIR="${pio_cache_base}/cache"
 export PLATFORMIO_GLOBALLIB_DIR=/piolibs

 esphome $1 $2
fi

# For interactive session to esphome
# docker exec -it addon_5c53de3b_esphome bash

Second script name is makeall. This is useful when I want to compile or upload all my esphomes in one go. You need to edit this script with your esphome.yaml names inside.

#! /bin/bash
SCRIPT=$(realpath "$0")

COMMANDS="compile upload clean config"

if [ "$HOSTNAME" != 5c53de3b-esphome ]; then
 if ! [[ " $COMMANDS " =~ .*\ $1\ .* ]]; then
  echo
  echo "Usage:"
  for COMMAND in $COMMANDS
  do
   echo "$0 $COMMAND"
  done
  echo
  echo "Example:"
  echo "$0 "${COMMANDS%% *}""
  echo
  exit 0
 fi
 docker exec addon_5c53de3b_esphome /bin/sh $SCRIPT $1
else
 export ESPHOME_IS_HA_ADDON=true
 pio_cache_base=/data/cache/platformio
 export PLATFORMIO_PLATFORMS_DIR="${pio_cache_base}/platforms"
 export PLATFORMIO_PACKAGES_DIR="${pio_cache_base}/packages"
 export PLATFORMIO_CACHE_DIR="${pio_cache_base}/cache"
 export PLATFORMIO_GLOBALLIB_DIR=/piolibs

 esphome $1 /config/esphome/bedroom-ac.yaml
 esphome $1 /config/esphome/livingroom-ac.yaml
 esphome $1 /config/esphome/sonoff-tv-2.yaml
 esphome $1 /config/esphome/sonoff-tv-3.yaml
fi

# For interactive session to esphome
# docker exec -it addon_5c53de3b_esphome bash
2 Likes

Docker is the command that is not being found.

You need to be using advanced ssh and web terminal and turn off protection mode.

1 Like

Has anybody here gotten any idea where the compiled firmware files go? Can I find them somewhere in the file system of HA or the ESPHome docker container?

You can find compiled bin files here:

docker exec -it addon_5c53de3b_esphome bash
cd /data/build

each project has own folder, for example inverter project binaries are here:

/data/build/inverter/.pioenvs/inverter#
1 Like

Might have found them:

docker exec -it addon_5c53de3b_esphome bash
cd /data/NODENAME/.pioenvs/NODENAME/

(Replace NODENAME with your ESPHome node name.)

Update:
The path in the previous posting is correct. Mine here is wrong. Please ignore!

Thanks @hpi, seems that we posted both at the same time.
But your answer is correct. The discrepancy in the paths in my answer was the result of the parameter build_path in my ESPHome YAMLs. It was set to the same name as the node name, which made sense long time ago. I have removed it now.
Thanks again.

Maybe some of you pros might also be interested in having a look at the following discussion that I started. It is somewhat related and I am clearly approaching the limits of my Linux knowledge over there.