SSH & Web Terminal - using alias command

I’m trying to get an alias to stick when the SSH & Web Terminal add-on is started. I have it added to the configuration (screenshot below) but when I login I can’t use it.

It appears to run the init_commands fine (no log errors) when the add-on starts, but I does not work. Am I missing something simple here or have I understood it wrong?

If you run that in a shell script does it work OK? Have you tried putting it into a shell script and just calling the script directly instead of coding everything into the config?

Personally I tend to stay away from shell interpreters of any kind and just write the shell script myself and simplify any need to interpret what I type because you never know if the slightest format difference breaks the interpretation (like YAML for instance).

Haven’t tried that route yet. That was going to be plan B. Perhaps now would be a good time to do so. :slight_smile:

Once you start on the shell scripting in this way you really open up a whole new world of HA opportunities. I have shell scripts to rotate my HA logs, stop my HA server to get a clean backup of my database, operate my entire, and somewhat complex, TTS system, cause my computers to do a number of things without me having to do it - the list goes on. But, what really makes it nice, is that when you use a script it is recyclable, you can use that same script in other automations where if you lock it down to just the YAML integration it’s only for that and nothing more.

Baby steps… I already have a full time job. :rofl:

1 Like

It’ll take as much or less time to do what you did in YAML in a script - so no more time wasted :slight_smile:.

Easy guide:

vi setalias.sh
your code...
!wq to save it
chmod 0755 setalias.sh (to make it runnable)

Then change your YAML to execute that script. Assuming you created it in your home folder on the HA computer:

~/setalias.sh

And done.

Thanks! I appreciate that. I wouldn’t have thought to use chmod, so thanks for that too! :+1:

Yea, that’s a step a lot of folks forget to do!

Okay. Two things leaned:

(1) I discovered that when the SSH & Web Terminal addon restarts it clears whatever extra files are in the ~/ directory.

(2) so I recreated the same script setalias.sh in ~/addons/ and put this in the addon’s config file and it still does the same thing where I can’t get the alias to run.

Screenshots for sanity

Config file text
image

Script
image

Alias command tried
image

Yea, the “~” is shortcut for home and may have been a bad suggestion (force of habit, I use the home folder a lot). All of my shell scripts are in a subfolder off config (I created one called “bash” for this purpose), so your command would be

/config/bash/setalias.sh

And run it by hand after you create it to make sure the command is legit by changing to the location of your script and just running:

./setalias.sh

If it errors there it’s going to error in your YAML too, they are using the same shell.

If you typed it correctly it will work, I just tested it in a shell terminal on my HA:

addons  backup  config  share   ssl
~ $ alias lg='tail -f /config/home-assistant.log'
~ $ lg
2021-05-13 14:45:05 INFO (MainThread) [homeassistant.helpers.script.driveway_camera_mailbox] Driveway Camera Mailbox: Running template script
2021-05-13 14:45:05 INFO (MainThread) [homeassistant.helpers.script.driveway_camera_mailbox] Driveway Camera Mailbox: Executing step call service
2021-05-13 14:45:05 INFO (MainThread) [homeassistant.components.script.security_sweep_driveway] Driveway Security Sweep: Executing step delay 0:00:05
2021-05-13 14:45:10 INFO (MainThread) [homeassistant.components.script.security_sweep_driveway] Driveway Security Sweep: Executing step call service
2021-05-13 14:45:10 INFO (MainThread) [homeassistant.helpers.script.driveway_camera_tufts] Driveway Camera Tufts: Running template script
2021-05-13 14:45:10 INFO (MainThread) [homeassistant.helpers.script.driveway_camera_tufts] Driveway Camera Tufts: Executing step call service
2021-05-13 14:45:10 INFO (MainThread) [homeassistant.components.script.security_sweep_driveway] Driveway Security Sweep: Executing step delay 0:00:05
2021-05-13 14:45:15 INFO (MainThread) [homeassistant.components.script.security_sweep_driveway] Driveway Security Sweep: Executing step call service
2021-05-13 14:45:15 INFO (MainThread) [homeassistant.helpers.script.driveway_camera_default] Driveway Camera Default: Running template script
2021-05-13 14:45:15 INFO (MainThread) [homeassistant.helpers.script.driveway_camera_default] Driveway Camera Default: Executing step call service
^C
~ $ 

The reason lg didn’t work is because it probably couldn’t find your shell script. Do what I posted above to put your script in config/bash and it’ll run so long as you chmod it.

Sorry or the delay. Thanks for the help. I’m still running into issues. Before I go any further, you are using the same add-on that I have, which is this one, right?
image

Just making sure since there are two of them. The shell I’m using from this add-on is zsh, if it matters.

When I try the steps you listed from above, this is what I get:
image

image

But if I manually type it out like you did, it works, but that defeats purpose.
image

Not sure what to tell you, it works for me. It doesn’t matter which SSH you use, its all ZSH in the end.

config $ echo "alias lg='tail -f /config/home-assistant.log'" > setalias.sh
config $ chmod 0755 setalias.sh
config $ ./setalias.sh
config $ lg
2021-05-14 14:00:43 INFO (MainThread) [homeassistant.components.mqtt.discovery] Component has already been discovered: sensor room-assistant garage-status-cluster-size, sending update
2021-05-14 14:00:43 INFO (MainThread) [homeassistant.components.mqtt.mixins] Got update for entity with hash: ('sensor', 'room-assistant garage-status-cluster-size') '{'payload_available': 'online', 'payload_not_available': 'offline', 'name': 'garage Cluster Size', 'device': {'sw_version': '2.16.1', 'identifiers': '0000000017023470', 'name': 'garage', 'model': 'Raspberry Pi Zero W Rev 1.1', 'manufacturer': 'Raspberry Pi Foundation'}, 'unique_id': 'room-assistant-garage-status-cluster-size', 'state_topic': 'room-assistant/sensor/garage-status-cluster-size/state', 'json_attributes_topic': 'room-assistant/sensor/garage-status-cluster-size/attributes', 'availability_topic': 'room-assistant/sensor/garage-status-cluster-size/status', 'icon': 'mdi:server', 'unit_of_measurement': 'instances', 'platform': 'mqtt'}'
2021-05-14 14:00:43 INFO (MainThread) [homeassistant.components.mqtt.mixins] Ignoring unchanged update for: sensor.garage_cluster_size
2021-05-14 14:00:43 INFO (MainThread) [homeassistant.components.mqtt.discovery] Component has already been discovered: sensor room-assistant family-room-status-cluster-leader, sending update
2021-05-14 14:00:43 INFO (MainThread) [homeassistant.components.mqtt.mixins] Got update for entity with hash: ('sensor', 'room-assistant family-room-status-cluster-leader') '{'payload_available': 'online', 'payload_not_available': 'offline', 'name': 'family_room Cluster Leader', 'device': {'sw_version': '2.16.1', 'identifiers': '00000000fe22e69f', 'name': 'family_room', 'model': 'Raspberry Pi Zero W Rev 1.1', 'manufacturer': 'Raspberry Pi Foundation'}, 'unique_id': 'room-assistant-family-room-status-cluster-leader', 'state_topic': 'room-assistant/sensor/family-room-status-cluster-leader/state', 'json_attributes_topic': 'room-assistant/sensor/family-room-status-cluster-leader/attributes', 'availability_topic': 'room-assistant/sensor/family-room-status-cluster-leader/status', 'icon': 'mdi:account-group', 'platform': 'mqtt'}'
2021-05-14 14:00:43 INFO (MainThread) [homeassistant.components.mqtt.mixins] Ignoring unchanged update for: sensor.family_room_cluster_leader
2021-05-14 14:00:44 INFO (MainThread) [homeassistant.components.mqtt.discovery] Component has already been discovered: sensor room-assistant garage-status-cluster-leader, sending update
2021-05-14 14:00:44 INFO (MainThread) [homeassistant.components.mqtt.mixins] Got update for entity with hash: ('sensor', 'room-assistant garage-status-cluster-leader') '{'payload_available': 'online', 'payload_not_available': 'offline', 'name': 'garage Cluster Leader', 'device': {'sw_version': '2.16.1', 'identifiers': '0000000017023470', 'name': 'garage', 'model': 'Raspberry Pi Zero W Rev 1.1', 'manufacturer': 'Raspberry Pi Foundation'}, 'unique_id': 'room-assistant-garage-status-cluster-leader', 'state_topic': 'room-assistant/sensor/garage-status-cluster-leader/state', 'json_attributes_topic': 'room-assistant/sensor/garage-status-cluster-leader/attributes', 'availability_topic': 'room-assistant/sensor/garage-status-cluster-leader/status', 'icon': 'mdi:account-group', 'platform': 'mqtt'}'
2021-05-14 14:00:44 INFO (MainThread) [homeassistant.components.mqtt.mixins] Ignoring unchanged update for: sensor.garage_cluster_leader
config $

Okay, thanks. I’ll get there in the end. Appreciate the help.

@ cbhiii I am playing around with the setting of aliases as well and came across this thread.

IMHO the reason it does not work for you, is, that you need to ‘source’ the script to keep the alias active once you exit the setalias.sh script, so, instead of running the script directly

./setalias.sh

you need to source the script, like

source ./setalias.sh

If you run the script directly, the alias will only be valid as long as the script is running, allowing you to use the alias inside the script.

If you source the script, the alias will still be valid after the script terminated.

Give it a try (if you have not already solved your issue :wink: )

Btw: Unfortunately, using the ‘source’ option as part of the init_command does not work (like your alias command did not produce the result you expected), so you still have to call the setalias script manually.