Skip to content

Instantly share code, notes, and snippets.

@rien333
Created October 10, 2024 15:44
Show Gist options
  • Save rien333/4748191022501a326c44750ae29d8806 to your computer and use it in GitHub Desktop.
Save rien333/4748191022501a326c44750ae29d8806 to your computer and use it in GitHub Desktop.
Log data from the `sensors` program to a json file
import sh
import json
from time import sleep
def transform_readings(readings, path=''):
"""Transform all readings from `sensors` into a dict that contains just sensor names and sensor values"""
results = {}
# not all values are dicts
if not isinstance(readings, dict):
return {}
for k, v in readings.items():
current_path = path + k + '.'
if k.endswith('_input'):
results[current_path.rstrip('.')] = float(v)
# recurse
results.update(transform_readings(v, path=current_path))
return results
while True:
# call the sensors CLI program
readings = sh.sensors(j=True)
readings_json = json.loads(readings)
readings_inputs = transform_readings(readings_json)
readings_inputs['date'] = sh.date("+%H:%M:%S").strip()
with open('readings.json', 'a') as f:
json.dump(readings_inputs, f)
sleep(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment