I recently found myself in need of a sensor that displays the hostname of the system running Home Assistant. This was for a test system I’m setting up, and I wanted a clear way to differentiate it from my production system. After some research, I stumbled upon a feature request and a few older posts, but none of the solutions quite fit my needs. So, I decided to tackle the problem head-on.
Initially, I tried using a command_line sensor, but I quickly realized that commands working in the SSH addon don’t always work the same way in the Home Assistant container. This led me to explore the Supervisor API, which turned out to be the key to solving the problem. The Supervisor API provides a /info endpoint that includes the hostname, which can be accessed using the SUPERVISOR_TOKEN system variable.
Here are two methods I found to create a hostname sensor using the Supervisor API:
yaml
Method 1: Using command_line
sensor:
- name: Hostname
command: >
curl -s -H “Authorization: Bearer $SUPERVISOR_TOKEN” -H “Content-Type: application/json” http://supervisor/info | jq -r .data.hostname.data.hostname
scan_interval: 3600
unique_id: supervisor_hostname
Method 2: Using rest
rest:
- resource: http://supervisor/info
method: GET
headers:
Authorization: !env_var SUPERVISOR_TOKEN
Content-Type: application/json
scan_interval: 3600
sensor:
- name: Hostname_rest
value_template: “{{ value_json.data.hostname }}”
unique_id: supervisor_hostname_rest
Both methods work seamlessly and provide a clean solution. I’ve tested these configurations across multiple backups and restores, and they consistently update the hostname sensor accurately. This approach not only solves my immediate need but also opens up possibilities for leveraging other Supervisor API endpoints for future projects.
If you’ve been looking for a reliable way to include system-specific information in your automations or dashboards, I highly recommend exploring the Supervisor API. It’s a powerful tool that can greatly enhance the functionality of your Home Assistant setup!