import json import datetime import os import sys def unix_to_real(unix): return datetime.datetime.fromtimestamp(unix/1000).strftime('%Y-%m-%d %H:%M:%S') def list_to_dict(input): output = {} #take the input and split the timestamp date from the time for i in input: sensor = i["sensor"] date = i["timestamp"].split(" ")[0] time = i["timestamp"].split(" ")[1] #check if sensor is not in output if sensor not in output: output[sensor] = {} #check if date is not in output for the sensor if date not in output[sensor]: output[sensor][date] = {"on": [], "off": []} #check if the state is on or off if i["state"] == "on": output[sensor][date]["on"].append(time) else: output[sensor][date]["off"].append(time) return output def main(file): with open(file, "r") as f: data = f.read() lines = data.split("\n") line_data = [] for line in lines: if line == "" or not line.startswith("{"): continue data = {} line = json.loads(line) data["state"] = line["payload"] data["timestamp"] = unix_to_real(line["detected"]) data["sensor"] = line["data"]["entity_id"] line_data.append(data) #convert the list to a dict data = list_to_dict(line_data) #save file to desktop desktop = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop') with open(desktop + "/data.json", "w") as f: f.write(json.dumps(data, indent=4)) if __name__ == "__main__": file = sys.argv[1] main(file)