Switch template triggering unwanted effects

Hey all, to start with a bit of back ground. I have a dumb TV in my living room, I have it plugged into a smart plug which is only there for energy monitoring. I also have a broadlink RF blaster programmed with a few of the TV remote buttons, and a short script for each of the programmed buttons to work, ie when I want to turn up the volume I call upon the relevant script. I setup a Threshold sensor helper to identify if the TV was on or not (reads 0 Watts when powered off, about 5W when first starting up and about 90-100W when operating). The threshold is 0.4-105W reads as the TV is on and everything else is off, its been working perfectly. I then used a template switch to make a UI switch that I can use from my app that gets feedback from the helper RE the tv state. The YAML from my config file relevant to the template switch is below, its fairly straight forward.

Onto the strangeness, when I use this switch; Home Assistant for unknown reasons also tries to turn on my Xbox. The Xbox integration works intermittently at best in my experience, and not at all for the last week or so at the time of this writing. When the integration isn’t working and I use the button an error message reading something to the effect of “xbox unavailable right now” and it details the specific service it is trying to call which turns on the xbox. If the integration is working at the time of triggering the switch, it turns the xbox on if it isn’t already on (if it is on already, a “I can’t do that right now” message comes up on the xbox.

switch:
  - platform: template
    switches:
      living_room_tv:
        value_template: "{{ is_state('binary_sensor.living_room_tv_power_state', 'on') }}"
        turn_on:
          service: script.living_room_power
          target:
            entity_id: binary_sensor.living_room_tv_power_state
        turn_off:
          service: script.living_room_power
          target:
            entity_id: binary_sensor.living_room_tv_power_state
        icon_template: >-
          {% if is_state('binary_sensor.living_room_tv_power_state', 'on') %}
            mdi:television-classic
          {% else %}
            mdi:television-classic-off
          {% endif %}

And next is the YAML of the script for the broadlink device being called


alias: Living Room TV Power
sequence:
  - type: toggle
    device_id: 4abbb3a42e0094e19b6a497bf20c1459
    entity_id: remote.living_room_tv_remote_remote
    domain: remote
    enabled: false
  - service: remote.send_command
    data:
      device: LivingRoom_TV
      command: Power
      hold_secs: 0.3
      num_repeats: 1
      delay_secs: 0.4
    target:
      entity_id: remote.living_room_tv_remote_remote
      area_id: living_room
mode: single
icon: mdi:television-classic

Having one button turn on the xbox and the tv isn’t entirely undesirable as it is the device we use for most of our entertainment on that TV anyway, but I would rather set that up separately perhaps in a scene or the like.

EDIT: Update, I haven’t done anything, but I just used it and no message popped up about being unable to reach the Xbox, I think its more to do with how broken that integration is than my switch not calling the service.

You seem to have a few extraneous configuration entries related to targets. For example, your turn_on and turn_off have targets that don’t make sense and your remote.send_command is targeting both a specific entity and every entity in the livingroom…

There also doesn’t seem to be any connection to the XBox in what you have posted. Are you sure there aren’t other automations or scripts that could be interacting negatively?

I don’t know what you mean by the first part, It is the first and only template I’ve done so I mostly copy/pasted it together and don’t really understand all of it. It is very possible I have something copied from the internet into my code incorrectly when I put it together.

I only have 4 scripts, all for functions of the TV remote (power which you can see in my post), input, volume up, volume down). And I don’t have any automatons involving the living room space, my setup is very new and I only have a few automations. 3 of them only serve to notify me for various things (low batteries, flood sensor, fridge temperature), 2 to control the ventilation fan in my bathroom (can probably be reduced to one but this is simpler for now) and a new one I wrote yesterday to control devices in a group for Christmas lights.

If it is targeting every entity in the living room, the xbox might be the only other entity in the livingroom which can be turned on (most of my devices are sensors at the moment)

No problem, that’s where we all start… try the following for your Template Switch:

switch:
  - platform: template
    switches:
      living_room_tv:
        value_template: "{{ is_state('binary_sensor.living_room_tv_power_state', 'on') }}"
        turn_on:
          service: script.turn_on
          target:
            entity_id: script.living_room_power
        turn_off:
          service: script.turn_on
          target:
            entity_id: script.living_room_power
        icon_template: >-
          {% if is_state('binary_sensor.living_room_tv_power_state', 'on') %}
            mdi:television-classic
          {% else %}
            mdi:television-classic-off
          {% endif %}

As for the the script, it’s unlikely the target is causing the issue you’ve described, but Xboxes do have IR sensor on them to work with universal remotes, so it’s a possibility. I’d go ahead and get rid of the area: target since it’s not necessary.

alias: Living Room TV Power
sequence:
  - service: remote.send_command
    data:
      device: LivingRoom_TV
      command: Power
      hold_secs: 0.3
      num_repeats: 1
      delay_secs: 0.4
    target:
      entity_id: remote.living_room_tv_remote_remote
mode: single
icon: mdi:television-classic
1 Like

Well I’ve implemented your suggested changes and it at the very least still works, I noticed right after posting that I am not getting any indication that it is calling to power on the xbox anymore so I can’t really say with certainty if this is resolved but it might have to do for now.

I’ll have to see if I can program the broadlink to turn on the xbox intentionally, I didn’t know I could control it via IR, that could be handy given how unreliable the Xbox integration is proving.

If you search the forum there are a couple threads that have the Broadlink codes for XBox. You may also want to take a look at the SmartIR custom integration. It basically combines the aspects of template switches and commands from remotes like Broadlink so you have media player entities to work with in HA.