Python Forum
Need help with coding in script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with coding in script
#8
(Feb-03-2023, 08:29 PM)deanhystad Wrote:
import json

with open("test.txt", "r") as file:
    json_data = json.load(file)
    states = json_data["states"][0]["state"]

for state in states:
    print(
        "id =", state["id"][0], "value =", state["tempValue"][0], "dhw", state["dhw"][0]
    )
Output:
id = 0 value = 2000 dhw 1 id = 1 value = 1900 dhw 1 id = 2 value = 1700 dhw 1 id = 3 value = 1800 dhw 1 id = 4 value = 1000 dhw 1

Many thanks for your support.
This helped a lot

    def onMessagetoonSceneinfo(self, Connection, Response):	
        Domoticz.Debug("onMessagetoonSceneinfo called")
        if 'states' in Response:
            #this message contains the scenes
            Domoticz.Debug("onMessagetoonSceneinfo processing list of scenes")
            for state in Response["states"][0]["state"]:
                Domoticz.Log("id ="+ state["id"][0] + " Temp =" + state["tempValue"][0])
Output:
2023-02-04 09:58:02.200 Toon: id =0 Temp =2000 2023-02-04 09:58:02.200 Toon: id =1 Temp =1900 2023-02-04 09:58:02.200 Toon: id =2 Temp =1700 2023-02-04 09:58:02.200 Toon: id =3 Temp =1800 2023-02-04 09:58:02.201 Toon: id =4 Temp =1000
Still getting this error

Error:
TypeError: list indices must be integers or slices, not str
How can i solve this?
Reply


Messages In This Thread
Need help with coding in script - by madpatrick - Feb-03-2023, 07:33 PM
RE: Need help with coding in script - by buran - Feb-03-2023, 07:39 PM
RE: Need help with coding in script - by madpatrick - Feb-03-2023, 07:43 PM
RE: Need help with coding in script - by deanhystad - Feb-03-2023, 08:13 PM
RE: Need help with coding in script - by madpatrick - Feb-03-2023, 08:21 PM
RE: Need help with coding in script - by deanhystad - Feb-03-2023, 08:29 PM
RE: Need help with coding in script - by madpatrick - Feb-04-2023, 08:59 AM
RE: Need help with coding in script - by snippsat - Feb-03-2023, 10:23 PM
RE: Need help with coding in script - by snippsat - Feb-04-2023, 12:14 PM
RE: Need help with coding in script - by madpatrick - Feb-05-2023, 04:24 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  coding error from a script (absolute noob) fuchls 2 3,960 Jun-08-2018, 02:29 PM
Last Post: webrunner1981

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020