Template Switch does not work

Hi all,

I have a template switch that looks like this:

switch:
    - platform: template
      switches:
        alarm_intern:
        value_template: >-
          {% if is_state('binary_sensor.alarm_status_intern_scharf', 'off') %}
              off
          {% else %}
              on
          {% endif %}
          turn_on:
            service: switch.turn_on
            data:
              entity_id: switch.alarm_innen_scharf
          turn_off:
            service: switch.turn_off
            data:
              entity_id: switch.alarm_innen_unscharf

The switch is supposed to arm the alarm_system when its unarmed and unarm when its armed.

Arm and Unarm are 2 different actions on 2 different KNX group addresses. If I just create two buttons for arm using switch.alarm_innen_scharf and the other one with switch.alarm_innen_unscharf I can enable and disable the alarm system. Also the status works (binary_sensor.alarm_status_intern_scharf).

But when I want to combine this to only one button it does not work.

What am I doing wrong?

Your YAML doesn’t appear to be indented properly, plus you can eliminate the if…then…else since the device state is the same as what you want to represent.

You can try this to see if it helps you at all:

switch:
    - platform: template
      switches:
        alarm_intern:
        value_template: "{{ states('binary_sensor.alarm_status_intern_scharf') }}"
        
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.alarm_innen_scharf
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.alarm_innen_unscharf

Also, and maybe this is just semantics, I have my templates set up like this, it is a bit different than yours where I’m including the switch entity:

switch:
  - platform: template
    switches:              
      my_custom_switch:
        friendly_name: "My Custom Switch"
        unique_id: my_custom_switch
        value_template: "{{ states('input_boolean.whatever') }}"
        turn_on:
          - service: input_boolean.turn_on
            entity_id: input_boolean.whatever
    
          - service: input_boolean.toggle
            entity_id: input_boolean.whatever      
          
        turn_off:  
          - service: input_boolean.turn_off
            entity_id: input_boolean.whatever  

Indent lines beyond alarm_intern like this:

switch:
  - platform: template
    switches:
      alarm_intern:
        value_template: "{{ is_state('binary_sensor.alarm_status_intern_scharf', 'on') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.alarm_innen_scharf
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.alarm_innen_unscharf

EDIT

Corrected as per tom_I’s post below.

1 Like

Ahh, that’s where the name was, I wasn’t sure if that was an attribute he was setting!

Should the the template resolve to true or false, not off or on?

value_template: "{{ is_state('binary_sensor.alarm_status_intern_scharf', 'on') }}"
1 Like

Correct! My mistake; I blindly copied it.

Thank you! I’ve corrected the example.

1 Like

Hi all,

thank you for all your replies. Its really wired. I have fixed the intend the code looks now like

switch:
  - platform: template
    switches:
      alarm_intern:
        value_template: "{{ states('binary_sensor.alarm_status_intern_scharf') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.alarm_innen_scharf
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.alarm_innen_unscharf

but it does not work. Its really strange

Your entity_id is different for the on and off actions, is that intentional?

Yes that’s the problem. The KNX connector for the alarm system resolves the arm and unarm action to 2 different group addresses. So one is “arm” and one is “unarm”. If I create 2 buttons it both buttons work and I can turn on and off the alarm. But I want to configure it into one “normal” switch.

I’d guess your switch.alarm_innen_scharf is always on and switch.alarm_innen_unscharf is always off. so no state change happens and nothing is sent to the bus?

1 Like

yes the state is reported via

binary_sensor.alarm_status_intern_scharf.

Can I use if else somehow in the action?

I guess… but you could also just use the state_address of the switches to set the state of your switch correctly.
Or completely omit the 2 switches and use knx.send service instead.

Your latest example contains the wrong value_template. See tom_I’s post above for the correct template.

It may not fix everything but it’ll be one step closer.

Hey @123 @farmio

guys that was great! this works like a charm:

switch:
  - platform: template
    switches:
      alarm_intern:
        value_template: "{{ is_state('binary_sensor.alarm_status_intern_scharf', 'on') }}"
        turn_on:
          service: knx.send
          data: {"address": "14/5/200", "payload": 1}
        turn_off:
          service: knx.send
          data: {"address": "14/5/216", "payload": 1}

thank you for all your help!

2 Likes

This is the third topic I’ve seen this week where, after receiving assistance from several people, the author marks their own post as the Solution. It’s impressive how they solved their own problem.

1 Like

Sorry @123 , I didn’t know how that works. I just thought the final code is the best thing for any other member to see.

In fact 2 people helped strongly to solve this. How would you suggest to mark the topic. Happy to learn and improve.

Ultimately, its your decision to select a reply that best describes what is wrong and how to fix it.

In this case, the problem was far more fundamental than what was associated with turn_on and turn_off. It had incorrect indentation and an invalid value_template. Unless those basic errors were fixed, it didn’t matter what was in turn_on and turn_off.