Skip to content
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE
README.md
alpha_vantage_to_mqtt.py
env-sample
requirements.txt

README.md

Alpha Vantage to MQTT

This script was written to provide stock price to a MQTT sensor in the HomeAssistant home automation platform. Unfortunately the built in Alpha Vantage componet fails if you have more than 1 or 2 stocks that you want to follow. And this script also allows you to offload the sensor update to another system to save your HomeAssistant resources from non-mission critical functions.

Requirements

Installation

  • Install the app by cloning this repo
  • git clone https://github.com/thejeffreystone/alpha_vantage_to_mqtt.git
  • Install the required python libraries:
  • pip install paho-mqtt
  • pip install alpha_vantage
  • pip install python-dotenv
  • Edit the env-sample and saved as .env

Run

This script is meant to run with something like supervisord, and has a default interval of 3600 seconds (1 hour). If you want to have this script to update your stocks continiously set the interval greater than zero. If interval is set to 0 the script will exit after running once. If you want to have cron or some other system mange the script execution then simply set the interval to 0 in the .env

There is a brief write up about the specific use case I built this for at [https://slackerlabs.org/2019/03/08/home-assistant-alpha-vantage/]

Features

  • This script publishes stock price to topic stock/<stock_name>/price
  • This script utilizes the get_batch_stock_quotes method of the Alpha Vantage API to reduce the number of calls to the API to avoid hitting the API more than once a second.
  • This script is built to run continiously and will pause for the interval set in the .env file.
  • The script has an app_mode set in the .env file. When app_mode is set to debug the script will output status messages to stdout. If you would like to supress these, change app_mode to somethign other than debug. Default app_mode is prod.
  • Log the script to Splunk using Splunk's HTTP Event Collector

You can Splunk It if you want to. You can leave your friends behind...

I added the ability to log to Splunk's HTTP Event Collector if you are like me and like to Splunk everything. The script leverages George Starcher's Python Class.

If you want to use the Splunk component you will need to install George Starcher's Python Class

  • pip install git+git://github.com/georgestarcher/Splunk-Class-httpevent.git

The update your .env to include the details of your Splunk server:

# Splunk Server:
splunk_server=192.168.1.20
# Splunk HEC Port - Default is 8088:
splunk_hec_port=8088
# Splunk Hec SSL:
splunk_hec_ssl=False
# Splunk HEC Token:
splunk_hec_key=
# Splunk index:
splunk_index=main
# Splunk sourcetype:
splunk_sourcetype=alpha_vantage
# Splunk Source:
splunk_source=alpha_vantage_script
# Splunk host:
splunk_host=jarvis

If you don't set a token, the script will ignore the Splunk portion.

If you are new to Splunk or the Splunk's HTTP Event Collector you can find more information at Use Splunk's HTTP Event Collector

Compatibility

This script was written and tested using python 3.7.2

You can’t perform that action at this time.