The ha-cli tool now supports automatic loading of environment variables from a .env file using python-dotenv.
-
Copy the example file:
cp .env.example .env
-
Edit
.envwith your Home Assistant credentials:# Your Home Assistant URL HOME_ASSISTANT_URL=http://homeassistant.local:8123 # Your Long-Lived Access Token HOME_ASSISTANT_TOKEN=your-token-here
-
The tool will automatically load these values - no need to export them!
The tool looks for credentials in this order:
- Command line arguments (
--urland--token) - Environment variables (including those from
.envfile) - Config file (
~/.config/home-assistant-cli/config.json) - Interactive prompt (if all else fails)
HOME_ASSISTANT_URLorHA_URL- Your Home Assistant URLHOME_ASSISTANT_TOKENorHA_TOKEN- Your access token
- The
.envfile is already in.gitignore- never commit it! - Keep your
.envfile secure with appropriate permissions:chmod 600 .env
If you prefer not to use .env files, you can still export variables:
export HOME_ASSISTANT_URL="http://homeassistant.local:8123"
export HOME_ASSISTANT_TOKEN="your-token-here"- Log into Home Assistant
- Click your profile (bottom left)
- Go to "Security" tab
- Create a "Long-Lived Access Token"
- Copy the token to your
.envfile