Hello everyone!
I’ve been looking at the RECORDER components, and none of the free tier options seem to be a good alternative.
Behold UBIDOTS for EDUCATION they allow for a single device (your HASS installation), and up to 10 variables (device/sensor states).
They hold your history for up to 3 months and allow nearly realtime uploads (one data push per second).
I’ve written a very rudimentary Python daemon, but getting the states from the HASS CURL api has proven unreliable.
I’m hoping someone with better HASS knowledge that can make use of my sample as an inspiration to properly take advantage of the RECORDER feature built into HASS.
import requests
import time
from time import sleep
###HEADERS HASS API REQUEST###
headers_hass = {
'x-ha-access': 'insert-hass-password-here',
'Content-Type': 'application/json',
}
###HEADERS UBIDOTS POST REQUEST###
headers_ubi = {
'Content-Type': 'application/json',
}
###UBIDOTS TOKEN###
ubi_token = "insert-token-here"
###URL UBIDOTS API###
url_ubi = "http://things.ubidots.com/api/v1.6/collections/values?token="+ ubi_token
###JSON TO BE UPLOADED TO UBIDOTS API###
payload_ubi = []
###INDEX TO BE USED IN RECURSIVE FOR CYCLE###
index = 0
###TEMPORARY JSON STORAGE OF INMEDIATELY PREVIOUS VALUES TO CHECK FOR CHANGES###
old_value = []
###JSON CONFIGURATION DICTIONARY OF HASS ENTITIES AND THEIR UBIDOTS VARIABLE ID###
dict = [
{
"variable": "insert-ubidots-variable-here",
"name": "insert.hass.entity.id"
},
{
"variable": "insert-ubidots-variable-here",
"name": "insert.hass.entity.id"
},
{
"variable": "insert-ubidots-variable-here",
"name": "insert.hass.entity.id"
},
{
"variable": "insert-ubidots-variable-here",
"name": "insert.hass.entity.id"
},
{
"variable": "insert-ubidots-variable-here",
"name": "insert.hass.entity.id"
},
{
"variable": "insert-ubidots-variable-here",
"name": "insert.hass.entity.id"
},
{
"variable": "insert-ubidots-variable-here",
"name": "insert.hass.entity.id"
},
{
"variable": "insert-ubidots-variable-here",
"name": "insert.hass.entity.id"
},
{
"variable": "insert-ubidots-variable-here",
"name": "insert.hass.entity.id"
},
{
"variable": "insert-ubidots-variable-here",
"name": "insert.hass.entity.id"
}
]
###RECURSIVE FOR CYCLE TO GO THROUGH HASS API RESPONSE, CHECK OLD VALUES, FORMAT STATES TO NUMBERS AND BUILD UBIDOTS JSON PAYLOAD###
while True:
sleep (0.7)
#start_time = time.time()
response_hass = requests.get('https://0.0.0.0:8123/api/states', headers=headers_hass, verify=False)
resp_hass = response_hass.json()
for i in dict:
for j in resp_hass:
if i["name"] == j["entity_id"]:
#print("the index is ", index, i["name"], " has a state of ", j["state"])
name = i["name"]
value = j["state"]
if (value == "off") or (value == "not_home"):
value = 0
elif (value == "on") or (value == "home"):
value = 1
var_id = i["variable"]
if len(old_value) < len(dict):
old_value.append({name:value})
payload_ubi.append({"variable":var_id,"value":value})
else:
#print("the old value is ", old_value[index][name])
#print("the current value is ", value)
if old_value[index][name] != value:
old_value[index][name] = value
payload_ubi.append({"variable":var_id,"value":value})
#print("payload_ubi is ", payload_ubi)
#print("payload_ubi lenght is ", len(payload_ubi))
#print("old_value is ", old_value)
#print("old_value lenght is ", len(old_value))
index += 1
if len(payload_ubi) == 0:
#print("payload_ubi empty, sleeping")
sleep (1)
else:
#print("sending payload_ubi to ubidots ")
response_ubi = requests.post(url_ubi, headers=headers_ubi, json=payload_ubi)
resp_ubi = response_ubi.json()
#print("ubidots reply ", resp_ubi)
index = 0
payload_ubi = []
#print("--- %s seconds ---" % (time.time() - start_time)) json=payload_ubi)