SOLVED: Switch template gets stuck ‘on’ in HomeKit when it should turn off again like in HA

Hi guys,

I’m struggling to work out how ensure the switch template I created and passed through to HomeKit as a switch mirrors it’s correct state in HA.

I created it so it turns off again after being activated. Works great in HA. But when activating it on HomeKit it turns on and performs the intended action but unlike HA, it stays on until I manually turn it off again.

Is there something I can do to fix this, please?

I’m not sure that I need to add a command to the turn_on action to turn off again as it is already turning off, do I? I don’t think I need to use a Boolean to store the value of the switch as that would complicate things and the performance of the switch would change? But HomeKit needs something to check for the state of the switch for it to turn off again.

Is the fix to fire an event off that shows the switch turning off again maybe? Maybe then HomeKit will behave? :thinking:

Below is my current code.


switch:
  - platform: template
    switches:
      fix_appletv:
        friendly_name: "Fix Apple TV"
        value_template: "{{ is_state('switch.fix_appletv', 'off') }}"
        turn_on:
          - service: remote.send_command
            data:
              command: inputdvd
              device: 'Denon AV Receiver'
            target:
              device_id: 60a0405005a54b058c277c094515a299
          - service: remote.send_command
            data:
              command:
              - PowerOn
              - InputHdmi2
              device: 'Samsung TV'
              delay_secs: 0.6
            target:
              device_id: 60a0405005a54b058c277c094515a299
        turn_off:
        icon_template: mdi:hammer-wrench

TIA

UPDATE: While waiting for someone to volunteer some helpful advice I continued to try and find what I was missing. I finally worked it out, with a few additional tweaks to keep the Harmony remote activity in sync after the fix.

switch:
  - platform: template
    switches:
      fix_appletv:
        friendly_name: "Fix Apple TV"
        value_template: "{{ states('input_boolean.fix_appletv_state') }}"
        turn_on:
          - service: remote.send_command
            data:
              command: inputdvd
              device: 'Denon AV Receiver'
            target:
              device_id: 60a0405005a54b058c277c094515a299
          - service: remote.send_command
            data:
              command:
              - PowerOn
              - InputHdmi2
              device: 'Samsung TV'
              delay_secs: 0.6
            target:
              device_id: 60a0405005a54b058c277c094515a299
          - service: remote.turn_on
            target:
              device_id: 60a0405005a54b058c277c094515a299
            data:
              activity: "Watch Apple TV"
          - service: input_boolean.turn_on
            data:
              entity_id: input_boolean.fix_appletv_state
          - service: switch.turn_off
            data:
              entity_id: switch.fix_appletv
        turn_off:
          - service: input_boolean.turn_off
            data:
              entity_id: input_boolean.fix_appletv_state
        icon_template: mdi:hammer-wrench

input_boolean:
  fix_appletv_state:

I hope this helps someone else. :blush: