-
Notifications
You must be signed in to change notification settings - Fork 10
/
nordpool_mqtt.py
58 lines (49 loc) · 2.25 KB
/
nordpool_mqtt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/python
# coding: utf-8
import configparser
from urllib.request import urlopen
import json
import datetime
import pytz
import paho.mqtt.publish as publish
import os
import codecs
main_base = os.path.dirname(__file__)
config_file = os.path.join(main_base, "config", "prod.cfg")
config = configparser.ConfigParser()
config.read_file(codecs.open(config_file, 'r', 'utf8'))
mqtt_port = config.getint('MQTT', 'port')
mqtt_ip = config.get('MQTT', 'ip')
mqtt_auth = {'username':config.get('MQTT', 'username'), 'password':config.get('MQTT', 'password')}
mqtt_username = config.get('MQTT', 'username')
mqtt_password = config.get('MQTT', 'password')
mqtt_topic_today = config.get('MQTT', 'today_pub')
mqtt_topic_tomorrow = config.get('MQTT', 'tomorrow_pub')
dir_path = config.get('Nordpool', 'cache_dir')
area = config.get('Nordpool', 'area')
currency = config.get('Nordpool', 'currency')
dt_today = datetime.date.today().strftime("%d-%m-%Y")
def publish_price(topic, date):
data = json.load(open('{}/{}.json'.format(dir_path, date)))
#print u'Units: {}\n Updated: {}\n Currency:{}'.format(resp_dict['data']['Units'][0], resp_dict['data']['DateUpdated'], resp_dict['currency'])
for row in data['data']['Rows']:
for col in row['Columns']:
if col['Name'] != area:
continue
hours_display = row['Name'].replace(' ', '')
publish.single(topic.format(value=hours_display), col['Value'].replace(',','.').replace(' ',''), hostname=mqtt_ip, port=mqtt_port, auth=mqtt_auth)
#Check if current time
now = datetime.datetime.now(pytz.timezone('CET'))
hours = hours_display.split('-')
try:
if date == dt_today and (datetime.time(int(hours[0]),00) <= now.time() <= datetime.time(int(hours[1]),00) or (datetime.time(23,00) <= now.time() and int(hours[1]) == 0)):
publish.single(topic.format(value='current'), col['Value'].replace(',','.').replace(' ',''), hostname=mqtt_ip, port=mqtt_port, auth=mqtt_auth)
except ValueError:
pass
#Get todays prices
publish_price(mqtt_topic_today, dt_today)
#We only have next day prices after 12:00
now = datetime.datetime.now(pytz.timezone('CET'))
if now.time() >= datetime.time(13,00):
dt_tomorrow = (datetime.date.today()+ datetime.timedelta(days=1)).strftime("%d-%m-%Y")
publish_price(mqtt_topic_tomorrow, dt_tomorrow)