Create a constant

whats the best way to create a constant?
ie: in climate i want to set the temperature as one constant value, then refer to that constant entity_id in all my other scripts and automations. so i when i want to change it, its only in one spot.

Not sure this would work, but can you use a !secret to refer to a value that you store in the secrets file?

Not sure you can use !secret in a template, but worth tryingā€¦
Or maybe just a template sensor that you load with a number.

What about using an input slider? Couple that with templates, and you should be able to achieve what youā€™re after.

How about a command line sensor that reads the value from a file.

You could then change the value in the file without having to restart HA.

2 Likes

@stone did you find a way to do this - if yes can you share what you ended up implementing?

I have a similar need: I want to be able to create a simple constant at the top of my configuration.yaml that I will later refer to multiple times.

Example:

weekday_wakeup_time = "06:30:00"
weekend_wake_time = "07:30:00"

Then I have multiple automations that would use these constants e.g. in ā€œatā€ triggers, and also notifications etc.

Currently I have to repeat 3-4 times the same ā€˜constantā€™ value, once in each automation, and inside some notifications, and every time I need to change it, I need to touch 3-4 places, which is an issue (easy to miss some of the instances).

Any advise on how to handle this use case would be great - thanks

1 Like

i ended up using the secrets.yaml to store my constants. then just use the !secret some_constant in my other yamls.

4 Likes

Thanks! I had the exact same idea! and then I thought maybe there was a cleaner to handle this use case, e.g. !constant or !macro or some other similar mechanism that would allow defining this in a different file

Iā€™d be surprised there hasnā€™t been an ask to support this use case before, I still have some hope there could be a better way. Maybe using Jinja but without having to use templates everywhere and lots of overhead for a simple thing

If not better idea comes up I will give the !secrets approach a shot

You could just create a sensor,
feed the sensor the value you want and off you go
To be honest Iā€™d just use an input_number and if you donā€™t trust your user base set the low limit to equal the high limit.
That way the way I use variables is standardised.

Both would require a restart though if you ever needed to change the value.

@Mutt thanks. I see - in my use case I could use an input_text sensor to store the text string.

I see other people explored the same, for reference:

The drawback would be that Iā€™d have to change all my automation time triggers to something more cryptic like

   trigger:
      platform: template
      value_template: "{{ states('input_text.weekday_wakeup_time') == states('sensor.time') }}"

The resulting config will some extra boilerplate but this approach has much more potential. I.e turn on the coffee machine 10 minutes in advance, turn on/off some lights a few minutes after, etc

FWIW I ended up using two input_datetime variables, with the thought that they are easier to expose as ā€inputs" in the GUI (as opposed to input_text), which I also ended up doing.

This is how the triggers look like:

trigger:
      - platform: template
        value_template: '{{ states.sensor.time.state ==
states.input_datetime.weekend_wakeup_time.state[0:5] }}'

In Messages (GUI notifications, emails) the same approach applies.

All Valid and good options to list, for the next guy to visit this thread.
In fact Iā€™m thinking about how I could use this ā€¦
Cheers

Iā€™ve been using this and it works AWESOME.

You can easily create variables and update as you see fit - and set them to be ā€˜rememberedā€™ across restarts.

1 Like

Still getting my feet wet in HA but it seems that this is a specific version of a very general problem with Home Assistant, namely duplication of literal values throughout the configuration files. Iā€™m hoping Iā€™m wrong and that there is a good general solution to this, but Iā€™m finding that if I want to do something that sets or depends on (eg) the current activity in my Harmony Hub, or the current source on my Vizio, or the current input on my receiver, or the Spotify source list, Iā€™m doing it based on a string literal. And it this seems pretty fragile to me. Am I missing something?

1 Like

Happy to try to help, but would need more information.

HA is really based on setting up integrations (which youā€™re doing it sounds like) and then leveraging Automations and/or Scripts to ā€˜work withā€™ these integrations to do things.

Being able to grab ever-changing values of different entities w/in various integrations is the heart of automating TBH.

Personally Iā€™ve been running HA for almost a year and, beyond some breaking changes w/ version upgrades and underlying integrations changing APIs and the like, Iā€™ve found HA to be extremely stable and reliable. If you have an example or something more specific - the community is typically happy / eager to help.

1 Like

Thanks for the help. Letā€™s dig into one of these examples.

I have a template sensor that returns the value of my Harmony Hubā€™s current activity. This lets me do lots of things based on the current activity.

For example, I have a vertical stack card which includes picture-glance card for my basement media setup; it has a state_image that is based on the current activity. I have other cards in the stack that are conditional on the current activity, eg to put up the right buttons for that activity. These conditions are all based on the string returned by the Harmony - like ā€œWatch TVā€ for example. Thatā€™s the string that literally appears in the Harmony app.

Now imagine that I decide to change the string that appears on the Harmony app to be longer, shorter, friendlier, whatever. Maybe my wife says that ā€œWatch TVā€ is confusing because itā€™s all TV - it should say ā€œWatch Fios.ā€

Iā€™m pretty sure that if I do this, all of my code breaks. I think there ought to be a table or something that maps between the user facing strings and state values that I use in the code. I hope thereā€™s a way to do this.

(I had a thought for how to put this indirection in the sensor itself with a bunch of conditionals, but that seems kludgey.)

Does that make sense?
~Steve

So, you change a state value to something else. And you suggest a state value should have a state value uid to always stay the same no matter what value you decide to give it? This sounds logical, but the question is, what is more work to maintain?

Well the specific problem here is that the state values and the user presentation of the state values are the same. Entities have an entity_id that is distinct from the friendly name. But certain states (Harmony activity names, and I think the input names on tvā€™s and receivers, for example) have IDs that are the same as the user facing names. I feel like there are other places where Iā€™ve encountered this situation in HA but canā€™t recall where.

And honestly, typing YAML thatā€™s the equivalent of "if x == ā€œSome Stringā€ just seems wrongā€¦

Thank you for elaborating. Though again I ask myself, what would be more time consuming?

You could create duplicate template sensors, one w/ your ā€˜prettyā€™ name and another with ā€˜staticā€™ names and leverage the latter for your conditions and such. You could also just get a text editor like Atom and do find/replace in all files. You could also tell your wife that sheā€™s just has to live with whatever name you give the statesā€¦ :man_shrugging:

1 Like

@frits1980 - if there are lots of scripts that depend on those state names then it would be way easier to make the change in one place - where the mapping is established - than everywhere. This kind of indirection is, of course, common in many systems.

@Markus99 - your suggestion was exactly my thought for how to put this indirection in the sensor itself with a bunch of conditionals. I think itā€™s a bunch of if statements like ā€˜if the state is ā€œPretty Nameā€ then the sensor value is symbolic_nameā€™ which is an odd form of a map.

I think what Iā€™m starting to realize is that while HA is amazing in its scope and flexibility, the YAML decision is weird. Itā€™s may be too technical/geeky for ā€œlay peopleā€ and not as expressive as something imperative or procedural.

I may also look into YAML templating solutions; wondering if anyone has applied these in HA?

1 Like