!env_var help

Is there any advice/guides on how to actually use !env_var in configuration.yaml?

I noticed that one of the example configurations has been updated with some but it is not clear how it works in practice

Cheers

In your config:

some_api_key: !env_var MY_API KEY

If you’re using Linux, at the command line:

export MY_API_KEY="something"

I wrote a shell script that has all my exports, followed by hass, and I run the script instead of running hass directly.

thanks

so we can’t just create a variables.yaml file which can then contain these variables?

No. These variables are only read from your system’s environment.

okay thanks for the info

I just got around to setting this up yesterday. If you are using the systemd autostart that is referenced in the docs, you can just add an EnvironmentFile line and then you can put all the variables in there

Here is an example of my systemd config:

[Unit]
Description=Home Assistant
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/hass
Restart=always
User=pi

Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
EnvironmentFile=/home/pi/.homeassistant/ha.env

[Install]
WantedBy=multi-user.target

Then in /home/pi/.homeassistant/ha.env I just have a new line separated list of variables

2 Likes

home/hass/ha.env should just be a newline separated list of name=value pairs

(that tripped me up as I had export prepended to each line)

Is it possible to use environment variables with a template?

This post has helped me a lot, thank you.

However, I’m having a problem where I have a generic IP camera that doesn’t work when configured this way (per the documentation):

- platform: generic
  still_image_url: http://192.168.1.141/snapshot.cgi
  name: Nathan
  username: !env_var CAM_USERNAME
  password: !env_var CAM_PASSWORD

It only works like this:

- platform: generic
  still_image_url: http://192.168.1.141/snapshot.cgi?user=xxx&pwd=xxx
  name: Nathan

(This is a Foscam, actually, but it doesn’t work with the foscam platform for some reason)

I can’t figure out how to use the environment variables inline in the still image url, so my only solution right now is to hard code the username and password. Is it possible to use them inline with another string?

No, at least not right now. The solution is to make the whole URL an environmental variable.

I was also searching on how to use this !env_var thingy and found this explanation on how to use the secrets.yaml here: https://home-assistant.io/topics/secrets/

I’m not sure if this is also meant to store stuff like camera URL and such, but for passwords and API-keys that i use it is OK.

###Setting up !env_var on Raspberry Pi 2, Home-Assistant All-In-One Installer (for noobs, like myself :blush:)

Login to Raspberry Pi. For example:

ssh [email protected]_raspberry_pi_ip

Edit home-assistant.service file:

sudo nano /etc/systemd/system/home-assistant.service

paste >

Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
EnvironmentFile=/home/hass/.homeassistant/ha.env

file should look like this >

[Unit]

Description=Home 
Assistant
After=network.target

[Service]

Type=simple
User=hass
ExecStart=/srv/hass/hass_venv/bin/hass -c "/home/hass/.homeassistant"

Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
EnvironmentFile=/home/hass/.homeassistant/ha.env

[Install]
WantedBy=multi-user.target

save and close.

Run the following command to make a new file (ha.env file) >

sudo nano /home/hass/.homeassistant/ha.env

Type your sensitive data as newline separated list of name=value pairs

http_password=yourpassword
some_api_key=yourapikey
   ...

save and close file, reboot.

sudo reboot

Whenever you need to hide sensitive data add, for example, in configuration.yaml:

http:
  api_password: !env_var http_password 

restart HA service

sudo service home-assistant restart

4 Likes

Thanks for posting, this helped me out!

Can I replace !secret with !env_var and assuming I have matching values, will everything work just the same?