Restart or shutdown your Proxmox node from Home Assistant automation

Restart or shutdown your Proxmox node from Home Assistant automation

There is a UPS monitoring through NUT configured in my Home Assistant. So I decided my Home Assistant could shut down my homelab servers on a low UPS battery.

We will use Proxmox VE API and Home Assistant RESTful Command integration for this.

Proxmox user and permissions

Let's do things right and don't allow Home Assistant to log in with the root user to your Proxmox environment. We will create a role first. Go to your Datacenter view in Proxmox web UI choose Permissions -> Roles and hit Create.

We will create a PowerManager role with Sys.PowerMgmt privileges. This will allow our user to execute shutdown/restart commands through Proxmox API, but nothing more.

Next, we will create a user. In Proxmox web UI go to Permissions -> Users and hit Add. Give him a username and password, and choose "Proxmox VE authentication server" as a Realm:

Next, we will go to our Datacenter view in Proxmox web UI choose Permissions and hit Add. Here, we will set /nodes/<node_name> as the Path, select our newly created User and our newly created Role for him:

Lastly, we will need an API token to make API calls. For this, we will go to Permissions -> API Tokens and click Add. Choose our newly created user, give the token an ID, and disable Privilege Separation.

After clicking "Add" we will see the token. We need to write down the token ID and token itself to use it in Home Assistant later.

Home Assistant integration

Now to the Home Assistant. We will need to edit our configuration.yaml file as currently, this is the only way to add RESTful Command integration. Here is an example:

rest_command:
  spacedock_one_shutdown:
    url: "https://192.168.99.3:8006/api2/json/nodes/spacedock-one/status"
    method: post
    headers:
      Authorization: PVEAPIToken=pwrmngr@pve!power=73892874-ad34-4b98-83e2-7be787f9bee3
    content_type: "application/x-www-form-urlencoded"
    payload: "command=shutdown"
    verify_ssl: false

The url should have the next format: https://<proxmox node IP>:8006/api2/json/nodes/<proxmox node name>/status

The authorization header should have the next format: PVEAPIToken=<proxmox username>@pve!<API token ID>=<API token>

You can use shutdown or reboot as a command.

After restarting Home Assistant you will have a new service available. In our example, it would be rest_command.spacedock_one_shutdown. Calling it will do the trick.