I would think you should set the global variable in the turn_on_action.
I’m confused on the intent of all this however. The state of your switches can only be true or false. It seems like this would all function better as a user defined service for which you can pass a parameter.
So i have 6 sprinklers , i want to make an automation to run each one of them for 5 or 10 minutes . Due to logistical issues i decided to go with your suggestion and build it inside esphome via native api . So i created a service that runs them one by one with an interval param. This is working fine but i would like to be able to also stop the service if i want . This is the api:
Unfortunately, I think you need to create a script for which you can check if it is running and stop it. I would personally create separate scripts for different time intervals which makes things much easier. You could consider adding a feature request for adding passing variables to scripts in esphome, I if it isn’t requested already.
Thatbis: You could still use the service to call it from home assistant, but the service first stops all running scripts than runs your script. Unfortunately, if you really want one script, you’ll still need global variables.