Xbox HA
I play video games. I started with Atari and Intellivision and now itâs xbox and even a little Oculus VR. The games have become so good it is often hard to stop playing. If we donât set limits it is easy for kids to spend too much time playing. But parents are busy, and arenât always aware of how much time their kids (or their spouse) is spending playing games. Well, once again Home Assistant provides a solution. Iâll show you how to set up Home Assistant and XboxAPI, to track how long kids are playing, get alerts sent to you when they hit certain limits, and even send them messages through the xbox when itâs time to turn it off. Sorry boysâŚ
Step one: Go to the âunofficial xboxapi.comâ at https://xboxapi.com/ and Register. Thereâs a free plan and several tiers of paid plans. The most important difference for me is the paid plan is required to let you send messages to an xbl gamertag through xbl. When my boys are playing they rarely answer a text, even from me. But, if I can send an xbl message that pops up on their screen⌠Then I know Iâve got their attention. #2 a month isnât much. Itâs totally worth it to me. Alan is a good guy and a Home Assistant enthusiast as well so Iâm happy to support him. Youâll need the Xuid for any xbl accounts you want to track or interact with. Youâll have to connect with your xbox live account, so whatever user/email & pw you use for xbl, youâll need here. You may get an error the first time. XBL is trying to protect your account and doesnât like this api website that is trying to log in for you. Alan gives you the link to follow to correct the error and verify to xbl that you are authorizing this site to log into your account. Once youâre logged in go to the documents page and you can click these links to see what info you can get. If youâre on the free plan you may go over your hourly limit if you click too many. To get your xuid click Endpoint #2 â/v2/accountXuidâ and it will give you your xuid. You may recognize the formatting. Itâs json.
To get xuidâs of your friends I used #12 â/v2/{xuid}/friendsâ. Itâll give you a bunch of info that may look intimidating. Again, itâs just json. Donât be scared. Search for the gamertag, then look for the associated âidâ, that number is that persons xuid, and thatâs what youâll need to track them (or send them messages).
So for me I grabbed the xuid for my 2 sons and myself. Thatâs all we need to do with the xboxapi for now. For the rest weâll go to our Home Assistant Configuration.yaml file.
Here, we need to create a few sensors. The first one uses the xbox_live platform and looks like this. This is where you put the api_key you got from xboxapi.com and the xuidâs you want to track.
#sensor:
– platform: xbox_live
api_key: xxxxxxxxxxxxxxxxxxxxxx
xuid:
– 25xxxxxxxxxx17 #DrZzs
– 25xxxxxxxxxx02 #Zerp
– 25xxxxxxxxxx58 #BearBait
The next thing to set up will be a âhistory_statsâ sensor, thatâll look like this:
#sensor:
– platform: history_stats
name: Zach Xbox
entity_id: sensor.zerp
state: ‘Online’
type: time
start: ‘{{ as_timestamp( now().replace(hour=0).replace(minute=0).replace(second=0) ) – now().weekday() * 86400 }}’
end: ‘{{ now() }}’
– platform: history_stats
name: Jackson Xbox
entity_id: sensor.bearbait
state: ‘Online’
type: time
start: ‘{{ as_timestamp( now().replace(hour=0).replace(minute=0).replace(second=0) ) – now().weekday() * 86400 }}’
end: ‘{{ now() }}’
– platform: history_stats
name: DrZzs Xbox
entity_id: sensor.dr_zzs
state: ‘Online’
type: time
start: ‘{{ as_timestamp( now().replace(hour=0).replace(minute=0).replace(second=0) ) – now().weekday() * 86400 }}’
end: ‘{{ now() }}’
I just used this syntax from the example in the HA docs.
https://www.home-assistant.io/components/sensor.history_stats/
Another useful bit of data you may want to collect is the History Graph. You can set the number of hours you want the graph to display and how often you want it to update (in seconds).
Itâll show the last weekâs worth of online/offline states for each xuid youâre tracking. This shows one weekâs worth of online/offline status for our 3 gamer tags. I think this is the most useful because at a glance you can see how much someone has been on over the past week. If you want to track a different time period there are other examples or you can just tinker with it until you get what you want.
history_graph:
gr1:
name: Xbox
entities:
– sensor.zerp
– sensor.bearbait
– sensor.dr_zzs
hours_to_show: 168
refresh: 3600
Now for the grand finale: Sending xbox Live messages from a HA automation!
I couldnât have done this without Ludeeus. Which reminds me. If your a Home Assistant user and you havenât joined the discord chat, you are missing out on a huge resource. And you may be able to help someone else who goes there looking for help. Itâs a great community, so check it out.
Hereâs what you need for the xbl Notification:
#notify:
– name: drzzs_xbox
platform: rest
resource: https://xboxapi.com/v2/messages
method: POST_JSON
headers:
Content-Type: application/json
X-Auth: b53xxxxxxxxxxxxxxxx4050
data:
to:
– 25xxxxxxxxxuid17
Then for an automation to warn them when theyâve been on 2 hrs you use this:
#automation:
– alias: DrZzs Xbox 2hr warning
hide_entity: true
initial_state: ‘on’
trigger:
– platform: state
entity_id: sensor.drzzs_xbox
to: ‘on’
for:
minutes: 120
action:
– service: notify.drzzs_xbox
data:
message: “You’ve been playing for 2 hours bud. Time to take a break.”
Thatâs it. Xbox live integration for Home Assistant. Mrs Zzs is always getting after the boys for being on the xbox too long. So this one got a full 5/5 WAF score. But only a 0.5 out of 5 on the KAF. Sorry boys.
Be careful with this one. When I first got it working and looked at the most recent weekâs data. I found a terrible error. It showed that I had actually been on the xbox longer than any of the kids. Dang it.
Until next time
Adios
