Home Automation

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

Social Links

Menu