Read JSON file into variables?  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
doh
Posts: 82
Joined: Monday 01 December 2014 13:28
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: London, UK
Contact:

Read JSON file into variables?

Post by doh »

Hi

I'm trying to use pynetgear_enhanced to retrieve a list of connected devices from my NetGear Router and then use this to update presence status in Domoticz.

I've got a cron job running every minute that retrieves the list of connected devices and writes it to a JSON text file.

How can I read this JSON text file into dzVents so I can then process the contents?

Thanks

Here's a sample extract of the file:

Code: Select all

[
  {
    "name": "SonyBravia",
    "ip": "192.168.1.176",
    "mac": "04:5D:4B:DA:52:B1",
    "type": "wired",
    "signal": 100,
    "link_rate": null,
    "allow_or_block": "Allow",
    "device_type": null,
    "device_model": null,
    "ssid": null,
    "conn_ap_mac": null
  },
  {
    "name": "XBOXONE",
    "ip": "192.168.1.120",
    "mac": "94:9A:A9:2E:33:9F",
    "type": "wired",
    "signal": 100,
    "link_rate": null,
    "allow_or_block": "Allow",
    "device_type": null,
    "device_model": null,
    "ssid": null,
    "conn_ap_mac": null
  }
]
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Read JSON file into variables?  [Solved]

Post by waaren »

doh wrote: Tuesday 28 July 2020 17:07 How can I read this JSON text file into dzVents so I can then process the contents?
Use below bash script example

Code: Select all

#!/bin/bash
#
# signal dzVents about a JSON in an OS file
#

#
# vars to be changed
#
JSONFile='/opt/domoticz/myJSONFile' # change to full qualified filename
domoticzPort=8084                   # change to your domoticz port number
domoticzIP=127.0.0.1                # change to domoticz IP (keep 127.0.0.1 if local)


#
# Keep this the same as the trigger of the dzVents script
#
customEvent=JSONFileReady

#
# signal dzVents 
#
curl "http://$domoticzIP:$domoticzPort/json.htm?type=command&param=customevent&event=$customEvent&data=$JSONFile"


and this dzVents

Code: Select all

return 
{
    on =
    {  
        customEvents =
        { 
            'JSONFileReady',
        },
    },

    logging =
    {  
        level = domoticz.LOG_DEBUG,
        marker = 'Read JSON file',
    },

    execute = function(dz, item)

        local function readFile(f)
            local file = io.open (f, 'r') -- open file in read mode
            local data =  file:read('*a')  -- read all
            file:close()
            return data
        end    
        
        --main
        local convertedJSON = dz.utils.fromJSON(readFile(item.data))
        dz.utils.dumpTable(convertedJSON)
    end
}

Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
doh
Posts: 82
Joined: Monday 01 December 2014 13:28
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: London, UK
Contact:

Re: Read JSON file into variables?

Post by doh »

Thanks - I've moved to using a customEvent now, so the routine only runs when the Cron job completes.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest