#!/usr/bin/python import requests import datetime import time # example JSON status string # {'Status': [{'Engine': [{'Switch State': 'Auto'}, {'Engine State': 'Off - Ready'}, {'Battery Voltage': '13.90 V'}, {'RPM': '0 '}, {'Frequency': '0.00 Hz'}, {'Output Voltage': '0 V'}, {'Active Rotor Poles (Calculated)': '0 '}, {'Unsupported Sensors': [{'Raw RPM Sensor': '0'}, {'Hz (Calculated)': '0.0 Hz'}, {'Unsupported Sensor 1 (0x0032)': '0.00'}, {'Unsupported Sensor 2 (0x0033)': '64816'}, {'Unsupported Sensor 3 (0x0034)': '-3'}]}]}, {'Line': [{'Utility Voltage': '241 V'}, {'Utility Max Voltage': '246 V'}, {'Utility Min Voltage': '234 V'}, {'Utility Threshold Voltage': '156 V'}]}, {'Last Log Entries': {'Logs': {'Alarm Log': '02/01/23 15:12:12 Inspect Battery ', 'Run Log': '03/21/24 07:24:09 Stopped - Auto '}}}, {'Time': [{'Monitor Time': 'Friday March 22, 2024 02:24:31'}, {'Generator Time': 'Friday March 22, 2024 02:24'}]}]} def GetVoltageInfo(): return_voltage = None return_status = None try: # TODO change this IP address to match your system r = requests.get('http://localhost:80/cmd/status_json', auth = ('public', 'Public1')) status_output = r.json() #print('status_output ', status_output) for entry in status_output["Status"]: if isinstance(entry, dict): for key, value in entry.items(): if key == "Engine": if isinstance(value, list): for subentry in value: if isinstance(subentry, dict): if "Output Voltage" in subentry.keys(): return_voltage = subentry["Output Voltage"] elif "Engine State" in subentry.keys(): return_status = subentry["Engine State"] else: continue return return_voltage, return_status except Exception as e1: print("Error: " + str(e1)) return return_voltage, return_status while True: interval = 1 voltage_data, engine_status_data = GetVoltageInfo() currenttime = datetime.datetime.now() if voltage_data and engine_status_data: if 'Off' in engine_status_data: interval = 60 else: print(str(currenttime) + "," + voltage_data + "," + engine_status_data) # sleep for interval seconds time.sleep(interval)