Value Template Error

I have this value_template:

value_template: >-
        {{ is_state('binary_sensor.tv_box','on') }}"
        {{ states("binary_sensor.tv_box") }}
        {% else %}
        {{ states("input_boolean.tv_box_status") }}
        {% endif %}

but i get this error:

Invalid config for [switch.template]: invalid template (TemplateSyntaxError: Encountered unknown tag 'else'.) for dictionary value @ data['switches']['tvbox']['value_template']. Got '{{ is_state('binary_sensor.tv_box','on') }}" {{ states("binary_sensor.tv_box") }} {% else %} {{ states("input_boolean.tv_box_status") }} {% endif %}'.

what is wrong ?

try:

    {% if is_state('binary_sensor.tv_box','on') %}
    {{ states('binary_sensor.tv_box') }}
    {% else %}
    {{ states('input_boolean.tv_box_status') }}
    {% endif %}
1 Like

Thanks a lot…

But in this way i have that sensor always on also if it’s off at the moment. How to check if it’s off and turn it on and viceversa?

My full switch is the following:

- platform: template
  scan_interval: 5
  switches:
    tvbox:
      friendly_name: 'TV Box'
      value_template: >-
        {% if is_state('binary_sensor.tv_box','on') %}
        {{ states('binary_sensor.tv_box') }}
        {% else %}
        {{ states('input_boolean.tv_box_status') }}
        {% endif %}
      turn_on:
        - service: switch.broadlink_send_packet_192_168_1_19
          data:
            packet: 
              - "JgAYAZOVEjgQOhE4EhMSExEUEBURFBE4ETkRORETERQSExITERQRFBE4EhMSExEUERQRFBETEjgSExE5ETgRORE5ETgSOBEABgWUlBI4EjgRORETEhMSExEUERQROBI4EjgRFBETEhMRFBITERQROBITERQRFBEUERQRFBE4EhMRORE4EjgRORE4EjgRAAYFk5URORE4EjgRFBEUERQRExEUEjgRORE4EhMSExEUERQRFBETEjgRFBEUERQRExITEhMSOBEUETgSOBE5ETgSOBE5EQAGBJSVETgSOBE5ERQRExITEhMSExE5ETgSOBEUERQRExITEhMSExE5EBQSExITEhMRFBEUETgSExI4ETgSOBI4ETgRORIADQU="
        - service: input_boolean.turn_on
          data:
            entity_id: input_boolean.tv_box_status
      turn_off:
        - service: switch.broadlink_send_packet_192_168_1_19
          data:
            packet: 
              - "JgCMAJSUFDYTNxI4EhITEhMSExITEhE5EjcTNxITEhMSEhMSExITEhI4ExETEhITExITEhITETgTEhM3EjgSNxM3EzcSNxQABgKTlRQ2EzcSNxQRFBETEhITEhITNxQ2EzYUERMSFBETEhMSEhITNxQRExITEhITEhIUERM3ExITNhQ2FDYTNhQ2FDYTAA0FAAAAAAAAAAAAAAAA="
        - service: input_boolean.turn_off
          data:
            entity_id: input_boolean.tv_power_status
      icon_template: mdi:android

if 'it’s" points to the tv_box and you want to check if the box is on, try:

value_template: >
  {{ is_state('binary_sensor.tv_box', 'on' }}

see my example:

switch:
  - platform: template
    switches:
      mode_developer:
        friendly_name: Developer mode switch
        value_template: >
          {{ is_state('input_select.mode', 'Developer') }}
        turn_on:
          - service: script.mode_developer_direct
        turn_off:
          - service: script.mode_normal_direct

Ok, but what to do after this check? I want to switch on if it is off and off if it is on…
The entire switch i posted, is correct for doing this or not?
Also doing this i get:

Error rendering template: TemplateSyntaxError: unexpected ‘}’, expected ‘)’

you’d have to try :wink:

the Value_template is there only to ‘decide’ when the template switch is ‘on’, in this case when the box is ‘on’ (the binary_sensor is ‘on’).

with the turn_off and _on settings, you enter what the switch should do. if you turn it ‘off’ the binary_sensor should turn false and hence put the switch in its Off position.

Ah, ok, now it’s clear… so then i must act on the input_boolean value to use it in automations or scripts?

it all depends on what you want to do.

but, and this is something Ive been struggling with too, you have 3 entities with the same on/off state for this box:
an input_boolean, a template switch and a binary_sensor.

No sure on which template you have based the binary_sensor, but it might be a bit overcomplicated for you needs :wink:

The template switch has as advantage you can do other things than simply toggle between on and off i the turn_on and turn_off, and add other scripts and automations for example.

If you simply want to toggle on/off, you wouldn’t need the switch perse…

input_boolean and automation could read:

  - alias: 'set tv box status'
    id: 'set tv box status'
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_boolean.tv_box_status
    condition: []
    action:
      service_template: >
        script.tv_box_{{ is_state('input_boolean.tv_box_status')}}

script.tv_box_on:
  alias: Tv box on
  sequence:
    - service: switch.broadlink_send_packet_192_168_1_19
          data:
            packet: 
              - "JgAYAZOVEjgQOhE4EhMSExEUEBURFBE4ETkRORETERQSExITERQRFBE4EhMSExEUERQRFBETEjgSExE5ETgRORE5ETgSOBEABgWUlBI4EjgRORETEhMSExEUERQROBI4EjgRFBETEhMRFBITERQROBITERQRFBEUERQRFBE4EhMRORE4EjgRORE4EjgRAAYFk5URORE4EjgRFBEUERQRExEUEjgRORE4EhMSExEUERQRFBETEjgRFBEUERQRExITEhMSOBEUETgSOBE5ETgSOBE5EQAGBJSVETgSOBE5ERQRExITEhMSExE5ETgSOBEUERQRExITEhMSExE5EBQSExITEhMRFBEUETgSExI4ETgSOBI4ETgRORIADQU="

  script.tv_box_off:
    alias: Tv box off
    sequence:
        - service: switch.broadlink_send_packet_192_168_1_19
          data:
            packet: 
              - "JgCMAJSUFDYTNxI4EhITEhMSExITEhE5EjcTNxITEhMSEhMSExITEhI4ExETEhITExITEhITETgTEhM3EjgSNxM3EzcSNxQABgKTlRQ2EzcSNxQRFBETEhITEhITNxQ2EzYUERMSFBETEhMSEhITNxQRExITEhITEhIUERM3ExITNhQ2FDYTNhQ2FDYTAA0FAAAAAAAAAAAAAAAA="

or of course, in the form of the switch:

switch:
  - platform: template
    switches:
      switch_tv_box:
        friendly_name: Developer mode switch
        value_template: >
          {{ is_state('binary_sensor.tv_box', 'on' }}
        turn_on:
          - service: script.tv_box_on
        turn_off:
          - service: script.tv_box_off
1 Like

I used this binary_sensor:

- platform: ping
  name: TV Box
  host: 192.168.1.38
  scan_interval: 2
  count: 2

Otherwise how to know if the TV Box is On or Off?

I must use input_select or also input_boolean?

sorry, my bad, copy-past error… input_boolean.

Ok i got all working now… thanks a lot…

1 Like

cool. what is your final code? care to share?

need some extra customization maybe?

homeassistant:
  customize:
    input_boolean.tv_box_status:
      templates:
        icon: >
          if (state === 'on') return 'mdi:television';
          return 'mdi:television-off';
        icon_color: >
          if (state === 'on') return 'rgb(251, 210, 41)';
          return 'rgb(54, 95, 140)';

Wow, sounds great… i just copied and pasted your configurations… thay are very good to see and working…

1 Like

cool, glad you got it working and I could be of assistance to you