Template switch with value template device tracker to turn on TV via broadlink IR turns TV off after 10 sec

Hello!

I have a template switch with this config:

platform: template
switches:
  salora:
    friendly_name: "Salora"
    value_template: >-
      {% if is_state('device_tracker.salora', 'home') %}
        on
      {% else %}
        off
      {% endif %}    
    turn_on:
      service: script.turn_on
      data:
        entity_id: script.tv_power
    turn_off:
      service: script.turn_on
      data:
        entity_id: script.tv_power
    icon_template: >                                                                                                                                 
      {% if states.device_tracker.salora.state %}                                                                                     
        {% if states.device_tracker.salora.state == home %}                                                                             
          mdi:monitor                                                                                                                                                                                                                                                          
        {% else %}                                                                                                                                   
          mdi:monitor                                                                                                                        
        {% endif %}                                                                                                                                  
      {% endif %}

Then i have the broadlink IR command (which is a toggle):

 tv_power:
  alias: 'Power'
  sequence:
    - service: broadlink.send
      data:
        host: !secret broadlink_host
        packet:
          - JgAwAB4cOxweHB4cHxsfODwbHzkeHDscHgALqB8bPBsfGx8bHxsfOTscHjkfGzwbHwANBQAAAAAAAAAA

Now it all works as expected, but the TV turns off after around 10 seconds. Is this due to the device tracker taking a while to show the TV as online? I’ve tried adding a - delay: 00:00:20 to the script sequence but that doesn’t work.

Any advice?

When your TV device tracker is reporting not_home and you turn your switch on, does it stay on or does it return to the off position?

I am wondering if the value_template evaluates the TV as being not_home, flips back to off position and triggers the turn_off script

That was my guess aswell, and indeed it switches back off (template switch). Thats why i added the delay in the turn on command, but that obviously doesnt really work.

Should i substitute the value template with a inputboolean which on device tracker away turns off and on in the script?

Edit:

    value_template: >-
      {% if is_state('input_boolean.salora', 'on') %}
        on
      {% else %}
        {% if is_state('device_tracker.salora', 'not_home') %}
          off
        {% endif %} 
      {% endif %}

With a script:

tv_power:
  alias: 'Power'
  sequence:
    - service: input_boolean.toggle
      data:
        entity_id: input_boolean.salora  
    - service: broadlink.send
      data:
        host: !secret broadlink_host
        packet:
          - JgAwAB4cOxweHB4cHxsfODwbHzkeHDscHgALqB8bPBsfGx8bHxsfOTscHjkfGzwbHwANBQAAAAAAAAAA

And an automation:

alias: Indirect salora on
hide_entity: true
initial_state: 'on'
trigger:
- platform: state
  entity_id: device_tracker.salora
  from: 'not_home'
  to: 'home'
action:
  - service: input_boolean.turn_on
    entity_id: input_boolean.salora
alias: Indirect salora off
hide_entity: true
initial_state: 'on'
trigger:
- platform: state
  entity_id: device_tracker.salora
  from: 'home'
  to: 'not_home'
action:
  - service: input_boolean.turn_off
    entity_id: input_boolean.salora

I’d try that at least to rule out that initial thought.

Still the same issue… can’t seem to figure it out.

A Template Switch’s value_template should return true or false and not on or off.

Replace this:

    value_template: >-
      {% if is_state('device_tracker.salora', 'home') %}
        on
      {% else %}
        off
      {% endif %}  

with this:

    value_template: "{{ is_state('device_tracker.salora', 'home') }}"

Great, nothing else? just that line? Will report back.

I didn’t inspect everything you created, just the value_template portion which doesn’t report true/false.

After you make the change and test it, report back with your results and we’ll take it from there.

Ok Iv’e tried:

platform: template
switches:
  salora:
    friendly_name: "Salora"
    value_template: >-
      {% if is_state('device_tracker.salora', 'home') %}
        true
      {% else %}
        {% if is_state('device_tracker.salora', 'not_home') %}
          false
        {% endif %} 
      {% endif %}    
    turn_on:
      service: script.turn_on
      data:
        entity_id: script.tv_power
    turn_off:
      service: script.turn_on
      data:
        entity_id: script.tv_power

And:

platform: template
switches:
  salora:
    friendly_name: "Salora"
    value_template: "{{ is_state('device_tracker.salora', 'home') }}"
      service: script.turn_on
      data:
        entity_id: script.tv_power
    turn_off:
      service: script.turn_on
      data:
        entity_id: script.tv_power

Both don’t work. Also this doesn’t work, still the same sings

    value_template: >-
      {% if is_state('input_boolean.salora', 'on') %}
        true
      {% else %}
        {% if is_state('device_tracker.salora', 'not_home') %}
          false
        {% endif %} 
      {% endif %}

Edit:
When i flip the switch.salora it turns on for about 5 seconds, then switches off, and then on again once the device tracker is online (which then turns it off, i guess)

Did you try the value_template I had suggested?

Never mind. It will never work correctly and here’s why:

The purpose of a Template Switch’s value_template is to report its current state. When you turn it on, the value_template receives feedback acknowledging the actual device was turned on.

If it fails to receive the feedback, it puts the switch back to its original state (if you turned it on, it will turn it back off).

Your value_template is monitoring the state of a device_tracker that has absolutely nothing to do with the TV you are turning on. As a result, any command the Template switch sends (turn on or off) results in no feedback returned to the value_template. So the switch reverts to its previous state.

Yes, see the config in my previous post. I’ve tried your suggestion, but still nothing. Also tried adding true/false

Edit:

Yes, see my edit in the previous state, thats what i suspected… to bad. I’ll look for an alternative.

Maybe, use the input boolean as the primary switch that reflects to the switch.salora state (which has the input boolean as the value template?)