Create a switch with conditions?

I have created a switch that works in my switch.yaml file

- platform: template
  switches:
    game_lights_toggle:
      value_template: "{{ is_state('input_boolean.game_lights_status', 'on') }}"
      turn_on:
        - service: scene.turn_on
          entity_id: scene.game_lights_scene
        - service: input_boolean.turn_on
          target:
            entity_id: input_boolean.game_lights_status
      turn_off:
        - service: scene.turn_on
          entity_id: scene.living_room_lights_adaptive_on
        - service: input_boolean.turn_off
          target:
            entity_id: input_boolean.game_lights_status

But I would like it to only be available if my light.living_room_lights entity is on. How would I rewrite this code so that it does so?

Removed irrelevant node-red and third-party-integrations categories.

What do you mean by “available”? You could add this, which would make the switch unavailable if the light is off, but I suspect that’s not quite what you mean:

      availability_template: "{{ states('light.living_room_lights') }}"

Consider using an “if” script statement. It can test for the light.living_room_lights status, and, if the switch should be unavailable, it can simply do nothing; otherwise do the actions you want. Is that the behavior you want?

Generally speaking, the term “unavailable” in connection with Home Assistant entities is specialized. You almost never want to actively make something “unavailable.” I suspect what you want is to be able to have the turn_on and/or turn_off actions do nothing in certain circumstances.

I have actually thought it over and would like to modify the switch. If the light.living_room_lights is already on, operate as before, but if the living room lights is off, turn it on and do the command. Then when toggle off is activated, turn off the living room lights as well, but only if this switch commanded the living room lights to turn on, is this possible?

Create an input boolean (toggle) helper called input_boolean.game_lights_flag, and an automation:

trigger:
  - platform: state
    entity_id: light.living_room_lights
    to: 'off'
action:
  - service: input_boolean.turn_off
    target:
      entity_id: input_boolean.game_lights_flag

Then your switch becomes this (exactly as before but with the two if/then blocks added):

- platform: template
  switches:
    game_lights_toggle:
      value_template: "{{ is_state('input_boolean.game_lights_status', 'on') }}"
      turn_on:
        - if:
            - alias: "Living room lights are off"
              condition: state
              entity_id: light.living_room_lights
              state: 'off'
          then:
            - alias: "Set the flag"
              service: input_boolean.turn_on
              target:
                entity_id: input_boolean.game_lights_flag
            - alias: "and turn the lights on"
              service: light.turn_on
              target:
                entity_id: light.living_room_lights
        - service: scene.turn_on
          entity_id: scene.game_lights_scene
        - service: input_boolean.turn_on
          target:
            entity_id: input_boolean.game_lights_status
      turn_off:
        - if:
            - condition:
                - alias: "Living room lights are on"
                  condition: state
                  entity_id: light.living_room_lights
                  state: 'on'
                - alias: "and this switch turned them on"
                  condition: state
                  entity_id: input_boolean.game_lights_flag
                  state: 'on'
          then:
            - alias: "Turn them off (automation will clear the flag)"
              service: light.turn_off
              target:
                entity_id: light.living_room_lights
        - service: scene.turn_on
          entity_id: scene.living_room_lights_adaptive_on
        - service: input_boolean.turn_off
          target:
            entity_id: input_boolean.game_lights_status

I think that does what you asked for: might need tweaking if the living room lights are impacted by the scene changes. The new flag entity records whether the living room lights were last turned on by the template switch.

You got it working! Thank you!