AUTOMATION - Gracefully Shutdown VM's & ESXi Host via SSH

Hi All,

Here is a simple guide on how to automatically execute commands within Home Assistant to your ESXi Host.

List of guide and links that I use to make it happen. Credit to them!

  1. Guide: Start up and shut down remote linux PC using Home Assistant

  2. ESXi keeps prompting for password after adding ssh public key to authorized_keys

  3. Home Assistant Shell Integration: Local & SSH Linux Control

For starters, below is the list of my environment setup information:

  1. ESXi Host - Intel NUC11PAHi7

  2. NAS Storage - Synology DS420+

  3. ESXi Version - ESXi-8.0.0-20513097-standard (VMware, Inc.)

  4. Home Assistant Version - Home Assistant 2023.1.7

  5. Supervisor Version - Supervisor 2023.01.1

  6. Operating System Version - Operating System 9.4

  7. Frontend Version - Frontend 20230110.0 - latest


*** Disclaimer *** 
Always make a full backup for all the related devices and system which involve in the activities.
You have been warned

ESXi Host

1. Enable SSH on ESXi

  • Select the TSM-SSH entry on the list
  • Click Actions - > Policy
  • Choose Start and stop with host, and the SSH service will activate after every host restart

2. Enable Autostart on ESXi

  • Select System
  • Click Edit Setting - > Enable (choose Yes)

Home Assistant Instance

1. Install official ‘SSH & Web Terminal’ Add-on

  • Untick the 'Protection mode’
  • Start the Add-on after installation
  • Open the Web UI

2. Creating SSH key pair

mkdir /config/ssh_keys
ssh-keygen -t rsa -b 4096 -f /config/ssh_keys/id_rsa_homeassistant
  • When a password is queried, leave it empty. If fulfilled with password the SSH connection will require both key and password and can’t be used for remote commands by home assistant
  • In above lines we first create a directory for our ssh keys, because Home Assistant is run in a docker container and does not have visibility to default /root/.ssh folder at all
  • Second line starts the key generation. The ssh key is generated with by default 2048 bits, but should be 4096 bits

3. Next we are going to copy the public key to our remote ESXi Host

cat /config/ssh_keys/ | ssh [email protected]_esxi_host_ip 'cat >>/etc/ssh/keys-root/authorized_keys'
  • Please change the ‘username’ according to user created for SSH
  • Please change the ‘remote_esxi_host_ip’ according to your ESXi Host IP’s

4. Verify that it’s working by SSH’ing into ESXi Host from Home Assistant shell

ssh -i /config/ssh_keys/id_rsa_homeassistant [email protected]_esxi_host_ip
  • If everything is working as expected, no password will be queried

5. We going to create a shell commands which is done through configuration.yaml

  turn_off_esxi: ssh -i /config/ssh_keys/id_rsa_homeassistant -o StrictHostKeyChecking=no [email protected]_esxi_host_ip "/sbin/ && /sbin/poweroff"
  • Save it, Check and Restart

6. We going to create a scripts which is done through scripts.yaml

alias: General - Turn Off ESXi
  - repeat:
      count: "3"
        - delay:
            hours: 0
            minutes: 0
            seconds: 7
            milliseconds: 0
        - service: notify.all_device
            title: ٱلسَّلَامُ عَلَيْكُمْ وَرَحْمَةُ ٱللَّٰهِ وَبَرَكَاتُهُ
            message: Power outage !!! at {{states('sensor.time')}}
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - service:
    data: {}
      entity_id: button.kenanga_shutdown
  - delay:
      hours: 0
      minutes: 1
      seconds: 0
      milliseconds: 0
  - service: shell_command.turn_off_esxi
    data: {}
mode: single
icon: mdi:laptop-account
  • FYI - ‘button.kenanga_shutdown’ is my NAS Storage

7. We going to create an automation which is done through automations.yaml

alias: General - Automate Power
description: ""
  - platform: state
      - switch.group_tasmota
    to: unavailable
      hours: 0
      minutes: 3
      seconds: 0
    id: main_power_off
condition: []
  - choose:
      - conditions:
          - condition: trigger
            id: main_power_off
          - service: script.turn_on
            data: {}
              entity_id: script.general_turn_off_esxi
mode: single
  • FYI - ‘switch.group_tasmota’ is the group of Sonoff POW R2 powered my appliance and stuff
  • FYI - My System Infrastructure was covered by UPS in-case of sudden power outage


The shell command and command line integrations are without a doubt some of the most powerful integrations available in Home Assistant. They open up a whole world of possibilities!

You should now be able to integrate remote bash scripts into Home Assistant, allowing almost unlimited control of any Linux-based remote device!

Congratulations if you made it to the end!

1 Like