Securing Your Smart Home: Managing Secrets with ESPHome and Home Assistant

When building a smart home with ESPHome and Home Assistant, it's crucial to manage sensitive information (secrets) securely. Hardcoding passwords or API keys directly into your ESPHome configurations is a major security risk. Fortunately, both ESPHome and Home Assistant provide mechanisms to handle secrets properly.
Why Use Secrets?
- Security: Prevents accidental exposure of sensitive data in your code repository or configuration files.
- Maintainability: Allows you to update secrets in one place without modifying multiple ESPHome configurations.
- Portability: Makes it easier to share or migrate your configurations without exposing your private information.
ESPHome Secrets Management
ESPHome uses a YAML file named secrets.yaml
located in the same directory as your ESPHome configuration files. This file contains key-value pairs where the key is the secret name and the value is the actual secret.
Setup Steps:
- Create
secrets.yaml
: In your ESPHome configuration directory, create a file namedsecrets.yaml
. - Define Secrets: Add your secrets in the following format:
wifi_ssid: "YourWiFiSSID" wifi_password: "YourWiFiPassword" api_key: "YourAPIKey"
- Reference Secrets in ESPHome Configuration: Use the
!secret
tag to reference secrets in your ESPHome configuration files:wifi: ssid: !secret wifi_ssid password: !secret wifi_password
Device Integration Tips:
- Restart ESPHome Devices: After modifying
secrets.yaml
, remember to re-upload the ESPHome firmware to your devices for the changes to take effect. - Version Control: Add
secrets.yaml
to your.gitignore
file to prevent it from being committed to your code repository.
Home Assistant Secrets Management
Home Assistant also provides a secrets management system. Similar to ESPHome, it uses a secrets.yaml
file located in your Home Assistant configuration directory.
Setup Steps:
- Create
secrets.yaml
(if it doesn't exist): Check if asecrets.yaml
file exists in your Home Assistant configuration directory (usually/config
). If not, create one. - Define Secrets: Add your secrets in the same key-value format as ESPHome:
google_maps_api_key: "YourGoogleMapsAPIKey" twitter_api_key: "YourTwitterAPIKey"
- Reference Secrets in Home Assistant Configuration: Use the
!secret
tag to reference secrets in your Home Assistant configuration files:device_tracker: - platform: google_maps api_key: !secret google_maps_api_key
Best Practices for Managing a Reliable Smart Home Ecosystem
- Regular Backups: Automate regular backups of your Home Assistant configuration and ESPHome firmware to a secure location.
- Strong Passwords: Use strong, unique passwords for all your smart home devices and services.
- Software Updates: Keep your Home Assistant installation, ESPHome devices, and all related software up to date with the latest security patches.
- Network Security: Secure your home network with a strong WiFi password and consider using a firewall.
- Two-Factor Authentication: Enable two-factor authentication (2FA) for all your accounts, including Home Assistant and ESPHome Cloud.
By following these steps and best practices, you can significantly improve the security and maintainability of your smart home ecosystem, ensuring a safer and more reliable experience.

NGC 224
Author bio: DIY Smart Home Creator