My Custom Alpha Vantage Integration for Home Assistant

My Custom Alpha Vantage Integration for Home Assistant

I recently added the Alpha Vantage component to my Home Assistant setup, and quickly realized it wasn’t going to meet my needs. First off there appears to be a pretty critical bug with the component that severely hampers the built in components usability. You can read about it at if you want to know the specifics. And second I have started looking at ways to off load the non-mission critical functions of my Home Assistant setup.

Turns out the way the component was built, if you have more than a couple of stocks you want to create sensors for, the initial load of the sensors fails due to built in limits of the Alpha Vantage API. I’m guessing these limits didn’t exist when the component was first built otherwise they wouldn’t have shipped with them. Or maybe I should say I hope they weren’t. Either way, I decided to take a crack a solution tailored to my requirements. What I came up with is

My use case involves Home Assistant, but if you are looking for a way to publish stock prices to MQTT that is essentially what my script does. If that is your use case what follows may not be interesting. Besides, everything you need to simply send stock prices to MQTT using Alpha Vantage and python just got to my repo at

The whole idea behind this script is to use the batch capabilities in the Alpha Vantage to get the stocks I want to follow all in a single call to get past having to include self-imposed throttling to prevent the calls from getting kicked by the API, and to run on another server and simply publish the updates to MQTT. Then I can have a MQTT sensor on Home Assistant just monitor the MQTT topic and display the sensor.

So this is how I put this solution into production.

The secret sauce is all in and I don’t think I need to give a history of how I built it. This isn’t a recipe blog after all. But the basic gist is you tell it what stocks you want to get the price for, where your MQTT server is and let it do its thing. The script publishes the updates to stock//price so if you want to following Facebook’s stock, and you added FB to your .env the script would publish the price of the stock, at an interval of your choosing, to stock/FB/price and then Home Assistant can read that in as a sensor.

The script it self is pretty simple, and everything you need to run it in your own environment is in the readme. If you find bugs, or see enhancements let me know.

With the script done, I simply deployed to my Ubuntu server (the one that I use for running various non-essential home automation tasks) and set up supervisord to manage it. Supervisord is a critical in my environment. I seriously do not know how things would stay running it was’t supervising the show.

For those of you using supervisord, I simply added /etc/supervisord/conf.d/alpha_vantage.conf And inside the file added:

[program:alpha_vantage]  command=/home/thejeffreystone/bin/alpha_vantage_to_mqtt/

Then I told supervisord to reead the conf directory: sudo supervisorctl reread

Then told it to run the new config: sudo supervisorctl update

Once last check to make sure it was running: sudo supervisorctl

Once the script was running I checked to make sure my script was publishing the stock prices as expected: mosquitto_sub -h -p 1883 -v -t 'stock/#' -u mqtt -P my_password

And then the updates came in and there was much rejoicing:

stock/DIS/price 113.8100
stock/BAC/price 28.6600
stock/INTC/price 52.5050
stock/BKS/price 5.3150
stock/SIRI/price 5.9450    

You may need to set the interval lower if you want to do testing. Or simply kill the script and rerun it. If will publish updates as soon as it starts. By default I set the interval to 1 hour. As long as it more than 1 second you should be good.

Next I simply needed to build the sensors for each of my stocks in Home Assistant, so I created a finance.yaml in my sensors directory.

- platform: mqtt
  name: "Disney"
  state_topic: "stock/DIS/price"
- platform: mqtt
  name: "BoA"
  state_topic: "stock/BAC/price"
- platform: mqtt
  name: "Intel"
  state_topic: "stock/INTC/price"
- platform: mqtt
  name: "Sirius"
  state_topic: "stock/SIRI/price"
- platform: mqtt
  name: "Barnes and Nobel"
  state_topic: "stock/BKS/price"

After that I just needed to reboot my Home Assistant to get my new sensors to start working and then I went and had a nice beverage.

If you want to see more of my Home Assistant Config and how I am using it, head over to my Home Assistant Config Repo. If you found this helpful let me know.

Until next time…Go Automate Something.