I have a large number of Sonoff smart plugs and I’m looking to templatize my configuration YAML using variables. It’s generally working, but the compile-time substitution seems to be failing for secret variables. For example, this fails:
substitutions:
plug_id: "20"
ha_api_key: !secret api_key_sonoff_s${plug_id}
Is there a different syntax needed for this, or is this not possible?
This seems to speak to the topic.
opened 03:12PM - 04 Jun 25 UTC
**Describe the problem you have/What new integration you would like**
Allow sub… stitutions in secret references
**Please describe your use case for this integration and alternatives you've tried:**
The esphome UI suggests that `api.key` should be unique to each device, however this precludes using variables over hardcoding device names into the reference, as substitutions aren't expanded in secret references.
For example, if you have multiple devices each with their own API key (or any other secret), it's cleaner to have `api_key: !secret "${name}".api.key` inherited (or even repeated) across configs, than to hardcode/retype the device name in each file.
**Additional context**
Back in 2019 (https://github.com/esphome/issues/issues/486#issuecomment-506014504) it was said that secrets were resolved prior to substitutions by design and this is how it'll stay as "it makes more sense in some cases". Perhaps it would be possible to preserve this order by prepending a third step to the order of processing:
1) substitutions in `!secret` references are expanded
2) `!secret` references themselves are expanded
3) Other substitutions are expanded
I'm unsure of the level of interest in this as a whole, but I think it may make for slightly more reusable config and easier maintenance in larger esphome deployments.
Thanks! :)
1 Like