Help with template switch

Hi all,

I’ve read all post on template switch, value template etc and tried to configure my own template switch sensor but without success.

Here’s the code I’m using:

input_boolean:
  operazioneadele:
    name: "Operazione Adele"
    icon: mdi:account-child

switch:
  - platform: template
    switches:
      operazioneadele:
        value_template: "{{ is_state('input_boolean.operazioneadele', 'on') }}"
        turn_on:
          - service: script.turn_on
            entity_id: script.buonanotteadele
          - service: input_boolean.turn_on
            entity_id: input_boolean.operazioneadele
        turn_off:
          - service: script.turn_on
            entity_id: script.buongiornoadele
          - service: input_boolean.turn_off
            entity_id: input_boolean.operazioneadele

The error is that switch turn on but then after 1 sec it goes to off (while the status is still on because the input boolean does not change).

Can some one please help me fixing this error?

Thank you in advance

Can you post the scripts as well? This seems fine so far.

EDIT: incorrect.
I think the turn on and turn off key-value pairs have to be single actions not a list of actions. You might have to put them in scripts.
```
switch:
- platform: template
switches:
operazioneadele:
value_template: “{{ is_state(‘input_boolean.operazioneadele’, ‘on’) }}”
turn_on:
script.switch_operazioneadele_on
turn_off:
script.switch_operazioneadele_off

script:
- switch_operazioneadele_on:
sequence:
- service: script.turn_on
entity_id: script.buonanotteadele # you could put the actions from this script here
- service: input_boolean.turn_on
entity_id: input_boolean.operazioneadele

- switch_operazioneadele_off:
sequence:
- service: script.turn_on
entity_id: script.buongiornoadele # you could put the actions from this script here
- service: input_boolean.turn_off
entity_id: input_boolean.operazioneadele

Think that will return true or false, not on or off. Try this instead, this is what I use in switches:


value_template: "{{ states('input_boolean.operazioneadele') }}"

Which is what it should do. Have a look at the examples:

Fair enough, you learn something new every day. thanks

I think I’ve run into this before.

try the following in the “value_template”:

value_template: >
  {% if is_state('input_boolean.operazioneadele', 'on') %}
    true
  {% else %}
    false
  {% endif %}

I’m not sure why but for some reason even tho the template returns “True/False” I think it is treating it as a string and not a boolean. the template above forces the string to a boolean and it should work.

hey Tom, your solution was the right one!

Moreover with the idea to create another script for on and off I created different scenarios without condition making the overall script more flexible.

Thank you Tom and thank you all for your kind support!

Well that’s interesting. Someone had a similar setup in another thread (two actions in the turn_on/off) and I asked them if it actually worked. They said yes. So I thought my suggerstion must be wrong. Maybe their second action wasn’t as important :man_shrugging:

I think template switches are a bit complex. We are lucky to have an active community to fix things :slight_smile: