Odd behaviour with Template Switch

Ive noticed a random behaviour with template switches, when turned on they TURN OFF after a random period of time. They DO NOT turn on if in the off position.

Can someone explain in plain english what

value_template: "{{ is_state( 'switch.shelly_8' , 'on') }}" actually does???

Why ‘on’???

switch:
  - platform: template
    switches:
      sw1:
        value_template: "{{ is_state( 'switch.shelly_8' , 'on') }}"
        turn_on:
          service: switch.turn_on
          target:
            entity_id: switch.switch.shelly_8
        turn_off:
          service: switch.turn_off
          target:
            entity_id: switch.switch.shelly_8

My goal is to reliably mimic a switch with a template switch (like a light switch that has a local switch)

The basis of this is from my earlier post

https://community.home-assistant.io/t/template-so-that-input-bool-and-shelly-switch-mimic-each-other/585635

You have an extra switch. in each of your service calls, which is why it’s not working.

          target:
            entity_id: switch.switch.shelly_8
remove this ------------------^^^^^^^

As per the docs, the value_template sets the state of the switch. Your code:

value_template: "{{ is_state( 'switch.shelly_8' , 'on') }}"

returns true if the state of switch.shelly_8 is 'on', otherwise false.

typo sorry…

It actually looks like this

switch:
  - platform: template
    switches:
      test_template_solenoid:
        value_template: "{{ is_state( 'switch.shellyplus1_80646fdaca0c_switch_0' , 'on') }}"
        turn_on:
          service: switch.turn_on
          target:
            entity_id: switch.shellyplus1_80646fdaca0c_switch_0
        turn_off:
          service: switch.turn_off
          target:
            entity_id: switch.shellyplus1_80646fdaca0c_switch_0

Its weird - will randomly turn off when in the on state, but wont randomly turn on in the off state.

I have checked the shelly device on the shelly app and there are no issues (connection etc)

So that’s the real code now?

Can you operate the Shelly switch directly without delay? Does the physical switch also turn off after the random time, or just the template switch? Is there anything in the logs at the time of the spurious switching?

Try removing the value_template line altogether and see if that works. The template switch might not report the correct value but the switch itself should stay on.

That means your switch is going unavailable. The template is specifically looking for the ‘on’ state, so if it’s flip flopping to off, that means it’s not getting an 'on' state from the source sensor. Switches only have 4 states: ‘on’, ‘off’, ‘unknown’, ‘unavailable’. So your source switch is going to one of the states that’s not ‘on’ at some point making your template switch turn off.

OK I think I must be the DUMBEST person on the face of the earth and am almost too embarrassed to say …

Full Transparency!

10 days ago I spun up a DUPLICATE instance of my dev HA on a Rpi4 (I think Id had too many beers beforehand and had some “brilliant idea”) I then proceeded to connect it to my router and then placed it in my IT cupboard (same subnet etc etc), and then promptly forgot about it until about an hour ago.

The PI also had the same devices on it and obviously it was ALSO controlling the same device but with slightly different automation parameters to my dev instance.

And thats how it was exhibiting odd behaviour.

Sorry to have wasted everyones time.

1 Like

Username checks out…

Thanks for fessing up — it can be harmful to the community and the perceived reliability of HA when problem threads are left unresolved, so it’s great to hear that it was amusing user error :+1:.