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 https://github.com/home-assistant/home-assistant/issues/15271 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 https://github.com/thejeffreystone/alpha_vantage_to_mqtt

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 https://github.com/thejeffreystone/alpha_vantage_to_mqtt

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 https://github.com/thejeffreystone/alpha_vantage_to_mqtt 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/<insert stock name>/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 https://github.com/thejeffreystone/alpha_vantage_to_mqtt 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/alpha_vantage_to_mqtt.py
    autostart=true
    autorestart=true
    startretries=999999999999999
    user=jeffrey
    environment=HOME="/home/thejeffreystone",USER="thejeffreystone"
    directory=/home/thejeffreystone/

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 192.168.1.2 -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. And if you didn’t, that is ok as well. I just don’t need to hear about.

Back to Automating the Planet!