57 lines
2.9 KiB
Markdown
57 lines
2.9 KiB
Markdown
---
|
|
slug: restart-or-shutdown-your-proxmox-node-from-home-assistant-automation
|
|
title: "Restart or shutdown your Proxmox node from Home Assistant automation"
|
|
tags: [self-hosting, homelab, Proxmox, Smart Home, Home Assistant]
|
|
image: /img/blog/2023/12/power_switch.jpg
|
|
---
|
|
There is a [UPS monitoring through NUT](/2023-12-19-monitor-usb-ups-connected-to-synology-nas-in-home-assistant/index.md) configured in my Home Assistant. So I decided my Home Assistant could shut down my homelab servers on a low UPS battery.
|
|
|
|
<!-- truncate -->
|
|
|
|
We will use [Proxmox VE API](https://pve.proxmox.com/wiki/Proxmox_VE_API) and Home Assistant [RESTful Command](https://www.home-assistant.io/integrations/rest_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:
|
|
|
|
```yaml
|
|
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. |