PurpleAirLokiDataLogger module¶
Copyright 2023 carlkidcrypto, All rights reserved. A Python class designed to use the PurpleAirAPI for requesting sensor(s) data. Data will be pushed to a Grafana Loki instance via the Loki HTTP push API.
For best practice from PurpleAir: “The data from individual sensors will update no less than every 30 seconds. As a courtesy, we ask that you limit the number of requests to no more than once every 1 to 10 minutes, assuming you are only using the API to obtain data from sensors. If retrieving data from multiple sensors at once, please send a single request rather than individual requests in succession.”
- class PurpleAirLokiDataLogger.PurpleAirLokiDataLogger(PurpleAirApiReadKey=None, PurpleAirApiWriteKey=None, PurpleAirApiIpv4Address=None, loki_url=None, loki_usr=None, loki_pwd=None)¶
Bases:
PurpleAirDataLoggerA data logger class that pushes PurpleAir sensor data to a Grafana Loki instance via the Loki HTTP push API. Grafana can then be used to visualize the stored data.
- store_sensor_data(single_sensor_data_dict)¶
Push the sensor data to Loki. Each data group is sent as a separate Loki log stream labelled with ‘sensor_index’ and ‘data_group’. The log line is a JSON string of the relevant fields for that group.
- Parameters:
single_sensor_data_dict (dict) – A python dictionary containing all fields for insertion. If a sensor doesn’t support a certain field make sure it is
Noneand part of the dictionary. This method does no type or error checking. That is up to the caller. Failures are caught, logged to stdout, and counted rather than raised.