Disable or prevent switch.turn_on based on a condition

Use case: I have a window fan and only want to be able to switch it on if the window is open. If it is accidentally turned on with the window closed, it can cause damage to the motor. I know I can create an automation to catch when the state goes to ‘on’ and then switch it back to off right away.

I was wondering I there is a way to go one better and prevent the switch from executing? Like a disable concept?

How about using a template switch.

You could in theory use a template for the “turn on” action to only fire if window is open.

Something like this?

switch:
  - platform: template
    switches:
      window_fan:
        friendly_name: 'Window Fan'
        value_template: "{{ is_state('sensor.window_fan', 'on') }}"
        turn_on:
          service: switch.turn_on
          data_template:
            entity_id: >
              {% if is_state('binary_sensor.window', 'open') %}
               entity_id: switch.window_fan
              {% else %}
               entity_id: switch.null
              {% endif %}
        turn_off:
          service: switch.turn_off
          entity_id: switch.window_fan
3 Likes

This looks promising, didn’t know you could use a data_template in a template switch. I’ll test it out when I get home. Thanks!

Just gave it a go and it seems to work, thanks!

Just to add onto this I’ve tweaked the code a little to suit what I’m using it for (similar thing not being able to send the swing ir command if the fan isnt on) but it doesnt seem to actually work it never actually toggles the swing switch?

- platform: template
  switches:
    wall_fan_swinger:
      friendly_name: 'Wall Fan Swinger'
      value_template: '{{ states.switch.wall_fan_swing.state }}'
      turn_on:
        service: switch.turn_on
        data_template:
          entity_id: >
            {% if is_state('switch.wall_fan', 'on') %}
            entity_id: switch.wall_fan_swing
            {% else %}
            entity_id: switch.null
            {% endif %}
      turn_off:
        service: switch.turn_off
        data_template:
          entity_id: >
            {% if is_state('switch.wall_fan', 'on') %}
            entity_id: switch.wall_fan_swing
            {% else %}
            entity_id: switch.null
            {% endif %}

Two things to narrow it down. If you get rid of the template and just do entity_id: switch.wall_fan_swing does it turn on? If you toss the template in the template area on the ui (Developer Tools - Template) does it give the result you are expecting?

Not sure if this is it or not. but try this

    value_template: '{{ is_state('switch.wall_fan_swing', 'on') }}'

yours returns off where mine returns false.

value_template: ‘off’
value_template: ‘False’

it would appear but not documented that value_template wants a true/false statement.

Totally agree with @rabittn but I think the value template should just be what is used to maintain the state of the switch and not affect the actual service from executing.

Here is mine that I made that for sure works.

 - platform: template
    switches:
      fan:
        friendly_name: "Fan"
        value_template: "{{ is_state('switch.wemo_switch','on')}}"
        turn_on:
          service: switch.turn_on
          data_template:
            entity_id: >
              {% if is_state('sensor.office_window', 'Open') %}
                switch.wemo_switch
              {% else %}
                switch.null
              {% endif %}
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.wemo_switch
1 Like

I suppose i misunderstood. I though maybe the switch itself wasn’t showing the correct state. I did not realize the command did not work as well.

Ha, I guess it could be either. Depends on whatever this means.

Ill re-explain the problem so what im trying to do is have it so that I can only use the swing toggle if the fan is already on, both are sent via IR so I’m trying to use home assistant to keep track of the status eg
Fan is off you cannot send the swing command
Fan is on you can send the swing command

I have to do it that way because it remembers the last option for swing so if the fan was turned off with swing on then swing on needs to remain untouchable until the fan is switched back on.
Hopefully they makes a bit more sense also @azeroth12 I tried your code and I still have the same issue, the “real ir switch that sends the command” is not toggled in home assistant and also the template switch always automatically switches itself off if the fan is off and vice versa if the fan is on (itll turn itself back to the on position if I try switch it off)
Sorry for the thread hijack I just thought our issues were similar.

Hey Guys, when I try this solution I get:

ERROR (MainThread) [homeassistant.helpers.script.1p_svetlo_kuchyne_bodovky] 1P Světlo Kuchyně bodovky: Error executing script. Invalid data for call_service at pos 1: not a valid value for dictionary value @ data[‘entity_id’]

Error is showing, when I turn on switch with following setting in configuration.yaml:

- platform: template
    switches:
      homekit_1p_svetlo_kuchyne_bodovky:
        friendly_name: '1P Světlo Kuchyně bodovky'
        value_template: "{{ is_state('sensor.svetlo_kuchyne_bodovky_sensor_mb', '770') }}"
        turn_on:
          service: switch.turn_on
          data_template:
            entity_id: >
              {% if is_state('switch.obyvak_stul_homekit', 'on') %}
              entity_id: switch.svetlo_kuchyne_bodovky_obyvakstul_on_switch_mb
              {% else %}
              entity_id: switch.svetlo_kuchyne_bodovky_obyvakstul_off_switch_mb
              {% endif %}
        turn_off:
          service: switch.turn_off
          data_template:
            entity_id: >
              {% if is_state('switch.obyvak_stul_homekit', 'on') %}
              entity_id: switch.svetlo_kuchyne_bodovky_obyvakstul_on_switch_mb
              {% else %}
              entity_id: switch.svetlo_kuchyne_bodovky_obyvakstul_off_switch_mb
              {% endif %}

Is the anybody, who knows where is problem?

Thanks.