Support environment-variable expansion in config values (e.g. ${SUPERVISOR_TOKEN} for Home Assistant add-on)
Is your feature request related to a problem? Please describe.
I run evcc as a Home Assistant OS add-on.
I feed a solar forecast that already exists in Home Assistant (Solcast, with local dampening applied) into evcc via the generic http forecast provider, instead of querying the Solcast API a second time and consuming its limited daily quota.
This works β but to authenticate against the Home Assistant API I have to manually create a Long-Lived Access Token and store it in clear text inside the evcc config. At the same time, the Home Assistant Supervisor already injects a SUPERVISOR_TOKEN environment variable into every add-on container, which can authenticate against the Core API via http://supervisor/core/api/...
I cannot use it, because config values don't seem to expand environment variables (maybe there is a way I missed?). I tried e.G.:
tariff: solar
forecast:
source: http
uri: http://supervisor/core/api/states/sensor.my_forecast
auth:
type: bearer
token: ${SUPERVISOR_TOKEN}
jq: .attributes.forecast_json
This fails with 401 (Unauthorized). The behavior is consistent with the ${SUPERVISOR_TOKEN} placeholder being sent verbatim rather than expanded.
Note: I haven't fully ruled out a second possible cause, maybe the add-on manifest's homeassistant_api permission, but the symptom points to no expansion happening.
Describe the solution you'd like
Generic environment (!) -variable expansion in config values, e.g.:
token: ${SOME_ENV_VAR}
expanded at config-load time from the process environment.
This would let add-on users authenticate via the injected SUPERVISOR_TOKEN without hardcoding any static credential (which is not really possible anyway, since the SUPERVISOR_TOKEN rotates to my knowledge).
More generally, this is useful far beyond Home Assistant: specific secret or environment-specific value β API keys, passwords, tokens for vehicles, chargers, tariffs, MQTT brokers, etc. β could be kept out of the config file and injected from the environment (e.g. Docker). This benefits anyone running evcc in Docker, Kubernetes, ... and makes life much easier to use those information.
Since this is a fully generic (and hopefully not very complicated) mechanism, no HomeAssistant-specific code is required: SUPERVISOR_TOKEN would simply be one environment variable among any others a user might reference.
If desired, the add-on could additionally be shipped with the token already exposed under a well-known variable name (or the "right one" can be mentioned in the docs), so that in the add-on case it works out of the box β but that would just be documentation/packaging on top of the same generic expansion, not special-casing in evcc itself.
Describe alternatives you've considered
-
Long-Lived Access Token hardcoded in the config: This is what I do today. Works, but requires manual token creation and stores a secret in clear text.
-
A dedicated type of homeassistant tariff/forecast provider (proposed in #27645, but closed): I understand and agree with the maintainer's position there that per-integration parsing logic isn't desirable and that HTTP is "just another transport." The generic http provider already does the job. The only missing piece is injecting the credential from the environment instead of hardcoding it, i.e. keep the generic transport, just make the token injectable.
-
Publishing to MQTT and reading from there: adds an extra broker path and moving parts for what is fundamentally just a credential-injection problem.
Additional context
This is closely related to #6218, which previously raised environment-variable / secret handling for discussion. This request revisits that with a concrete, current use case (Home Assistant add-on + SUPERVISOR_TOKEN).
By the way: Thanks for evcc β it's excellent, I use it every day and got a sponsor token.
Thanks for the detailed writeup and the clear use case.
I confirmed the current state of env-var support in the code: evcc uses Viper with a fixed prefix/replacer (cmd/root.go):
viper.SetEnvPrefix("evcc")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
This only lets you override a known, fixed config key via an EVCC_...-prefixed env var (e.g. EVCC_DATABASE_DSN), as discussed in #6218. There is no generic ${VAR} placeholder expansion inside arbitrary string values (e.g. auth.token: ${SUPERVISOR_TOKEN} in a forecast/tariff block) β a repo-wide search for ExpandEnv/template-style substitution in config loading turns up nothing. So the 401 you're seeing is expected: the literal string ${SUPERVISOR_TOKEN} is sent as the bearer token, not the resolved env var.
This is a legitimate feature request, not a bug: the config loader would need a generic pass (likely os.ExpandEnv or similar) applied to string leaf values after YAML unmarshaling, so it works uniformly across forecast, tariff, vehicle, mqtt, etc. That's a design decision (whether to expand only ${VAR} syntax vs. all shell-style expansion, escaping literal $, docs impact) rather than a small isolated bug fix, so I'm not attempting an automatic fix here.
π€ Generated with Claude Code
Thanks Mr. AI :-),
that confirms it precisely:
the 401 is the literal ${SUPERVISOR_TOKEN} being sent as the bearer token, exactly because there's no generic placeholder expansion in config loading.
Agreed as well, that this is a design decision rather than a small bug fix. Hopefully the maintainers will approve it :-)
To make the request concrete, here's how the open questions could be answered (suggestion via my AI (Claude), hope that is OK β only as a first thought):
Which syntax to expand
Support only the explicit ${VAR} form, not bare $VAR. ${VAR} is unambiguous and matches what users expect from Docker Compose etc., while bare $VAR is exactly what would cause accidental expansion of legitimate values (jq expressions, passwords, tokens containing a $).
Where to apply it
After YAML unmarshaling, over string leaf values, so it works uniformly across forecast, tariff, vehicle, mqtt, etc. β no per-integration code.
A first minimal illustration (just to show the order of magnitude, not a proposed patch) is the three-liner from [spf13/viper#761](https://github.com/spf13/viper/issues/761):
for _, k := range viper.AllKeys() {
viper.Set(k, os.ExpandEnv(viper.GetString(k)))
}
However, os.ExpandEnv also expands bare $VAR, which is exactly what we'd want to avoid per the point above. So the actual recommendation would be os.Expand with a custom mapping function that only resolves the ${...} form and leaves a bare $ untouched. Also note that viper.AllKeys() may not cover every nested/list leaf, so the real implementation likely needs to walk the unmarshaled string values rather than relying on it β the three-liner is just illustrative.
Escaping a literal $
With a custom os.Expand mapping the escaping behavior is whatever you define β e.g. treating $$ as an escaped literal $ (the same convention os.ExpandEnv uses). Whatever is chosen should be called out clearly in the docs.
Undefined variables
Decide whether an unset ${VAR} expands to an empty string (the default os.Expand/os.ExpandEnv behavior) or should fail loudly at startup. Failing loudly is probably friendlier β an empty bearer token just produces a confusing 401 downstream, which is how this whole thread started.
See #19687
Thanks for the pointers to #19687 and the context, I didn't find/see that.
I understand YAML is being retired in favor of guided UI forms, so a generic ${VAR} YAML-interpolation pass is the wrong layer to invest in. Fair enough, I won't push for that.
To answer @naltatis question in #19687 directly ("What's the goal? Not having to repeat credentials?"): The goal isn't really about YAML or repetition. It's not having to store a secret in clear text at all, and - specifically in the Home Assistant add-on case - being able to use a token the runtime already provides (SUPERVISOR_TOKEN) instead of manually minting and pasting a Long-Lived Access Token.
So the "new" request to fit the guided-forms direction would be: A UI-native way to reference a secret rather than type it inline. For a credential field (bearer token, password, API key), allow the value to be a reference to an environment variable, resolved at runtime. That's independent of YAML, it would live in the guided form as a "use environment variable" option on secret fields, and would also cover the add-on token case without any HA-specific code.
If that direction is interesting I'm happy to move this into a fresh discussion framed around UI secret handling rather than YAML interpolation, so it doesn't get conflated with the (declined) ${VAR} approach.
And if it's simply not on the roadmap, that's a fine answer too.
Weβre not adding more yaml features, sorry.
Just my 2 cents: there is already the option to read Home Assistant sensors for other devices (like meters, chargers, etc)
Instead of dealing with yaml, adding a new var, etc.. Why not just add a new forecast provider "Home Assistant" which is reading the forecast sensor / attributes the same way as for the other devices?
@andig Understood, and thanks for the clear decision β no worries.
That reference-a-secret idea was what I took from Stefan's pointer to #19687.
Just to record it for anyone landing here later (not to reopen this): The request reframed for the guided-forms direction would be a UI-native way to reference a secret instead of typing it inline. For a credential field (bearer token, password, API key), the value could point to an environment variable resolved at runtime. That lives entirely in the guided form as a "use environment variable" option on secret fields, is independent of YAML, and would cover the add-on token case without any HA-specific code.
On @SolarPower2024's suggestion of a dedicated "Home Assistant" forecast provider: I think it is worth noting that a forecast sensor usually isn't a plain state value β the data sits in the sensor's attributes as a structured JSON forecast, so it would need attribute/JSON parsing rather than a simple state read. That's exactly the per-integration parsing logic you guys declined in #27645.
I'll open a fresh one framed around UI secret handling for the guided form, so it doesn't get conflated with the (declined) YAML approach.
The request reframed for the guided-forms direction would be a UI-native way to reference a secret instead of typing it inline. For a credential field (bearer token, password, API key), the value could point to an environment variable resolved at runtime.
Thank you for the clarification. That is of course the "proper" 12 factors way to do it. I'm still reluctant though since we're really targeting end users here and not IT professionals. If we can find a minimally-invasive wait of actually doing this (and I can't think of one atm) I'd reconsider.
I wrote a new one with a suggestion :-) Hopefully, es gefΓ€llt :-)