Creating and using a boolean variable (state?) in a flow

I’ve searched around for help with this and found some references to similar problems but I was unable to understand them. I was hoping that if I describe precisely what I want to do, one of you smart people will be able to help :).
I have Tesla Powerwalls. Due to the way they are connected to my house, they will not power my AC outdoor units when the grid is out. I would like a flow that will detect the grid outage and then tell my Ecobee thermostats to turn off (so that I don’t bother blowing air that is not being cooled). Then, when the grid comes back, I want to turn the AC back on.
Here’s how I want the flow to work:

  1. If the grid goes down and the thermostats are set to “cool”, then turn off the thermostats. I don’t want to turn off the thermostats if they are set to “off” or “heat” since there’s no need in these cases.
  2. If the thermostats were turned off as a result of #1 above, I want to turn them back to “cool” when the grid comes back. I don’t want to just set the thermostats to cool unconditionally since they may have been previously set to “off” or “heat”.

If I look at this from a programming perspective, it feels like I would want to set some variable if #1 happens, then condition #2 happening only if #1 happened first. I’m using the events:state and current state nodes to detect when the grid goes down while thermostat is set to cool. This successfully does #1. Now, I’m stuck as to how to condition turning AC back on.

If there’s an entirely different way to accomplish this, lay it on me :). I’m still getting used to this message passing paradigm of node red.

Attached is a picture in case it helps. Ignore the “heat at” text below the current state node…I’m testing with the thermostat set to heat because it’s March :).

You may need to use a function node to store some flow or global values.
https://nodered.org/docs/user-guide/context

Thanks.
Yeah, that does look like it might work. I didn’t realized this was called “context”. Makes sense, of course :).

Ok, got this to work without using the function node or writing any javascript. I just used the change node to save context and the switch node to make a decision on it. The flow is attached. Thanks for the help.