How to access the hostname of the machine running Home Assistant?

How can I access the hostname of the machine running Home Assistant from within HA? I have a dashboard showing the status of all the computers on my network, so I’d like to have my HA host provide the hostname via MQTT. Ideally, I’d like to access the hostname through the HA Node-RED installation.

I wasn’t able to find any kind of built-in support for this (nothing in System Monitor, for example), so I tried using a Node-RED exec node to execute the “hostname” command; unfortunately the result is just the name of the Docker container, which isn’t terribly helpful in this situation.

I tried posting this in the HA forums (twice, even), but haven't gotten any responses. I know the information is there -- a recent addition to the Supervisor allows changing the hostname from the UI.

Any help or guidance would be appreciated.

You cannot access the system from within the docker container. You can specify docker run --hostname myhostname when creating the container, after which you can use the exec node with hostname within the container.

Normally i would say you should locate your settings.js file and add to the functionGlobalContext to the os module.
After this you can use it in a function node.
But not sure if this works inside docker.

-> its only giving the container hostname. Not the one from the underlaying os.
But to complete this snippet here-> you have to locate in hassio the /config/node-red/settings.js file and search for

functionGlobalContext: {
    // jfive:require("johnny-five"),
    // j5board:require("johnny-five").Board({repl:false})

just comment in the first line with os and restart the container.
then you could drop a function node and write

var os=global.get('os')
msg.payload = os.hostname()
return msg

Thanks for the responses. Getting the hostname is no problem at all in NR without Docker: just use an exec node and run the hostname command. And I know that Docker is designed not to provide access to its host system; this is why I I'm hoping there is a mechanism provided by Home Asst that would expose the information. It's looking like that might not be the case. SOMEBODY out there knows 100% for sure (even if the answer is no), since HA only just started providing facilities for changing the hostname from within the HA user interface. :slight_smile:

I'm not quite sure how to go about doing that, but I will give it a try. Thanks!

I have fixed this with s little ugly hack: If you are running the HA container on a Linux system, mount /etc as a read only volume as /hostetc, Now you can access the file /hostetc/hostname within the container.

Good luck

See my reply - set the hostname of the container and then you can request it.

I did see that, @bakman2 , and thanks for the tip -- I can see that that will work. In a practical sense, I don't really have full access to Home Assistant's containers, and creating a new one would take more time and computational resources than I'm willing to put in for such a small bit of information (I'm not particularly facile with Docker). Mostly right now I want to know if HA exposes that information somewhere...if not, I'll stop looking :smiley:

For the time being, my workaround is just to hard-code the hostname and graft it onto all outgoing MQTT requests -- took all of two minutes to put into place. That will tide me over until there's a more "official" solution.

I'm running HA as HassOS on a dedicated Raspberry Pi, not as a separate container on an existing system. HA doesn't really want you messing with the main containers, which is where I would need to have access to the hostname.

in the sources of hassio they make an api call to show the hostname info from the supervisor tab.

Maybe you could make an API call with one of the hassio nodes or with a simple http-request node.

1 Like

@H3llsing -- Amazing....I wouldn't have been able to hone in on that in a hundred years...thanks so much! This is definitely more of the approach that I'm looking for, since it exposes the information from within Home Asst. I'll see what I can dig up now.

Hello steve2020
For my part, I had solved this problem in this way. Maybe you are interested.
Well it's to get the IP address of the host, but same idea to get the name of the host

@steve2020 is it working like you were expecting ?

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.