How to do things better in Home Assistant - A question about variables, custom functions, constants

Hi All,

I’ve done plenty of amateur level PHP programming for personal projects and one thing I like to do is that when it makes sense to do so, to write a custom function and then re-use that in lots of places to avoid code duplication and to so simplify things when I want to make a change, having to do it only in one place.

  1. I am using the Pushover notification integration where I specify what device the notification goes to. I get a new phone each year through work and I know that for all my notifications I will have to go an update it in all my automations. Is there a way to set a constant or define variable somewhere central in the system and be able to use that in my automations so that I change it in only one place and not in all automations?

  2. I am finding all of my notfications are almost using the same YAML code that I am pasting in over and over but obviously in some cases things are different. Is there are way for me to create a custom function that accepts parameters that are stored in one location that i could call from the automations?

Thanks,
Adrian

  1. Yes. Create a notification group (even if it is only one phone). Use the notification group in your automations. Then you only have to change which devices are in the group. https://www.home-assistant.io/integrations/group/#notify-groups

  2. Yes. You can pass variables/data to scripts.

Regarding getting a new device: You would just rename the device and entities in HA to match the old names.

Global state is typically achieved by using one of the suitable input helpers.

For reuse, it depends: you can define scripts in YAML that can be invoked front the UI or automations. These can take parameters. For reuse of templates, Jinja templates are supported. There are also YAML anchors to reuse (reference) YAML within the same file.