As I’m slowly getting my feet under me with creating Automations (I haven’t even begun to start getting acclimated to Lovelace), I’ve struggled with a couple of things.
I’ve written code in a plethora of languages, Fortran, Basic, C, Python, LabView, etc. I’m not a programmer, not I’m not a neophyte either.
Truth be told, YAML has been a challenge for this old fart.
I’m noodeling through it, and one of the things I am struggling with are places to store data.
I’ve got a number of automations written, which are very sequential and would benefit having a place to toggle a bit to reference in a subsequent automation.
We’ve got a sleep number bed, which evidently came with the option to have up to four lights.
We don’t have the lights, but these four toggles have proven invaluable in my sequential automations.
I’ve looked around (a bit), but I’ve not found a way to toggle a flag like this in HA.
Is there a way to create a global variable to use in these types of purposes?
I think you’re looking for an input_boolean helper. An example of how I use this is I have one called “disable motion” that I use in every motion or occupancy automation as a conditional test before proceeding. This allows me to globally turn off or on those automations.
There are other helpers that will allow you to store, save and change state that you can use as “variables” in places where you need them.
Hi Christopher. I have a similar IT languages background, and found (still find) HA’s YAML context very challenging to work with after professional programming. Other will disagree of course.
Basically, as far as I understand, YAML does not really have Variables (that you can write to within a Script/Automation). You can declare Variables or Fields in a Script and assign a value at that point, but not update it later AFAIK - more like Constants really. (Watch your Variable scope too - not always as we might expect.)
So you have to create a “Helper” Entity of suitable type, like the input_boolean helper mentioned by Rouxdoo above. These are your Global Variables. Global across HA. Then you can assign (set) a value to it in YAML. There are multiple Helper “datatypes”. Look in HA → Settings → Devices and Services → Helpers then +Create Helper and it will give a list of types to select from. Helpers can also be created using YAML, but it’s easier using HA’s UI first. If you “open” the created entity (like any other) you can then edit its attributes more fully.
There are also ways (I’ve yet to try) to create more complex value holder entities. There have been several discussions about “creating variables” in HA / YAML. Here’s one https://community.home-assistant.io/t/how-to-use-variables-in-ha/483977 . Several older ones (if you can find them) are interesting too.
Being keen to keep my code simple, as yet I use Helpers. They (mostly) remember their state over HA power-downs. But you may need to use a HA_Start trigger to re-instate things correctly, as their state reloads quite late I think. E.g. I have a “Set Part of Day” script I run at specific times (Starttimes of Morning, Afternoon, Evening, Night). If HA is down, then I need to trigger that script at HA_Start too.