Templating, value_template: <-- How to use it and what does it do?

Dear Community,

i created a template switch and it does what it is supposed to do, but i don’t understand what value_template actually does. Is it a kind of a condition, does it set a state?

Could somebody clear this up for me please? I read the documentation but it’s still unclear to me.

Template Switch Official Documentation

These two template switches im using currently:

#Switch Vacuum Homekit
  - platform: template
    switches:
      homekit_vacuum_downstairs:
        friendly_name: "Roomba Downstairs"
        value_template: '{{ is_state("vacuum.roomba_downstairs", "cleaning") }}'
        turn_on:
        - service: vacuum.start
          data:
            entity_id: vacuum.roomba_downstairs
        - service: counter.increment
          data:
            entity_id: counter.counter_roomba_downstairs
        turn_off:
        - service: vacuum.return_to_base
          data:
            entity_id: vacuum.roomba_downstairs
        - delay:
            seconds: 1
        - service: vacuum.return_to_base
          data:
            entity_id: vacuum.roomba_downstairs
#Main sound system
  - platform: template
    switches:
      amp:
        friendly_name: "Amp"
        value_template: '{{ is_state("switch.amp", "on") }}'
        turn_on:
          service: switch.turn_on
          data:
            entity_id: 
              - switch.socket_cinema_main
              - switch.socket_cinema_sub_left
              - switch.socket_cinema_sub_right
        turn_off:
          service: switch.turn_off 
          data:
            entity_id: 
              - switch.socket_cinema_main
              - switch.socket_cinema_sub_left
              - switch.socket_cinema_sub_right
1 Like

Eh ?
The value template is the condition/state that you emulate with your virtual switch.
It keeps track of the real world device and mimics the state in HA.
But with the turn on / turn off statements it can also do the reverse (ie act on real world objects)

I don’t understand your second switch though, its turn on == turn off

Edit: your use of quotes is (though workable) contra convention, as most people tend to use double quotes on the outside, not the inside (makes cutting and pasting from the forum less frought)

ohh, my mistake I just changed something and forgot to set it to off again.

But i only provide one state right? “on”, “cleaning”
What is the default for off then?

sry, i still don’t get it.

Yeah, so when it’s cleaning the switch will show on and when it’s not the switch will show off :man_shrugging:

1 Like

And (to what Marc said) …
Your off commands it to return to base so it’s not cleaning anymore

The title of this topic is wrong, it should be about template switch not templates

 - platform: template
    switches:
      amp:
        friendly_name: "Amp"
        value_template: '{{ is_state("switch.amp", "on") }}'

So in this case the switch would just mimic it’s own state?

ok, i got it, it’s actually super simple, don’t know what was wrong here.

1 Like