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 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.
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 .