Template switch stopped working

This morning I added a switch via broadlink integration for my IR blaster to power on/off my xbox. I’ve for some some time now had a simple template switch which references the power sensor on a smart plug for its state and executes a script to have the IR blaster turn the TV on/off. See below for the yaml of both switches.

After getting the xbox switch where I want it (for now), about an hour later I open the app on my phone to find an error message reading Entity not available: switch.living_room_tv

I’ve confirmed that the script the template switch calls still works, the smart plug is working and getting accurate power information, the helper I setup to delineate when the tv is on or off is working. I didn’t make any changes to the the template switch when I added the other switch, it could be coincidence. I also updated HA supervisor this morning IIRC, but that was several hours ago.

I’m at a loss here, I can say with 100% confidence that the yaml for the tv switch has not changed (I got it from a helpful commenter from a post of mine here a while back and I can confirm from that I didn’t accidentally change it in any way).

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 %}

switch:
  - platform: broadlink
    mac: mac address here
    switches:
      - name: xbox_power
        command_on: JgBQAAABKJQTEhMTExITEhQSExIUEhM3ExMTEhMSFDcTNxQSEzcTOBMSEzcUEhM3FBITNxMTExITNxQSEzcUEhM3ExMTNxM3FAAFngABKUoTAA0FAAAAAAAAAAA=
        command_off: JgBQAAABKJQTEhQSExIUEhMSFBITEhM3FBITEhQSEzcUNxMSEzgTNxM3FBITEhQ3ExITOBMSExIUEhM3FDcTEhM3FBITNxQ3EwAFnwABKUkTAA0FAAAAAAAAAAA=

How are you determining the state of the TV? Is the TV plugged into the Smart Plug? How about posting a connection diagram because what does the Xbox have to do with the other components you mentioned?

Dumb tv plugged into smart plug, when power is over 0.4 Watts and below 105 Watts the binary sensor helper determines the TV is on.

The only thing the TV and Xbox have in common (in HA) is they are both using the same IR blaster and they are next to each other in my configuration.yaml file, my original assumption was I had accidentally changed the tv switch while I was working on the xbox switch as the timing was rather conspicuous.

I think that you have lost connection with the smartplug.

No, before posting I confirmed the smart plug is working fine. It is reading an accurate power consumption value and responds to on/off commands as I would expect. I just checked again before submitting this reply to be double sure, still looking like rock solid performance on behalf of the plug.

I’m no expert, but why do you have duplicate key’s? I would have thought it should be:

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 %}

  - platform: broadlink
    mac: mac address here
    switches:
      - name: xbox_power
        command_on: JgBQAAABKJQTEhMTExITEhQSExIUEhM3ExMTEhMSFDcTNxQSEzcTOBMSEzcUEhM3FBITNxMTExITNxQSEzcUEhM3ExMTNxM3FAAFngABKUoTAA0FAAAAAAAAAAA=
        command_off: JgBQAAABKJQTEhQSExIUEhMSFBITEhM3FBITEhQSEzcUNxMSEzgTNxM3FBITEhQ3ExITOBMSExIUEhM3FDcTEhM3FBITNxQ3EwAFnwABKUkTAA0FAAAAAAAAAAA=

Huh, that did the trick. Kicker is I even looked into that but couldn’t find anything on it, I’m no programmer and mostly taking things from examples and re-purposing them for my own use. I guess sometimes I just lack the right keyword or background knowledge to do the right way, thanks much for the help!

Use Visual Studio Code it should pick up error’s like that.

I think it say’s some where in the Doc’s that if there are multiple Key’s it will use the last one.

Does visual studio code run well on a raspberry pi? My HA is on a pi 4, I’m just using the generic file editor integration.

Yes, it works fine Raspberry Pi 4