Detecting when an Apple TV 4th generation has woken from sleep
Asked Answered
B

3

8

I'm working on some home automation programs and one of the things I want to be able to do is detect when my 4th generation Apple TV has woken from sleep. This will generally only ever happen when someone pressed a button on its Siri remote to wake it up.

I have a PC (connected to the same TV as the Apple TV) that has a Pulse-Eight USB-CEC adapter, so naturally the first thing I tried was using CEC to determine when the Apple TV is awake. Unfortunately it's not reliable, since monitoring the Apple TV's power status to see when it wakes up produces false positives. (I should note that I do not have "Control TVs and Receivers" enabled on the Apple TV, and can't turn it on for the particular project I'm working on because I need the Apple TV to not change the TV's input.)

I'm trying to think of some other way to do this. I'm open to any possibilities, including things like:

  • Making use of private APIs on the Apple TV
  • Running an 'always on' program in the background of the Apple TV that sends a signal when the Apple TV wakes up, if that's even possible. (I suspect that it isn't.)
  • Monitoring the bluetooth communication between the Siri Remote and the Apple TV, if that's possible
  • Somehow filtering HDMI-CEC commands so that I can turn on 'Control TVs and Receivers', allow the Apple TV's CEC commands for turning on and off the TV, and exclude commands for changing the TV's input.
  • Any other method, no matter how hacky or ridiculous, as long as it works!

Does anyone have any suggestions? I'm running out of things to try!

Breena answered 27/2, 2016 at 21:43 Comment(14)
I think you're looking at an app that provides a network server, probably published via Bonjour.Kenric
@Kenric Can you be more specific? Would this app run on the Apple TV, or run somewhere else and have the Apple TV connect to it? If the latter, how could I get the Apple TV to be always connected when it's awake?Breena
I don't know precisely; I'm really just guessing, but my feeling is an app providing a network service on the Apple TV.Kenric
Not sure if this will fit what you are looking for but you can have an app on your intranet that queries the apple TV (UDP broadcast so Apple TV returns a response if it is alive) This will work with ANY apple TV (not just the 4th generation) but you will need a process running all the time somewhere else on the network.Rodrick
@Rodrick I've been trying something like that. I turns out I do have a process that's always running and checking the Apple TV. When it's asleep it shuts down its ethernet port so I can see if it's awake 98% of the time just by pinging it. However 2% of the time its ethernet port wakes up for some other reason and I get a false positive. Not sure if there's anything I can send to it that will only get a response if it's awake.Breena
I've used bonjour discover services very succesfully for this kind of tasks. I node js packages for this and worked very well. Search for node.js bonjour discovery to get you started. It will be MUCH better than what you are doing now.Rodrick
@Rodrick Can you tell me what services you're checking for? So far using Bonjour Browser I only see two services from my Apple TV: _appletv-v2._tcp and _airplay._tcp. Both of them seem to stay up while the Apple TV is asleep.Breena
Just to make sure: you want to detect the "awakeness" exclusively from the network, right?Eula
Any idea of what does the AppleTV's network activity look like when it causes those false positives? For example, if those erratic awakenings turn out to be DHCP renewals, maybe you could change the network configuration to minimize or avoid them.Eula
@Eula I'm trying to detect awakeness any way I can. So far I've mostly focused on HDMI-CEC messages as well as the network, but both have proven unreliable. I'm not sure precisely what the Apple TV's network activity looks like when a false positive occurs, but I can try to find out. I do know that when it wakes up due to user action there's no identifiable pattern to its network activity then.Breena
The point of my question about network activity on "spurious" wakeup is that maybe, hopefully, the AppleTV is doing something small and well-defined (like a DHCP renewal). It might guide you to configure the AppleTV to not do that, or about detecting that activity.Eula
@Eula I just tried doing some packet sniffing and compared what I see between a spurious wake up and a wake up caused by me using the Siri remote. Unfortunately I don't have the equipment or network setup to be able to monitor all packets coming to or from the Apple TV, just what's visible over the local network. Using that there's no way to distinguish between the two cases. My always-on PC only has one ethernet port so there's no way for me to have it monitor all Apple TV traffic.Breena
If you're hardware handy you could look at the signal on the hdmi cable.Minimum
@mpactMEDIA I'm not particularly hardware handy but I'm willing to learn and try that. Do you have any pointers you can give on how to rig something up that tests for a signal?Breena
B
1

Here I am, six and a half years later, and I've finally found a better way to get the power state of my Apple TV.

I can simply use pyatv, which has a function named power_state that returns the Apple TV's current power state.

Breena answered 14/11, 2022 at 6:9 Comment(0)
E
2

I tried to post below on apple discussion / support communities but was told i don't have the right to post this content. Maybe someone in this group can succeed in doing it:

Apple TV 4 CEC integration is great when it works, but it doesn't work all the time and not with all the various equipment out there, you can do a search across forums and you will see lots of unhappy users. I would like to use a raspberry PI to detect when my AppleTV goes to sleep and wakes up and programmatically turn my tv on or off using its RS232C or custom CEC commands.

I used a bonjour services explorer and compared every single result between sleep and on states and there are no differences whatsoever.  I would have expected Apple to welcome such automation projects and make this information readily available with a variable such as status: sleep or status: on. 

Is there a way I could tell the two states apart via the network connection?

If not, could one build a TvOS app which runs on the background and makes this information available to clients somehow?

Eyas answered 12/10, 2017 at 6:29 Comment(1)
I haven't tried posting about this on the support forums, but I can answer your questions. It's not possible to make a background application, at least not without jailbreaking tvOS. There are no network services that report whether the Apple TV is in standby or not. And quite frustratingly, I reported a bug to Apple years ago about how the Apple TV doesn't report its status correctly via HDMI-CEC, and they still haven't fixed it. I guess CEC / home automation isn't a priority for them.Breena
B
1

Here I am, six and a half years later, and I've finally found a better way to get the power state of my Apple TV.

I can simply use pyatv, which has a function named power_state that returns the Apple TV's current power state.

Breena answered 14/11, 2022 at 6:9 Comment(0)
B
0

I finally found a method that seems to work consistently. This method is incredibly hacky and not at all the sort of way I'd prefer to do this, but it's the only one I've found so far that works consistently.

I have taken an old USB webcam and affixed it to the front of my Apple TV so that its lens is directly in front of the Apply TV's front facing light. Whenever the Apple TV is asleep, I simply check for the light turning on by taking images from the camera and analyzing their average luminosity. Since the lens is right next to the light, when it turns on it'll create a huge blown out white circle in the image that's incredibly easy to detect.

As long as the Apple TV is asleep, the light turning on seems to indicate 100% of the time that it has woken up. I have yet to find a single incident of either a false positive or false negative.

Since pressing buttons on the Siri remote causes this light to blink, this also means that I can detect buttons being pressed by looking for changes in the light while the Apple TV is awake. It's not 100% accurate, since some button presses are faster than the frame rate of my crappy old USB webcam, but it works well enough.

I would vastly prefer to find a better method of doing this, like making a request over the LAN to the Apple TV where the response clearly indicates it being awake or asleep, but so far it doesn't look like that's possible.

Breena answered 1/4, 2016 at 4:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.