Custom Crypto Currency Sensors in Homeassistant

Cryptocurrency and HomeAssistant

I have recently diversified my investments in crypto-coins, and wanted to keep track of them all in order to track my investments. So I utilized the cryptocompare API and built a python dictionary of all the different crypto-coins I own, and the rest is history!

Here is the script that I use:

import requests
import json
import datetime

# Custom Cryptocurrency sensor python script

# HASS URL
base_hass_url = "http://HASS URL/api/states/"

# Define headers for HASS
hass_headers = {'Accept': 'application/json', 'Content-Type': 'application/json', 'x-ha-access': "hunter2"}

# Get yesterday timestamp for historical pricing
yesterday = datetime.datetime.today() - datetime.timedelta(days=1)
yesterday_unix = yesterday.timestamp()
original = datetime.datetime(2017, 0, 0, 0, 0, 0)
original_unix = original.timestamp()

# Add base URL structure
base_url = "https://min-api.cryptocompare.com/data/price?fsym="
base_history_url = "https://min-api.cryptocompare.com/data/pricehistorical?fsym="
base_headers = {'User-Agent': 'Python Requests', 'Content-Type': 'application/json'}
base_tsym = "&tsyms=USD"
base_history_tsym = "&tsyms=USD&ts=" + str(int(yesterday_unix))
base_original_tsym = "&tsyms=USD&ts=" + str(int(original_unix))

# Cryptocoin maps
cryptocoins = { "ETH": {"state": "0", "sensor_name": "sensor.coin_eth_price", "attributes": { "friendly_name": "Ethereum Value", "unit_of_measurement": "USD", "previous_value": "0", "original_value": "0"}},
                "BTC": {"state": "0", "sensor_name": "sensor.coin_btc_price", "attributes": { "friendly_name": "Bitcoin Value", "unit_of_measurement": "USD", "previous_value": "0", "original_value": "0"}},
                "LTC": {"state": "0", "sensor_name": "sensor.coin_ltc_price", "attributes": { "friendly_name": "Litecoin Value", "unit_of_measurement": "USD", "previous_value": "0", "original_value": "0"}},
                "NMC": {"state": "0", "sensor_name": "sensor.coin_nmc_price", "attributes": { "friendly_name": "Namecoin Value", "unit_of_measurement": "USD", "previous_value": "0", "original_value": "0"}},
                "ZEC": {"state": "0", "sensor_name": "sensor.coin_zec_price", "attributes": { "friendly_name": "Zcash Value", "unit_of_measurement": "USD", "previous_value": "0", "original_value": "0"}},
                "ICN": {"state": "0", "sensor_name": "sensor.coin_icn_price", "attributes": { "friendly_name": "Iconomi Value", "unit_of_measurement": "USD", "previous_value": "0", "original_value": "0"}},
                "GNT": {"state": "0", "sensor_name": "sensor.coin_gnt_price", "attributes": { "friendly_name": "Golem Value", "unit_of_measurement": "USD", "previous_value": "0", "original_value": "0"}},
                "STRAT": {"state": "0", "sensor_name": "sensor.coin_strat_price", "attributes": { "friendly_name": "Stratis Value", "unit_of_measurement": "USD", "previous_value": "0", "original_value": "0"}},
              }

# For each coin
for coin in cryptocoins:

  # HTTP GET the API for price and price yesterday
  coin_request = requests.get(base_url + coin + base_tsym, headers=base_headers)
  coin_request_historical = requests.get(base_history_url + coin + base_history_tsym, headers=base_headers)
  coin_request_original = requests.get(base_history_url + coin + base_original_tsym, headers=base_headers)

  # Set the response as the state for the coin and the previous value as an attribute
  response = coin_request.json()
  historical_response = coin_request_historical.json()
  original_response = coin_request_original.json()
  cryptocoins[coin]["state"] = str(response.get("USD"))
  cryptocoins[coin]["attributes"]["previous_value"] = str(historical_response.get(coin).get("USD"))
  cryptocoins[coin]["attributes"]["original_value"] = str(original_response.get(coin).get("USD"))

  # Build the payload and URL for HASS
  coin_url = base_hass_url + str(cryptocoins[coin]["sensor_name"])
  coin_payload = {
                    "state": cryptocoins[coin]["state"],
                    "attributes": cryptocoins[coin]["attributes"]
                  }

  # Make the request
  coin_request = requests.post(coin_url, headers=hass_headers, data=json.dumps(coin_payload))