Making a Template Switch self aware

I have a template switch that works sort of. It shows as on, in the front end just as it should but it doesn’t seem to know it’s real state.
If it’s on and I have an automation try to turn it on it goes off.
This is the code:


switch:  
  - platform: template
    switches:
      v_switch_1:
        unique_id:  houselightsfifteen
        friendly_name: "Back Porch"
        value_template: "{{ is_state('sensor.mqtt_sensor', 'on') }}"
        turn_on:
          service: switch.toggle
          data:
            entity_id: switch.back_porch_lights
        turn_off:
          service: switch.toggle
          data:
            entity_id: switch.back_porch_lights

Anyone know how to make it self aware, i tried condition but couldn’t ever get it to work.

Thanks
Wayne

The value_template is what determines the Template Switch’s current state.

If you turn on a Template Switch and it immediately sets its state back to off, it means the value_template failed to report the correct state.

There has to be a direct correlation between the state of switch.back_porch_lights and the state of sensor.mqtt_sensor. When back_porch_lights is on then mqtt_sensor must report on as well. If the two have no such correlation then mqtt_sensor is not a good choice to represent the Template Switch’s state.

This is normal if you use switch.toggle for turn_on and turn_off.

It does change it’s state when turned on or off but it does take a few seconds.

The Template Switch expects the value_template to report state-changes much faster than “a few seconds”.

You may be better off excluding the value_template and having the Template Switch operate in optimistic mode.

The problem is that the lights these switches control are also controlled by a push button wall switch. So I use voltage as a way of telling the switch if its on or off.
I will see if I can use optimistic.
Thank you

The only way to make the Template Switch report the correct state (‘self aware’ as you called it) is via its value_template. If it reports the incorrect state, or reports it too slowly, the Template Switch won’t be ‘self aware’.

The other source of potential confusion is that the turn_on and turn_off services both rely on the same service call: light.toggle. Effectively, nothing actually sets a state but makes an assumption about the light’s current state and simply toggles it. For example, the light bulb might actually be off but by executing turn_off it gets toggled to on. Combine that with a slow acting value_template and everything is now out of sync (and optimistic mode can’t fix that).

I tried a few things and found that it does know its state. I changed the last entity to different switch just to see what happened and it did go the correct one. if it was possible to put a conditional statement before each turn_ on and Turn_off it could be made to work.

       turn_off:
          if sensor.mqtt_sensor is "on"
          service: switch.toggle
          data:
          entity_id: switch.back_porch_lights

Is there anyway to do this?

What did you change to make it work? Previously you said it took several seconds for sensor.mqtt_sensor to report a state-change. That was too slow for the Template Switch to correctly report its state. However, now you say it works so what did you do to correct it?

You can’t do that in the Template Switch but you could make turn_on and turn_off call a script. The script can contain conditions.

Ok thanks I’ll try that.

I configured the script and it works but I’m having problems calling the script.
Here is what I have and it doesn’t work.

switch:  
  - platform: template
    switches:
      v_switch_1:
        unique_id:  houselightsfifteen
        friendly_name: "Back Porch"
        value_template: "{{ is_state('sensor.mqtt_sensor', 'on') }}"
        turn_on:
         service: script.turn_on
         data:
         entity: script.turn_15_on
        turn_off:
         service: script.turn_on
         data:
         entity: script.turn_off_15

Any ideas will be appreciated.

The indenting is incorrect for all lines containing service, data, and entity_id.

In addition, you used the word entity instead of entity_id.

Refer to the documentation to see a properly indented example:

After working on this and not getting anywhere, I got to thinking that the scripts did what I needed in automations and the regular switch template works great on the front end so I’m going to just do it that way until my head need to be beat against the wall again.
This is the final code that configuration will accept but doesn’t do anything.

  - platform: template
    switches:
      v_switch_1:
        unique_id:  houselightsfifteen
        friendly_name: "Back Porch"
        value_template: "{{ is_state('sensor.mqtt_sensor', 'on') }}"
        turn_on:
          service: script.turn_on
          data:
            entity_id: script.turn_on_15
        turn_off:
          service: script.turn_on
          data:
            entity_id: script.turn_off_15

Thanks

I’m a crack head, I had the script named wrong. It all works as it should.

Could you please paste your scripts? Thank you.

Here you go.

switch:

  • platform: template
    switches:
    v_switch_1:
    unique_id: houselights1
    friendly_name: “v1”
    value_template: “{{ is_state(‘binary_sensor.binary_sensor_1’, ‘on’) }}”
    turn_on:
    service: script.turn_on
    entity_id: script.turn_1_on
    turn_off:
    service: script.turn_on
    entity_id: script.turn_1_off

    v_switch_02:
    unique_id: houselights02
    friendly_name: “v02”
    value_template: “{{ is_state(‘binary_sensor.binary_sensor_2’, ‘on’) }}”
    turn_on:
    service: script.turn_on
    entity_id: script.turn_2_on
    turn_off:
    service: script.turn_on
    entity_id: script.turn_2_off