Minecraft Server Sensor

I’ve also realized another big issue. @LookItsOps, the ‘User Online’ attribute has a space in it. It makes it incredibly hard to script with the space.

I made some edits to the file to give it more proper numeric values for automations. Caution : I took out the Ping though since it was triggering state changes too much.

https://github.com/CCOSTAN/Home-AssistantConfig/blob/master/custom_components/sensor/minecraft.py

1 Like

Why no submit this as a proper component?

I get problems setting up platform (server socket timeout) with both the modified versions in this thread from @CCOSTAN and @LookItsOps - the original one by @GreenTurtwig is working fine, but obviously only shows the player count.

Any ideas why I get server socket timeout with the latter two, anybody?

Beyond my skill set…

FWIW, this component is incompatible with HASS 0.70.0. See https://github.com/home-assistant/home-assistant/issues/14705

Hi, in 0.92 there’s some changes needed.

As always you need to place the files in a subfolder in custom_components called something unique.
I have called the one on my server custom_components/minecraft/

You also need to change the minecraft.py filename to sensor.py
And you need a manifest.json file.

Here is my branch from Github user Jopsahl:

Thanks for the update Ole :grinning:

I have this up and running perfectly (thank you so much) but i am struggling to be able to setup a automation when someone joins ? please advise/point me in the right direction.

@hardys How did you get it to work? Mine is throwing errors. https://hastebin.com/ibatuzafew.sql

EDIT: I’ve tried to ad a query.port in the minecraft config. Tried both internal and external IP. Tried with having port added like IP:port. But I get the same result everytime. Port is open in my router as well.

EDIT2: So I tried one of them web based services to check if it could get information from query on my server and I can. IP should be IP:port and that’s the port on your server as what you login with and not the query port. I still have problems with the component though. This time I get socket.timeout: timed out. https://hastebin.com/zakozuguya.sql

Hi filikun

Apparently, I can’t read, because I just asked if query was set to true in your server.properties file and of course it is as you’ve clearly stated you’re able to read the information from a query tool server.

So let’s try something else.

In my sensors configuration I’ve got two minecraft servers running at the moment.
Now, I’ve separated my config files, so right now I’ve got a file
called ~/.homeassistant/sensors/minecraft_vanilla.yaml

Quoth its content:

- platform: minecraft
  name: Vanilla Server
  server: minecraft.domain.com

That’s it. No ports used. How’s your config looking?

EDIT:
I just turned off my server and got this error:

ERROR (MainThread) [homeassistant.components.sensor] Error while setting up platform minecraft
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/helpers/entity_platform.py", line 150, in _async
    await asyncio.wait_for(asyncio.shield(task), SLOW_SETUP_MAX_WAIT)
  File "/usr/lib/python3.6/asyncio/tasks.py", line 358, in wait_for
    return fut.result()
  File "/usr/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/home/homeassistant/.homeassistant/custom_components/minecraft/sensor.py", line 31, in setup_platform
    MCServerSensor(server, name, mcserver)
  File "/home/homeassistant/.homeassistant/custom_components/minecraft/sensor.py", line 44, in __init__
    self.update()
  File "/home/homeassistant/.homeassistant/custom_components/minecraft/sensor.py", line 59, in update
    status = self._mcserver.lookup(self._server).status()
  File "/srv/homeassistant/lib/python3.6/site-packages/mcstatus/server.py", line 49, in status
    connection = TCPSocketConnection((self.host, self.port))
  File "/srv/homeassistant/lib/python3.6/site-packages/mcstatus/protocol/connection.py", line 129, in __init__
    self.socket = socket.create_connection(addr, timeout=timeout)
  File "/usr/lib/python3.6/socket.py", line 724, in create_connection
    raise err
  File "/usr/lib/python3.6/socket.py", line 713, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

That indicates theres no connection and looks very close to your first error.
Do you have a firewall that blocks the connections?

According to:

There was a problem with the server IP not being set in server.properties.
How does your minecraft config look?

Sincerely me

@ole.noerby Hey! Thanks for replying! So mine looks like this /config/sensor.yaml

platform: minecraft name: Filip's Minecraft-server server: 192.168.1.3

And I have tried with my public IP as well. And yeah the ports are open and accessible and no firewall what I’m aware of.

It might be that my server.properties has the wrong IP as it says the local IP 192.168.1.3. I have a public dynamic IP though so if I need to use that instead I must change it on both Hassio and Minecraft whenever it changes.

EDIT: I’m not sure why my formatting is all wrong here but it looks just like yours… I promise :smile:

EDIT2: Here’s the server.properties file as well https://hastebin.com/mopisijape.bash and I’m using AMP and the McMyAdmin to host it on my HPE Microserver gen8. I feel that AMP and McMyAdmin is confusing. It overwrites my server.properties all the time but only certain parts, not query and stuff. I might try something different there as well. How do you host?

No problems!
Props for using a Gen8 - great machine!

I immediately notice you have changed your query.port to 25566
Is there a reason for this?

Mine is hosted on a rented bare-metal Ubuntu server running a bunch of things like Apache, Teamspeak, Transmission and a few other things.

The minecraft is running in a screen and a java command.
Actually, since mine are mostly FTB they can run on the old version of Minecraft Remote Toolkit, (https://www.spigotmc.org/resources/minecraft-remote-toolkit-for-1-10-1-11-x.29777)

I’m using Cloudflare with SRV-records so people can connect to my servers using only a FQDN and no ports, meaning I’ve got multiple servers on multiple ports but the clients only connect using minecraft.domain.com even though the ports are actually 25575 and 25585.

Anyway - since you’re using an apostrophe in your name, that might be an issue - try removing that for testing.

I have only used McMyAdmin as a trial, but I’d say it MIGHT be the problem.
And get back on me regarding that query port.

EDIT: NVM with the apostrophe. It works perfectly for me with one.

So I tried adding my public URL to the server.properties but I did not get it to work there either. The port is set by AMP and for some reason it’s not the same as Minecraft originaly uses. Do you know any other Minecraft hosting software I can try? I’m quite new to all this :slight_smile: Or can it be that the Ubuntu server somehow blocks the port? Should not if people can join the server right?

It should not. It’s possible - but it should not.

Also, because you’re on a LAN between the Home Assistant and the Minecraft server, it’s not your firewall/router, so were not gonna look further into that bit.

You can try using putty or a similar telnet client to connect to the server 192.168.X.X port 25566 and see if there’s response.

getting entity not available error, any ideas or can you point me in the right direction ?

Update for sensor.finleys_modded_creative_server fails
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 261, in async_update_ha_state
    await self.async_device_update()
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 441, in async_device_update
    await self.hass.async_add_executor_job(self.update)
  File "/usr/local/lib/python3.7/concurrent/futures/thread.py", line 57, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/config/custom_components/minecraft/sensor.py", line 55, in update
    status = self._mcserver.lookup(self._server).status()
  File "/usr/local/lib/python3.7/site-packages/mcstatus/server.py", line 49, in status
    connection = TCPSocketConnection((self.host, self.port))
  File "/usr/local/lib/python3.7/site-packages/mcstatus/protocol/connection.py", line 129, in __init__
    self.socket = socket.create_connection(addr, timeout=timeout)
  File "/usr/local/lib/python3.7/socket.py", line 727, in create_connection
    raise err
  File "/usr/local/lib/python3.7/socket.py", line 716, in create_connection
    sock.connect(sa)
socket.timeout: timed out

Any ideas ?

From the socket timeout error, there is some sort of issue communicating with your server.

Some basic troubleshooting stuff but might help

Is the Minecraft Server on the same network? Make sure that the port is open.
If you aren’t sure try using telnet to connect to the Minecraft port from your HA box, it should give you a “Connected” message, if it can’t reach it then there’s a networking issue that needs to be resolved.

Also there’s been a few forks that haven’t been pulled back into the main repository.
https://github.com/olenoerby/MinecraftServer-HA worked for me with the most recent version.

Maybe double heck your minecraft server config that query=true

When I enable this in configuration.yaml and restart my Hass.io on Pi (0.103.4), it hangs and never finishes booting. I can’t tell what’s wrong, but I’m also fairly new to HA. Is there a startup log that I can look at to see what it doesn’t like and see if I can fix it?

  • edit - I can still ssh and samba to it, it just won’t load the HA interface. But I don’t know what log to look at.

  • edit - SSH’d in, saw the command “hassio homeassistant logs” and saw that the fork I was using was really messed up. Changed back to the one right above this post, and am now getting the socket timeout errors from a previous post. Status checkers like https://mcsrvstat.us/ show that it’s up and shows me all of its stats… Troubleshooting that, will update…