I’m new to HA but not new to smart-home automation and just starting to write my first few HA automations. In a couple instances I would like to set a value for a variable as an action in an automation. The objective is to be able to then use the variable in other places in HA.
In the automation I am working on at present I want to use the variable to hold a text string which I hope to use on dashboards. In this case a sting of text used to give a custom greeting to a user as the title block of a dashboard.
For example my automation determines that my son forgot to run the dishwasher before leaving the house so when he returns I want his dashboard greeting to read “Don’t forget to start the dishwasher John.”
I have all the triggering and logic worked out but I am stuck on how to use the “Define Variable” action. I’ve been going over documentation and forum posts but so far I have not found the answer. In this case I want to create a variable called Greeting_John and set its value to the string “Don’t forget to start the dishwasher”
In the automation I selected the action “Define Variable” and I am presented with this YAML editor view:
I have found documentation on variables and several posts that discuss there use in automation. But I haven’t found a specific reference to the syntax needed for this Automation Action. I’m hoping a simple example of the correct syntax needed to create a string variable that can be used elsewhere in HA will help address this and spring board me into several other variations and uses of variables.
Any assistance or reference links would be greatly appreciated.
Set up a helper (input_text or whatever), then in the automation assign a value to it. In later automations you can then refer to the value. You could also do this with template variables in configuration.yaml, the main difference being, the helpers will survive HA restarts and host reboots.
@tom_l Thanks!
In the automation that seems very straight forward. Unfortunately, for a newb like me it just moved the knowledge deficit further downfield.
I was planning to use a pair of variables {{ greeting_john }} {{ user }} to create the text string on the dashboard heading.
But, switching to input_text.greeting_john I can’t figure out the syntax needed to generate the text string on in the dashboard card.
variables:
greeting_john: Don’t forget to start the dishwasher
I don’t know if I oversimplified it but over on the dashboard I get an undefined error when I try to use the variable. Do I need to do something to set the scope in any way?
As tom_l said before, your best bet is to use a Helper. Storing data for the user is exactly what Input helpers, like the Input Text that Tom recommended, are for.
In HA, YAML Variables only have local scope… in this case since you have defined it in the Action block of an automation, it only has value inside that block, in that specific automation… it will never be available in a dashboard card.
It is possible to define custom macros and use them kind of like a global variable.
Finally, be aware that most dashboard cards do not accept templating, so the usefulness of the custom macro method will depend on the card type you are using… We could likely provide help that is more specific to the situation at hand if you provide more details.
Thanks! So all that’s left to do is figure out how to expose the helper value as part of a text string in a dashboard markdown card. I’m looking at a reference page now that talks about how the card can include code but that’s as far as I’ve gotten.
Any further pointers would be greatly appreciated.