Global Variables (House Statuses) and custom scripting

Hi all,

Pretty new to HA and trying to get my way around the automation/scene/scripting stuff.
I am used to create lua scripts or write some python code, but yaml sequences is quite new to me.

For starters I would like to set a (global) variable to hold the state of my house.
Sleeping/Home/Away/Holiday/etc. Based on those states I used to fire or hold a lot of automations.

  • How can I create and set global variables?
  • Also how to create some simple IF_ELSE, AND_OR scripts? It seems the automations are quite limited to at least different to what I know, compared to lua and python.

Use an input select:

Templates:

2 Likes

If you are familiar with Python, you can take a look at AppDaemon.

In YAML, I suggest creating input_selects for state of the house or similar things.

1 Like

Yep - input-select is the best way for storing list values between automations.

For non-list values people have used things like binary sensors (for boolean), sliders (integers) and for arbitrary strings I have used MQTT device trackers.

Serach the community for ā€œvariablesā€ - there are lots of options

is it better than a template sensor or input_text?

Just be aware, state of input_text can handle max 255 characters (or bytes?).
So use sensorā€™s attribute if you need to store more data.

And one more thing: AFAIK sensors doesnā€™t survive system restart.

any state of any state object.

yeah, hence I said template sensor.

what does that mean?

It does mean, that if you store some data in sensor, those data will be likely wiped out on Home Assistant restart.

Iā€™m not sure itā€™s distinctive term. You can fill attributes without using templates at all (using other languages, NodeRed etc)

unless itā€™s a specially designed sensor then yes. the thing is people use variables as both short- and long-term storage. for short-term storage HA restart is irrelevant. so it depends on use case.

of course itā€™s not. rather an example.

@AhmadK - for the life of me I canā€™t rememberā€¦ I started using them about version 0.6, input-text was around then so no idea why it was a better idea (if it was)

Iā€™m just curious because it seems a bit overkill to use device_tracker for storing strings :wink:
Tried to use it a couple times as device_tracker (nmap tracker to be precise) and gave up as it gave me a lot of home -> not home fluctuations and caused my wall tablet to stop charging even if I explicitly excluded its IP. Maybe itā€™s just me :wink:

Thanks for the reply all. I will have a look into all this.

Yet I do wonder why there is not a ā€˜normalā€™ code engine for languages like python or lua. Maybe something similar as the lua option in Fibaro hc2, where you can easily write your own code.

Well, itā€™s a design approach. Here you think about data that is stored in state objects and automations that react to state changes and change states as well.
As pointed out previously, your goal is not difficult to achieve, there are many examples on this forum.

Agree fully in this example, but as automation will become more complex when I start combining sensors, states, etc. some custom coding can become very handy. It just feels like this approach is somewhat restricted, but maybe I just need to spent some more time with it.

Did you take a look at AppDaemon from my first post in this thread? Itā€™s all python. I use it extensively in my setup.

I didnā€™t see a mention of it yet, but you could also take a look at hass-variables. Iā€™ve transitioned to using more input_selects recently, but still have a few things utilizing the hass-variables component.

Yeah. You can perform complex actions using python_script (just keep in mind itā€™s restricted Python so you wonā€™t get full freedom but then again, you can call a shell command or an external script (python or whatever) or use AppDaemon/NodeRed).
So itā€™s not that bad :wink: