Jan-02-2023, 10:15 PM
** Ok, I think I have figured out some of it after making myself a bit crazy. I was actually trying to read the device information from the wrong section. This is a JSON file output from a SONOFF SWITCH using Tasmota Firmware. Here is what I have for now and it seems to be working as expected. However, it is messy and I know needs some cleaning. I come from VB.NET so Python has been much different to me for defined functions and how they get called.
I guess right now my question is: Do I create a def for the temperature? I also want to create a label to display the time, which I have the code, I just want to be sure I am laying it out correctly so I can call these in a correctly. I plan to add 3-4 additional switch/relay devices so I want to be sure it is done in a clean way.
I guess right now my question is: Do I create a def for the temperature? I also want to create a label to display the time, which I have the code, I just want to be sure I am laying it out correctly so I can call these in a correctly. I plan to add 3-4 additional switch/relay devices so I want to be sure it is done in a clean way.
from tkinter import * from tkinter import messagebox import json import requests import time import os ws = Tk() ws.title('Relay Control') ws.geometry("600x400") ws.configure(background='white') on = PhotoImage(file = "lvgRoom_on.png") off = PhotoImage(file = "lvgRoom_off.png") sonoff01 = 'http://192.168.0.113/cm?cmnd=status' sonURL = 'NOT_INIT' sonURL = sonoff01 sonoff01 = requests.get(sonURL) x = json.loads(sonoff01.text) status = x['Status'] is_on = status["Power"] #New Section for TEMPS -------------------------------------------- sonoffTemp = 'http://192.168.0.113/cm?cmnd=status+10' tempURL = 'NOT_INIT' tempURL = sonoffTemp sonoffTemp = requests.get(tempURL) i = json.loads(sonoffTemp.text) statusT = i["StatusSNS"] timestamp = statusT["Time"] device_names = list(statusT.keys())[1:-1] temp_units = statusT["TempUnit"] templabel = Label(text="temp info", bg="White", fg="Black", font=("Helvetica", 12)) templabel.place(x=100, y=10) print(timestamp) #--------------------- for name in device_names: device = statusT[name] #print(f'ID={name}, Temperature={device["Temperature"]} {temp_units}') templabel.configure(text='Living Room '+f'Temperature: {device["Temperature"]} {temp_units}') def Switch(): global is_on if is_on: button.config(image = off) label.config(text = "Switch is Off", fg = "grey", bg='white') requests.post('http://192.168.0.113/cm?cmnd=Power Off') is_on = False else: button.config(image = on) label.config(text = "Switch is On", fg = "green", bg='white') requests.post('http://192.168.0.113/cm?cmnd=Power On') is_on = True if is_on == True: button = Button(ws, image = on, bg='white', bd = 0, command = Switch) button.pack(pady = 30) label = Label(ws, text = "Switch Is On!", bg='white', fg = "green", font = ("Helvetica", 32)) label.pack(pady = 20) else: button = Button(ws, image = off, bg='white', bd = 0, command = Switch) button.pack(pady = 30) label = Label(ws, text = "Switch Is Off!", bg='white', fg = "grey", font = ("Helvetica", 32)) label.pack(pady = 20) ws.mainloop()