-
Notifications
You must be signed in to change notification settings - Fork 1
/
433mqtt.py
executable file
·62 lines (54 loc) · 1.61 KB
/
433mqtt.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
59
60
61
62
#!/usr/bin/env python3
import paho.mqtt.client as mqtt
import json
import sys
import os
host = os.environ.get('RTL433MQTTHOST')
port = int(os.environ.get('RTL433MQTTPORT', 1883))
username = os.environ.get('RTL433MQTTUSER','rtl_433')
password = os.environ.get('RTL433MQTTPASS')
topic = os.environ.get('RTL433MQTTTOPIC','/rtl_433')
#fields which might contain a device id
idfields= [
'id', #most devices
'dev_id', #currentcost
'device', #ambient_weather vaillant_vrt340f
'sensor_id', #accurite
'node', #emontx
'device id', #hondaremote
'address', #ht680
'sid' #oregon_scientific springfield
]
if __name__ == "__main__":
client = mqtt.Client()
client.username_pw_set(username,password)
client.tls_set()
client.connect(host,port)
client.loop_start()
while 1:
try:
line = sys.stdin.readline().rstrip()
except KeyboardInterrupt:
break
if not line:
break
try:
jsonline = json.loads(line)
except ValueError:
# this is not a valid line of json, skip it
continue
#find if we have a model name
model = 'unknown'
if 'model' in jsonline:
model = str(jsonline['model'])
# find if we have an id field
idname='unknown'
for idfield in idfields:
if idfield in jsonline:
idname = str(jsonline[idfield])
break
#print(model,'/',idname,'=', line)
fulltopic = ''+topic+'/'+model+'/'+idname
client.publish(fulltopic,line)
client.loop_stop()
client.disconnect()