Damn, there’s a nice thread (or a tutorial) here on template sensors and I just failed at finding it. I’ll keep looking.
They end up working like little special-purpose automations. They trigger either on a state change or on an explicit trigger (or triggers), and do what an automation does but saves the result in the new sensor’s state (and/or also in attributes).
For example you could trigger on the time of day and set the sensor the the color level you are discussing. Then in another automation use that new template sensor for the light level. It simpifies the automations that use the sensors.
They create a sensor (of some type) where you set the state and optionally attributes. They “run” either implicitly when a watched state changes or explicitly (just like in an automation) using one or more triggers.
So a very simple example might be a temp sensor that reads 5 degrees low. When the source sensor state changes you add 5 to its value and save that state.
You can run actions from a triggered template and use the results from those actions.
There’s no sharing of variables, if I read that right. They typically update their own state but there’s actions so you can update something else (although those should probably just trigger on the state change of the source).
I wouldn’t say there’s automations on each side of them. You can run actions and use data returned by actions do update state and do all the stuff you can do with templates to figure out what state to set.
I believe that is what David is saying… though the variables are only global in the local sense…
When using the trigger-based schema, there are multiple places where you can define variables that will be available to all entities configured in that block:
It is possible to share variables, hence you can do calculations once and update multiple target sensors.
I was envisioning the var_2 from your example, I hadn’t considered the other variable use cases in your example.
However it should be noted that there is nothing stopping someone from creating sensors that simply return the value of one of these variables. I wouldn’t describe such sensors as “global variables” as that implies that one could update them directly, however I would say such sensors are “global state” since they remember their value and only update when the relevant triggers fire.
Which is a more verbose way of saying my first point:
For the most part they are an advanced way of managing state.