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.
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
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.
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.
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
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.
@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.