How to stop Apple TV from randomly turning on itself and TV

There have been a few past threads on this (here by @jcastro and here by @ColonelRhodes) and it doesn’t look like anyone in the HA community has shared a solution. As has been logged on many Apple forums as far back as 2015, an Apple TV when turned off may randomly turn back on. A key to this is that it’s where you have HDMI-CEC enabled between your TV and the Apple TV; when the Apple TV comes back on, so does the TV.

I got so frustrated with this (as did my wife) that I decided to see if I could capture data explaining when and if possible why it happens. I didn’t discover the why but I did find a pattern: after closely watching the HA history for the Apple TV and my LG TV, I noticed that on the occasions that the Apple TV turned everything back on, the state transition of the Apple TV from Idle to Standby and of the TV from On to Off happen at the exact same instant, down to the second. In cases where everything stayed off, the Apple TV state change happens first and then the TV state change happens about 20 seconds later.

With that in mind, I put together an HA automation that looks for this pattern and, when it occurs, turns everything back off.

Below is the automation YAML. A few factors to keep in mind:

  • This is only applicable if you have HDMI CEC enabled so that turning your TV on/off turns on/off the Apple TV and vice versa.
  • If you usually turn off your TV and let it turn off the Apple TV, you probably are not seeing this random restart action. For me it occurs specifically when we turned off the Apple TV and let it turn off the TV. However, I have a “goodnight” scene that I set up which has both the TV and Apple TV in off/standby states and when I use it, I’ll still sometimes get this random restart.
    Anyway, that’s why I call service to turn off the TV instead of the Apple TV.
  • In the automation, there are two delays - either of which you may need to adjust for behavior of your own system:
  1. checking when Apple TV comes on to see if things had been turned off within the last 2 minutes
  2. waiting 1 minute before actually turning everything off again to account for the lag between when the Apple TV goes to Idle and the TV comes on

I hope folks find this useful.

Automation

alias: Apple TV Ghostbuster
description: >-
  Turn off LG TV when Apple TV "ghost" detected (i.e., they came back on within
  a few minutes of being turned off without anyone specifically taking steps to
  turn them on). 
trigger:
  - platform: state
    entity_id: media_player.apple_tv_4k
    from: standby
    to: idle
condition:
  - alias: Check whether Apple TV turned itself and TV back on after being turned off
    condition: and
    conditions:
      - condition: state
        entity_id: media_player.living_room_tv
        state: "off"
        alias: Confirm TV is off (when ghost appears, Apple TV comes on first)
      - condition: template
        value_template: >
          {% set apple_tv_last_updated = states.media_player
              | selectattr('entity_id', 'eq', 'media_player.apple_tv_4k')
              | map(attribute='last_updated')
              | list | first %}
          {% set lg_tv_last_updated = states.media_player
              | selectattr('entity_id', 'eq', 'media_player.living_room_tv')
              | map(attribute='last_updated')
              | list | first %}
          {{ (as_timestamp(now()) - as_timestamp(apple_tv_last_updated)) <= 120
          and
             (as_timestamp(now()) - as_timestamp(lg_tv_last_updated)) <= 120 }}
        alias: >-
          Check if both the Apple TV and the TV had a state change in past 2
          minutes
action:
  - delay: "00:01:00"
    alias: >-
      Delay for 1 minute to allow time for the TV to register as On (it lags
      behind the Apple TV)
  - service: media_player.turn_off
    enabled: true
    data: {}
    target:
      entity_id: media_player.living_room_tv
    alias: >-
      Turn off the TV (having it pass the power command to Apple TV seems to
      avoid risk of Apple TV randomly coming back on)
  - service: notify.mobile_app_pixel_7
    data:
      message: Busted the Apple TV Ghost!
    enabled: false