Created
January 21, 2022 16:39
-
-
Save JosephHewitt/e49f5c1bdcfb27ddfb92cc7f715cb33d to your computer and use it in GitHub Desktop.
Parse rtl_433 and send CurrentCost power readings to IoTPlotter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import time | |
import json | |
try: | |
import requests | |
except: | |
print("\n\nPlease install Python-requests, this command will work on many systems: 'pip install requests'\n\n") | |
raise | |
#Run this script like so: rtl_433 -F json | python3 ccparse.py | |
#Please alter the following 3 lines to configure your setup: | |
cc_id = "" #The ID of your CurrentCost, please use rtl_433 to discover this. | |
api_key = "" #The API key for your IoTPlotter feed. | |
feed_id = "" #The ID of your feed (usually an 18 digit number | |
#End of configuration section. | |
to_plot = [] | |
def plot(to_plot): | |
#Send data to IoTPlotter.com | |
payload = "" | |
for dp in to_plot: | |
epoch = dp['epoch'] | |
for key in dp: | |
if key == 'epoch': continue | |
payload = payload + str(epoch) + "," + str(key) + "," + str(dp[key]) + "\n" | |
print(payload) | |
headers = {'api-key': api_key} | |
url = "https://iotplotter.com/api/v2/feed/" + str(feed_id) + ".csv" | |
try: | |
r = requests.post(url, data=payload, headers=headers, timeout=2) | |
except Exception as e: | |
print("IoTPlotter:", e) | |
return False | |
if r.status_code == 200: | |
return True | |
print("IoTPlotter says:", r.status_code, r.content) | |
return False | |
if not cc_id or not api_key or not feed_id: | |
print("\n\nPlease edit the Python file to include your CurrentCost ID and IoTPlotter key\n\n") | |
raise "Not configured" | |
for line in sys.stdin: | |
line = line.strip() | |
if not line: continue | |
obj = None | |
try: | |
obj = json.loads(line) | |
except: continue | |
if not obj: continue | |
is_valid = False | |
try: | |
if str(obj['id']) == str(cc_id) and 'currentcost' in obj['model'].lower(): | |
is_valid = True | |
except: continue | |
if not is_valid: continue | |
data = {} | |
for key in obj: | |
try: | |
if 'power' in key.lower() and int(obj[key]) > 0: | |
data[key] = obj[key] | |
data['epoch'] = time.time() | |
except: continue | |
to_plot.append(data) | |
if len(to_plot) > 5: | |
if plot(to_plot): | |
to_plot = [] | |
elif len(to_plot) > 30: | |
to_plot = [] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment