Running: Apple TV integration!

Hi Home Assistant Community,

i have multiple apple tv 4k.
I use the following Scripts to control power state:

power off script (scripts.yml)

atv01_off:
  alias: ATV01 off
  sequence:
  - service: remote.send_command
    target:
      entity_id: remote.atv01
    data:
      command:
      - home_hold
      - select
  - service: media_player.turn_off
    target:
      entity_id: media_player.atv01

power on script (scripts.yml)

atv01_trigger_on:
  alias: ATV01 on trigger
  sequence:
  - service: remote.send_command
    target:
      entity_id: remote.atv01
    data:
      command:
      - home
  - service: media_player.turn_on
    target:
      entity_id: media_player.atv01

sadly i need to execute power on trigger twice, because of that error:

2021-03-10 08:32:59 ERROR (MainThread) [homeassistant.components.apple_tv.remote] Unable to send commands, not connected to ATV01

Thats why i created third script (scripts.yml) calling the power on trigger script twice

atv01_on:
  alias: ATV01 on
  sequence:
  - repeat:
      count: '1'
      sequence:
      - service: script.atv01_trigger_on
        data: {}
      - delay:
          hours: 0
          minutes: 0
          seconds: 2
          milliseconds: 0
      - service: script.atv01_trigger_on
  mode: single

i wrap these scripts into a button (switch.yml)

  - platform: template
    switches:
      atv01_power_switch:
        friendly_name: ATV01
        value_template: >-
          {% if is_state('media_player.atv01','off') %}Off
          {% else %}On
          {% endif %}
        turn_on:
          service: script.atv01_on
        turn_off:
          service: script.atv01_of

Now my questions:

a) Can you help me finding out why i need to send power on command twice?

and

b) how can i create a single script for powering on and off the devices, with parameter (e.g. atv01, atv02 …) for using these scripts with different (calculated) entity ids for media player and remote? Can entity ids be calculated via script? Also i am a little bit stuck with the “data” and the “data-template” and the “fields” and the “variables” Can you help me solving that puzzle, so that i can use two parameterized scripts in multiple buttons for multiple atvs?

Regards
Clem

My final solution:

A switch:

  platform: template
  switches:
    atv01_power_switch:
      friendly_name: ATV01
      value_template: >-
        {% if is_state('media_player.atv01','off') %}off
        {% elif is_state('media_player.atv01','paused') %}on
        {% elif is_state('media_player.atv01','playing') %}on
        {% else %}on
        {% endif %}
      turn_on:
        service: script.atv01_on
      turn_off:
        service: script.atv01_off

on script

atv01_on:
  mode: restart
  alias: ATV01 on
  sequence:
    repeat:
      count: 3
      sequence:
        - service: remote.send_command
          target:
            entity_id: remote.atv01
          data:
            delay_secs: 1
            command:
            - wakeup
        - service: media_player.turn_on
          target:
            entity_id: media_player.atv01
        - delay:
            hours: 0
            minutes: 0
            seconds: 1
            milliseconds: 0

off script

atv02_off:
  alias: ATV02 off
  sequence:

  - service: remote.send_command
    target:
      entity_id: remote.atv02
    data:
      command:
      - home_hold
      - select

  - delay:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 0

  - service: media_player.turn_off
    target:
      entity_id: media_player.atv02
2 Likes

Did you ever figure out why you lose connection to begin with? I’m having the same problem on my Apple TV.

No.
Sadly not :frowning:
But i think about moving away from hass, as they plan to have a gui only system.

I love hass to configure my entire home in simple yml files, but if i see integrations reporting that yaml config will not work any more in the future, and i need to use a ui wizzard to create stuff - hass will be removed from my stack further or later!

Generally it seems that only integration configuration is moving to GUI → scripts/and automations will always be YAML (if you want)