Creating a switch to turn on and off TV, AppleTV and Amp with Homekit

Hello Everyone. I am new here and hoping I can get some help.

My end goal is to create a switch that I can setup with Homekit to turn on and off my TV setup (TV, AppleTV, Soundbar)

I have a harmony hub, but I don’t want to use activities, because they tend to fail.

Currently I have the following:

Scripts - I created four individual scripts to do the base items that I wanted - these all work

tv_device_on:
  alias: Turn On Vizio
  sequence:
  - service: remote.send_command
    data:
      command: PowerOn
      device: 46502904
tv_device_off:
  alias: Turn Off Vizio
  sequence:
  - service: remote.send_command
    data:
      command: PowerOff
      device: 46502904
amp_device_toggle:
  alias: Turn On/Off Amp
  sequence:
  - service: remote.send_command
    data:
      command: PowerToggle
      device: 46502932
apple_tv_device_on:
  alias: Turn On AppleTV
  sequence:
  - service: remote.send_command
    data:
      command: Menu
      device: 46502781

I then have two scripts that all the above building blocks. I created these using the UI script creator, so their formatting is different. These also work.

'1526243091969':
  alias: Turn On TV
  sequence:
  - data: {}
    service: script.tv_device_on
  - service: script.apple_tv_device_on
  - service: script.amp_device_toggle
'1526243168145':
  alias: Turn Off TV
  sequence:
  - data: {}
    service: script.tv_device_off
  - service: script.amp_device_toggle

Now, I run into some trouble. I have created a switch, that I am hoping can turn on and off the “TV” using the scripts above. Right now, the switch is there, but it doesn’t do anything… and it doesn’t keep its state. Once I have this, I plan on connecting this to HomeKit and using Siri to “Turn on TV”

- platform: template
  switches:
    tv_switch:
      friendly_name: 'TV'
      value_template: "{{ is_state('switch.TV', 'on') }}"
      turn_on:
        service: switch.turn_on
        data:
          entity_id: script.'1526243091969'
      turn_off:
        service: switch.turn_off
        data:
          entity_id: script.'1526243168145'

Any help would be greatly appreciated.

In your value_template: "{{ is_state('switch.TV', 'on') }}" you refer to switch.TV. Is that an actual switch you have? What does it do?

You could try the following and see if it works:

- platform: template
  switches:
    tv_switch:
      friendly_name: 'TV'
      value_template: "{{ is_state('switch.tv_switch', 'on') }}"
      turn_on:
        service: switch.turn_on
        data:
          entity_id: script.'1526243091969'
      turn_off:
        service: switch.turn_off
        data:
          entity_id: script.'1526243168145'

@icaman004 good catch and thank you for the help. That was what I had in my first iteration, before I realized that no caps were allowed.

however, even after the change, the behavior is the same. The switch starts off, I toggle it, the UI says I “Turned on TV” then nothing happens… until the switch goes back to the off state.

I wonder if I am approaching this incorrectly. Would it be better to try to do this as something other than a switch?

Right, just noticed in your service, it should be homeassistant.turn_on as you’re calling a script and not a switch.

Also, remove the ’ from around the script names.

Try:

- platform: template
  switches:
    tv_switch:
      friendly_name: 'TV'
      value_template: "{{ is_state('switch.tv_switch', 'on') }}"
      turn_on:
        service: homeassistant.turn_on
        data:
          entity_id: script.1526243091969
      turn_off:
        service: homeassistant.turn_on
        data:
          entity_id: script.1526243168145

EDIT: Corrected service call.

@icaman004 This is great, thank you!

The switch now works from a control standpoint. The only thing that doesn’t happen is the state being saved. so basically, when I force the state of the switch into the off state (using the states interface) and then toggle the switch, everything runs! The problem is that the switch returns to the off state (nothing turns off, just the switch)

The same is true in reverse, where I force the on state and toggle, everything goes off, but the switch moves back to an on state.

I imagine that the value_template is the culprit, but I am not sure what I could use as a sensor outside of the switch itself.

Again, thanks for the help.

A wild idea is to create an input_boolean with an automation that has the same scripts, then use the value_template data from that. It’ll basically double up on the same thing, but you’d be able to expose the switch to HomeKit. If you didn’t need it for HomeKit, I’d suggest using an input_boolean instead.

You could do something like this in the automation:

- alias: ib_tv_on
  initial_state: 'on'
  hide_entity: true
  trigger:
    platform: state
    entity_id: input_boolean.watch_tv
    to: 'on'
  action:
    service: homeassistant.turn_on
    entity_id: script.1526243091969

And the same for off, and then in your value_template you could use

value_template: "{{ is_state('input_boolean.tv_switch', 'on') }}

You could also hide the input_boolean so it’ll all just work in the background.

I’ve got a Harmony Hub as well, and might give this a shot myself, but try it and let me know if it works.

1 Like

Thanks @icaman004

It took a little bit of finagling, but it works!

input_boolean is definitely the way to go

Two things I learned:

  1. switch is really a toggle switch. If you don’t have a sensor to understand the state, then you should really use an automation…
  2. the HomeKit integration has a status file that you need to delete if you need to remove your bridge form Home for some reason

One tip, for the final step of the HomeKit integration, I used the following:

homekit:
  filter:
    exclude_domains:
      - script 

By doing this, all of the component scripts are not sent to the Home app, only the Input_boolean switch. This is nice because if I need to troubleshoot or need to turn on one device because the automation failed, I still have the UI/HA App, but it keeps the Home app really clean (for my wife :slight_smile: )

Thanks again

Hello, i know this is an older thread. but i am trying to get a similar kind off thing working.

I can’t seem to figure out how to achieve it.

This is what i have.
A Harmony hub configured with all my scenes.
One of the scenes is PowerOff.
I would like to create some kind of switch or automation to shut down certain lights, and power off all the devices i turned on with the harmony. (just like if you press the power off button on the remote)

This is what i have in config.yaml

input_select:
  harmony:
    name: Harmony
    options:
      - Tv apps over AV
      - TV over AV
      - CD's Luisteren
      - DVD Kijken
      - TV Kijken
      - PowerOff
      - Radio Luisteren
      - Nvidea Shield
    icon: mdi:monitor

This is what i put in the automations.yaml

- alias: Set Harmony activity using input_select
  hide_entity: True
  trigger:
    platform: state
    entity_id: input_select.harmony
  action:
    service: remote.turn_on
    entity_id: remote.harmony_hub
    data_template:
      activity: >
        {% if is_state("input_select.harmony", "Tv apps over AV") %}
          33040789
        {% elif is_state("input_select.harmony", "TV over AV") %}
          33040786
        {% elif is_state("input_select.harmony", "CD's Luisteren") %}
          33040788
        {% elif is_state("input_select.harmony", "DVD Kijken") %}
          33040790
        {% elif is_state("input_select.harmony", "TV Kijken") %}
          33040784
        {% elif is_state("input_select.harmony", "Radio Luisteren") %}
          33040785
        {% elif is_state("input_select.harmony", "Nvidea Shield") %}
          33040787
        {% else %}
          -1
        {% endif %}
        
# This next automation is to update the input_select when the Harmony's
# activity was changed from somewhere else, e.g. using its physical remote.
- alias: Update Harmony input_select
  hide_entity: True
  trigger:
    platform: state
    entity_id: remote.harmony_hub
  action:
    service: input_select.select_option
    data_template:
      entity_id: input_select.harmony
      option: "{{ states.remote.harmony_hub.attributes.current_activity }}"

This is not mine, i found this on the forum. I don't understand the Harmony docs

I have no idea how i can achieve this the best way.

Maybe a stupid question, but may I ask where the ‘device ID’ is coming from? I don’t know where to find the 8-digit code.

@bricknhymr Did you see my question about the device ID? It would be great if you could help me. Thank you!

So when you add the Logitech hub and restart the HA server a new file is created that has all of your devices, their ids and ids for all of your activities.

As stated in Harmony Hub Remote component

In the file ‘harmony_REMOTENAME.conf’ you can find the available activities, for example:

Activities
  22054907 - Watch TV

This file is in your config folder. You already added the activity IDs in your automations.yaml so you must have seen/used it before. Each device (DVD, TV, Radio, Nvidia Shield, etc.) has its own unique ID in this config file. Add these IDs to your automations.yaml and use them if you want to create scripts, etc.


To turn off a light when you pushed the Power Off button on your Harmony Remote or you changed the input on your input_select to PowerOff you could create a simple automation:

- alias: "Powered off from Harmony Hub and Turn Light Off"
    trigger:
      platform: state
      entity_id: remote.harmony_hub
  condition:
    condition: template
    value_template: '{{ trigger.to_state.attributes.current_activity == "PowerOff" }}'       
  action:
    service: homeassistant.turn_off
    entity_id: light.YOUR_LIGHT_ENTITY_ID

Not tested, but it should work.


EDIT/INFO: Logitech Harmony Hubs support Philips Hue Lights. If you have lights from Philips Hue you can add them to your Harmony Hub and add lights to your start/shutdown sequence.

Thank you @anon90333909 :slight_smile: