Skip to content

Instantly share code, notes, and snippets.

@wolph
Created January 9, 2024 02:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolph/989f9d7bfb81c72bcfeb2516d2f7bc60 to your computer and use it in GitHub Desktop.
Save wolph/989f9d7bfb81c72bcfeb2516d2f7bc60 to your computer and use it in GitHub Desktop.
Upload historical data from InfluxDB to mindergas.nl
'''
Use an influxdb query like this to fetch the data and store it as `meterstanden.csv`:
```
SELECT max("value") FROM "m3" WHERE ("entity_id"::tag = 'gas_consumption') AND $timeFilter GROUP BY time(1d) fill(linear)
```
'''
import pandas as pd
import json
import requests
import progressbar
df = pd.read_csv('meterstanden.csv', names=['t', 'v'], header=1, index_col='t')
df.index = pd.to_datetime(df.index, unit='ms')
# df = df[df.index > '2023-08-20']
df.dropna(inplace=True)
for (t, row) in progressbar.progressbar(df.iterrows(), max_value=len(df)):
# print(t, v)
data = dict(date=str(t.date()), reading=row.v)
requests.post('https://httpbin.org/post', data={'key': 'value'})
response = requests.post(
'https://www.mindergas.nl/api/meter_readings',
headers={'Content-Type': 'application/json', 'AUTH-TOKEN': 'API_TOKEN'},
data=json.dumps(data),
)
try:
response.raise_for_status()
except:
print(data)
print(f'Response: {response} :: {response.text!r}')
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment