Switch Template & Subordinate Switches

First and foremost, I’m sure this has been asked previously, and I tried to find it, but to no avail. So, here goes…

Here’s the code, then below is my question…

  - platform: template
    switches:
      office_stuff:
        unique_id: office_stuff
        value_template: "{{ is_state('input_boolean.work', 'on') }}"
        turn_on:
          - service: switch.turn_on  <--------------
            entity_id: switch.tv_power_onoff  <--------------
          - service: switch.turn_on
            entity_id: switch.office_heater_mss110_main_channel
          - service: switch.turn_on
            entity_id: switch.300184502462ab05f053
          - service: switch.turn_on
            entity_id: switch.blue_tablet
          - service: input_boolean.turn_on
            entity_id: input_boolean.work
        turn_off:
          - service: switch.turn_off  <--------------
            entity_id: switch.tv_power_onoff  <--------------
          - service: switch.turn_off
            entity_id: switch.office_heater_mss110_main_channel
          - service: switch.turn_off
            entity_id: switch.300184502462ab05f053
          - service: switch.turn_off
            entity_id: switch.blue_tablet
          - service: input_boolean.turn_off
            entity_id: input_boolean.work

I want to draw attention to the lines with the arrows pointing (no, that’s not in my code). Those are their own template switches:

     tv_power_onoff:
        unique_id: tv_power_onoff
        value_template: "{{ is_state('input_boolean.tv_onoff', 'on') }}"
        turn_on:
          - service: scene.turn_on        # uses IR Blaster
            entity_id: scene.wx8clarfb9kvriot
          - service: input_boolean.toggle
            entity_id: input_boolean.tv_onoff
        turn_off:
          - service: scene.turn_on        # uses IR Blaster
            entity_id: scene.wx8clarfb9kvriot
          - service: input_boolean.toggle
            entity_id: input_boolean.tv_onoff

Everything works beautifully. However, when I turn off the office_stuff switch, and input.boolean for tv_power_off is indicating off, it still fires that scene. My logic would say that if the template is turning off, but the subordinate switch is already off, it would skip over it?

Is there a reasonably easy fix in the above? I’d like to avoid doing an automation if possible.

Thanks!

Did you ever get this to work? I am trying to perform a TV on and Off also.

Yep, I sure did, and it worked wonderfully. However, the IR blaster has since been repurposed to do bigger/better things (controlling a space heater–but follows the EXACT same logic as if were toggling a TV). Since I now have Google TV devices on my TV’s, their on/off is controlled by HDMI CEC, for which I have made template switches, too.

Just to save us both some extra work, which switch do you want me to show? Power as a function of the IR Blaster or as a function of GoogleTV (media_player.turn_on / media_player.turn_off)?

Basically I have a Broadlink IR Blaster. I created scripts which calls a service to turn a TV on and off. Scripts work but I have to tell Echo to “turn on TV_Off” - ugly. You used a scene - not sure how you did that! When I go to create a scene I can see devices (which include broadlink but that the extent of it) and entities which includes my scripts.

So, here’s my setup for the space heater (which is on/off with my Tuya IR Blaster). My scene is based on the Tuya App (and Tuya Cloud integration). In the app with my phone, I created a scene, and the Tuya HA integration saw it and automatically created a scene entity to launch it. Looks like this for some context. But, in your case, I don’t see any reason why you can’t replace scene.turn_on (and the subsequent scene entity) with script.turn_on and your script; should work the same.

  • I first built an input boolean helper. In my case: input_boolean.ir_heater_onoff.

  • And from that, a template switch:

  - platform: template
    switches:
      space_heater:
        unique_id: space_heater
        value_template: "{{ is_state('input_boolean.ir_heater_onoff', 'on') }}"
        turn_on:
          - service: scene.turn_on
            entity_id: scene.heater_power
          - service: input_boolean.toggle
            entity_id: input_boolean.ir_heater_onoff
        turn_off:
          - service: scene.turn_on
            entity_id: scene.heater_power
          - service: input_boolean.toggle
            entity_id: input_boolean.ir_heater_onoff

Take note that it’s running the same scene to turn the switch on and off (since it’s just effectively pressing the power button the remote). You’ll do the same with your script.

ONE NOTE: It’s possible they can get out of sync (someone turns off the device manually, the IR is blocked, etc.), so HA could have the switch off, but the device on. Either go in and manually toggle the input boolean helper, or just turn off or on the device manually.

Hope it works. Good luck!

1 Like

Awesome! Thanks, I got it working !

happy to help!