Template Sensor feedback delay

Hi,

I’ve got a TV. I turn it on using a switch (Broadlink RM mini 3) and then I check a z-wave wall plug, and if the power draw is more than 100W I assume the TV is on. This is the config:

- platform: template
  switches:
    tv_power:
      friendly_name: "LG TV"
      value_template: "{{ (states.sensor.wallplug_tv_power.state | float) > 100  }}"
      turn_on:
        service: switch.turn_on
        data:
          entity_id: switch.tv_lg
      turn_off:
        service: switch.turn_off
        data:
          entity_id: switch.tv_lg

My problem is this: because there is a delay between the switch turning the TV on and the wall plug reporting the power usage, first the switch indicator turns on when I click it, then it turns back off because the reported power usage doesn’t indicate that it is on, then a second later it goes back on because the power usage is updated. Is there a way to increase the delay before the switch indicates that it didn’t turn on?

1 Like

I browsed the code a bit and, honestly, I didn’t try to fully understand it. But I have an idea that may or may not work. Try adding a delay to the turn_on and turn_off actions:

- platform: template
  switches:
    tv_power:
      friendly_name: "LG TV"
      value_template: "{{ (states.sensor.wallplug_tv_power.state | float) > 100  }}"
      turn_on:
        - service: switch.turn_on
          entity_id: switch.tv_lg
        - delay: 2
      turn_off:
        - service: switch.turn_off
          entity_id: switch.tv_lg
        - delay: 2

My theory is, the template switch’s state will update whenever the entity referenced in the value_template changes. But, it must also update in response to the switch.turn_on and switch.turn_off service calls (i.e., changing to ‘on’ after turn_on finishes, and to ‘off’ when turn_off finishes.) So, I’m guessing (hoping :slight_smile:) that by adding the delays will cause that state change in response to the service calls to be delayed. If it delays long enough for the value_template to agree with it, then that will hopefully solve your problem.

Hi There

I am attempting the same thing, im using the chromecast attached to my tv to determine the state of the switch:

 - platform: template
    switches: 
      tv_living_room:
        friendly_name: Television
        value_template: "{% if states('media_player.living_room_tv' ) != 'unavailable' %}on{%else%}off{%endif %}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.tv_samsung
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.tv_samsung

I tried the above suggestion by adding the delay: 5 to the turn_on section however it didnt work. not sure how else to achieve this.

Hi Guys,

I got around this by using a template binary sensor and a script to do the control.

I’m using it to turn off my computer - when I turn it off though it sends a shutdown command first to gracefully shutdown before turning off the power to the smart plug. The way I’ve got round it is having the value_template linked to the binary sensor.

the binary sensor is on whenever the smart plug is on and the shutdown script isn’t running. so when I press the switch in the UI this runs the script resulting in it turning off in the UI and not coming back on. The script has the delay in it which prevents any false positives.

Here’s the binary sensor config:

  - platform: template
    sensors:
      computer_state:
        friendly_name: "Computer state"
        value_template: >-
          {{ is_state('switch.the_computer', 'on')
             and is_state('script.shutdowncomputer', 'off') }}

and here’s the switch config:

  - platform: template
    switches:
      computercontrol:
        value_template: "{{ is_state('binary_sensor.computer_state', 'on') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.the_computer
        turn_off:
          service: script.turn_on
          data:
            entity_id: script.shutdowncomputer

You guys should be able to adapt this method to your use case, even if all the script does is turn on the switch and run a delay.

I know I am digging up and old thread but I cannot really follow the solution provided.

I am having a similar issue to the one in this post. I use a broadlink RM mini 3 to turn on and off my A/C from HA and a Wyse sensor as a binary sensor to determine the current state (as it may already be turned on/off from the original IR remote). I am using a template switch but when I turn it on in HA the switch immediate returns to the off position until the binary sensor triggers and it then displays as on again. Is there any way to add a delay to the trigger from the binary sensor so it doesn’t go ON -> OFF -> ON everytime I turn it on?

I don’t think my understanding of how the template switch works was very good a year and a half ago, so I’d take what I said back then with a huge grain of salt. :stuck_out_tongue_winking_eye:

After re-reading the code, as far as I can tell, the state of the template switch will only change in response to the value_template rendering a new result (which only happens when the referenced entity or entities change.)

So, in your case, if the template switch changes to on, then off, then back to on, when you turn it on, then it would seem that must be what your binary sensor is doing.

Can you post your template switch and binary sensor configurations?

Hi Phil,

Thanks for your reply.

Here is my template switch:

  - platform: template
    switches:
      bed_ac_heat_22_templated:
        friendly_name: "Bedroom A/C Heat 22"
        value_template: "{{ is_state('binary_sensor.wyzesense_bed_ac', 'on') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.bedroom_ac_heat_22
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.bedroom_ac_heat_22

My binary sensor is a Wyze Contact Sensor and HA auto detected it so I don’t have it in the YAML config file.

The entity in HA for the sensor is binary_sensor.wyzesense_bed_ac
and the state attributes when I just checked it are:

mac: 77831C4D
device_class: door
timestamp: '2020-06-19T08:01:14.238000'
rssi: -74
battery_level: 86
friendly_name: Bedroom A/C Sensor

Any help would be appreciated. This is not a big problem because every still works OK - it is just annoying to see the toggle switch in HA flick back to OFF and then a few seconds later back to ON every time I turn the aircon on.

Look at the history of binary_sensor.wyzesense_bed_ac. Does it go on and off and then back on when this happens? Or look at it in the Logbook. Again, the state of switch.bed_ac_heat_22_templated will only go on then off then on if binary_sensor.wyzesense_bed_ac does.