Shelly 1 reading json status in Python

  • Hi All... I am hoping someone else has tried reading the json messages from the Shelly 1 into Python. I am creating a small GUI to try and get the status of the relay before sending a command thru URL/HTTP request. I can return status from Python, but cannot determine which portion I need to try and read so I can see the json part of it "ison"=true or false. If anyone can assist me with this, that would be great.

    Einmal editiert, zuletzt von Mikeg71 (23. Januar 2022 um 03:37)

  • You can use the urllib.request and json lib

    Here an little Example:


    [script][/script]

    [script]
    [/script]

    [script]import urllib.request as request [/script]

    [script]import json [/script]

    [script]
    [/script]

    [script]with request.urlopen('192.168.xx.xxx/status') as response: [/script]

    [script]if response.getcode() == 200: [/script]

    [script]source = response.read() [/script]

    [script]data = json.loads(source) [/script]

    [script]else: [/script]

    [script]print('An error occurred while attempting to retrieve data from the API.')[/script]

    [script]
    [/script]

    [script][/script]

  • To Read objects:

    example:

    shelly_api = {}

    for i in range(len(data)):

    team, api_endpoint = data[i]

    if api_endpoint not in shelly_api.keys():

    shelly_api[api_endpoint] = []

    shelly_api[api_endpoint].append(status)