Failing at parsing some data from json reply (shelly 2) Topic is solved

Moderator: leecollings

Post Reply
midomot
Posts: 20
Joined: Sunday 12 February 2017 13:27
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Failing at parsing some data from json reply (shelly 2)

Post by midomot »

Pi with Domoticz V4.10301

Good afternoon
I have a Shelly 2 in my house (https://shelly.cloud/ very nice, can also be flashed with Tasmota, but not compulsory as mqtt can be enabled)

Found out I can query some data using http://192.168.178.79/status where 192... is the IP address of this switch with has 2 relays
The output is in json.
I managed to retrieve a number of data via lua, but fail to get data for individual relays even though all data is visible in the json feedback

Data retrieved from 192..., in raw data:
{"wifi_sta":{"connected":true,"ssid":"MYWIFISSID","ip":"192.168.178.79"},"cloud":{"enabled":false,"connected":false},"mqtt":{"connected":true},"time":"14:35","serial":1,"has_update":true,"mac":"MACOFTHESWITCH","relays":[{"ison":false,"has_timer":false,"overpower":false,"is_valid":true},{"ison":false,"has_timer":false,"overpower":false,"is_valid":true}],"rollers":[{"state":"stop","power":0.00,"is_valid":true,"safety_switch":false,"stop_reason":"normal","last_direction":"stop","current_pos":101,"calibrating":false,"positioning":true}],"meters":[{"power":0.00,"is_valid":true,"timestamp":1545921351,"counters":[0.000, 0.000, 0.000],"total":0}],"update":{"status":"pending","has_update":true,"new_version":"20181217-130502/v1.4.2@cc724b51","old_version":"20181219-171759/wifi_disconnects_again@90290083"},"ram_total":50488,"ram_free":38324,"fs_size":233681,"fs_free":158381,"uptime":86594}
the above makes is hard to see, so here is a screenshot with a much clearer json structure, I collapsed a few lines towards the end

Image

What I'm trying to retrieve, and so fail at, is the status for the individual relays: i.e. retrieve separately the "ison" information for relay 0 and relay 1

This is my code, the lines with ison are failing.. values 0 and 1 are clearly unacceptable !
How then do I point to the right information ?!

Thanks to whoever will kindly help me out !

Code: Select all

commandArray = {}
if devicechanged['TestSwitch3'] then
    json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()  -- For Linux    
    --  API call
    local config=assert(io.popen('curl "http://192.168.178.79/status"'))
    local result = config:read('*a')
    config:close()
    output = json:decode(result)

    MyUptime= output.uptime
    SoftwareCurrent=output.update.old_version
    SoftwareNew=output.update.new_version
    SwitchHallStatus=output.relays.0.ison
    SwitchPorchStatus=output.relays.1.ison
    
    print(MyUptime)
    print(SoftwareCurrent)
    print(SoftwareNew)
    print('SwitchHall is '..SwitchHallStatus)
    print('SwitchPorch is '..SwitchPorchStatus)
   
end  
return commandArray
elmortero
Posts: 247
Joined: Sunday 29 November 2015 20:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.9639
Location: Spain
Contact:

Re: Failing at parsing some data from json reply (shelly 2)

Post by elmortero »

Give it a try with this:
SwitchHallStatus=output.relays[1].ison
SwitchPorchStatus=output.relays[2].ison
User avatar
jvdz
Posts: 2328
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Failing at parsing some data from json reply (shelly 2)

Post by jvdz »

something like this?:

Code: Select all

MyUptime= output.uptime
SoftwareCurrent=output.update.old_version
SoftwareNew=output.update.new_version
SwitchHallStatus=output.relays[1].ison
SwitchPorchStatus=output.relays[2].ison

print(MyUptime)
print(SoftwareCurrent)
print(SoftwareNew)
print('SwitchHall is '..tostring(SwitchHallStatus))
print('SwitchPorch is '..tostring(SwitchPorchStatus))
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
midomot
Posts: 20
Joined: Sunday 12 February 2017 13:27
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Failing at parsing some data from json reply (shelly 2)

Post by midomot »

this is great, thanks both !!

I had tested SwitchHallStatus=output.relays.[0].ison.. which of course did not work
and if I even had reached the right format for the code, I would have probably gone with SwitchHallStatus=output.relays[0].ison by looking at the json data structure I could see :oops:

Anyway, I can now go further : thank you so much ! :D
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest