Skip to content

Instantly share code, notes, and snippets.

@cdump
Created January 3, 2020 08:50
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cdump/af1b082acbdee73a6bdba525b0688208 to your computer and use it in GitHub Desktop.
Save cdump/af1b082acbdee73a6bdba525b0688208 to your computer and use it in GitHub Desktop.
cleargrass prometheus
#!/usr/bin/python3
from time import sleep, time
import miio
from prometheus_client import start_http_server, Gauge
if __name__ == '__main__':
MONITOR_IP = '192.168.88.100'
MONITOR_TOKEN = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
battery = Gauge('home_battery', 'Battery', ['device'])
temperature = Gauge('home_temperature', 'temperature', ['device'])
humidity = Gauge('home_humidity', 'humidity', ['device'])
co2 = Gauge('home_co2', 'co2', ['device'])
pm25 = Gauge('home_pm25', 'pm25', ['device'])
tvoc = Gauge('home_tvoc', 'tvoc', ['device'])
update_ts = Gauge('home_update_ts', 'update_ts', ['device'])
start_http_server(5433)
monitor_conn = None
while True:
sleep_time = 5
try:
if monitor_conn is None:
print("try conn monitor...")
monitor_conn = miio.airqualitymonitor.AirQualityMonitor(ip=MONITOR_IP, token=MONITOR_TOKEN, model='cgllc.airmonitor.s1')
st = monitor_conn.status()
print(st)
battery.labels('monitor').set(st.battery)
temperature.labels('monitor').set(st.temperature)
humidity.labels('monitor').set(st.humidity)
co2.labels('monitor').set(st.co2)
pm25.labels('monitor').set(st.pm25)
tvoc.labels('monitor').set(st.tvoc)
update_ts.labels('monitor').set(int(time()))
except Exception as ex:
print(ex)
sleep_time = 1
sleep(sleep_time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment