While loop in value_template in template switch

is it possible to place a while loop in a value_template in template switch?
If yes, how?

You start with this:

why? did I something wrong?

The Jinja2 templating language support a for-loop.

The purpose of a Template Switch’s value_template is to report if the switch is currently on or off. How do you want to use a loop to perform that function?

I want to make a blinking/flashing effect :smiley:

How do you propose to do that with a Template Switch’s value_template?

while some state true: return true, delay, return false, delay, …

I was wondering it something like this is possible, if for loop is possible for value_template

You have misunderstood the purpose of a Template Switch’s value_template.

A Template Switch’s turn_on and turn_off options are used for controlling a device. The value_template is for reporting the device’s state.

I didn’t misunderstood it … I want to use it for a flashing effect.

If you think you can use a Template Switch’s value_template to make it blink then you have misunderstood how it works.

You asked a question in such a way that it will definitely lead to follow up questions.

If you expected us to just answer with yes or no then the post is fine. But does not seem to be what you want.

The classic x-y problem.

Tell us what you are trying to accomplish (in detail) rather than asking how to implement your (incorrectly) chosen solution.

I created a template switch and used that as a entity for button card, because with that button I am controlling the gate. So I can change the icon for the button when the gate is closed and when it is open.
Next I would like to change the state of the template switch to ON/OFF every x ms when the gate is closing or opening. So I want to have a flashing effect on that button. And I don’t want to use the custom cards, because I don’t understand them and I am not a fan of using 3rd party solutions. Maybe I will need in the future, but for now I would like to stick to clean and simple enough solutions.

As far as I know the minimum time between icon changes is about one second.
I have seen animated icons that some use, perhaps that is something you could have?

The icon will change only when opened or closed … that’s x seconds … the state (button turning gray a yellow) will change every x ms or even 1 second … doesnt really matter how fast it will blink.

is it possible for a script to be the value template? Something like: value_template: “{{ my_script }}” ?

I believe you can use a counter or boolean that you toggle every second.
This boolean will then be used to determine which icon to display.

toggle how? by a script? automation?

I would use the button to open close the gate trigger s script that runs for how ever many seconds you need.

repeat: x times
   toggle boolean
    Delay 1 second.

ok, thank you