Decoding JSON to LUA

Moderator: leecollings

Post Reply
Jumper3126
Posts: 105
Joined: Thursday 31 December 2015 15:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Decoding JSON to LUA

Post by Jumper3126 »

I try to get the mean temperature from wunderground. Following example online I should be using the following code.

Code: Select all

json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() 
local key = xxxxxxxxxxxx"
local location = "xxxxxxxxxxxx" 
local file=assert(io.popen('curl http://api.wunderground.com/api/'..key..'/history_20171024/q/CA/'..location..'.json'))
local raw = file:read('*all')
file:close()
local deviceinfo = json:decode(raw)
local meantemp = deviceinfo.history.dailysummary.0.meantempm
It appears however not to accept the 0 in the last row. I get this errror code. Any suggestions??
2017-11-14 20:27:13.839 Error: dzVents: Error: error loading module 'V Degreedays' from file '/home/pi/domoticz/scripts/dzVents/generated_scripts/V Degreedays.lua':
...oticz/scripts/dzVents/generated_scripts/V Degreedays.lua:38: expected near ''0''
The Json file looks like this:

Code: Select all

response	
history
	date	{…}
	utcdate	{…}
	observations	[…]
	dailysummary	
		0	
			date	{…}
			utcdate	{…}
			meantempm	"14.8"
Last edited by Jumper3126 on Tuesday 14 November 2017 20:51, edited 1 time in total.
Jumper3126
Posts: 105
Joined: Thursday 31 December 2015 15:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Decoding JSON to LUA

Post by Jumper3126 »

Sorry, I might not have my terminology right. I'm not a professional and just try to get to my desired end goal.
For now, that is to retrieve the mean temperature of a certain date from wunderground.
I know I need to select each "subdirectory" (deviceinfo, history, dailysummary,0) untill I get to meantempm, but the zero seams not to be accepted. I have tried already with '0' and (0), but that didnt work.
There is probably a very simple solution for this.
Jumper3126
Posts: 105
Joined: Thursday 31 December 2015 15:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Decoding JSON to LUA

Post by Jumper3126 »

Thanks for the suggestion. Unfortunatly that doesnt work. If I use:

Code: Select all

local meantemp = deviceinfo['history']['dailysummary']['0']['meantempm']
I get this error:
dzVents: Error: ...oticz/scripts/dzVents/generated_scripts/V Degreedays.lua:44: attempt to index field '0' (a nil value)
And if I use:

Code: Select all

local meantemp = deviceinfo['history']['dailysummary'][0]['meantempm']
I get:
dzVents: Error: ...oticz/scripts/dzVents/generated_scripts/V Degreedays.lua:44: attempt to index field '?' (a nil value)
Besides, this LUA script http://domoticz.com/wiki/Real-time_sola ... _sensor... doesnt use the straight brackets to get data from wunderground.

Code: Select all

local latitude = jsonLocation.current_observation.display_location.latitude
Jumper3126
Posts: 105
Joined: Thursday 31 December 2015 15:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Decoding JSON to LUA

Post by Jumper3126 »

I got the dump from
I made a full copy, perhaps this is helpful.

Code: Select all

{
  "response": {
  "version":"0.1",
  "termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
  "features": {
  "history": 1
  }
	}
		,
	"history": {
		"date": {
		"pretty": "November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "00",
		"min": "00",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "November 14, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "14",
		"hour": "23",
		"min": "00",
		"tzname": "UTC"
		},
		"observations": [
		{
		"date": {
		"pretty": "12:15 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "12",
		"min": "15",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "11:15 AM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "11",
		"min": "15",
		"tzname": "UTC"
		},
		"tempm":"10.1", "tempi":"50.2","dewptm":"10.0", "dewpti":"50.0","hum":"99","wspdm":"-1608.8", "wspdi":"-999.9","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"-9999","wdire":"North","pressurem":"1028.0", "pressurei":"30.36","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"-2539.7", "precip_ratei":"-99.99","precip_totalm":"-2539.7", "precip_totali":"-99.99","solarradiation":"","UV":"1290","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "12:20 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "12",
		"min": "20",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "11:20 AM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "11",
		"min": "20",
		"tzname": "UTC"
		},
		"tempm":"10.2", "tempi":"50.3","dewptm":"10.0", "dewpti":"50.0","hum":"99","wspdm":"-1608.8", "wspdi":"-999.9","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"-9999","wdire":"North","pressurem":"1028.0", "pressurei":"30.36","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"-2539.7", "precip_ratei":"-99.99","precip_totalm":"-2539.7", "precip_totali":"-99.99","solarradiation":"","UV":"1265","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "12:25 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "12",
		"min": "25",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "11:25 AM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "11",
		"min": "25",
		"tzname": "UTC"
		},
		"tempm":"10.3", "tempi":"50.5","dewptm":"10.1", "dewpti":"50.2","hum":"99","wspdm":"-1608.8", "wspdi":"-999.9","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"-9999","wdire":"North","pressurem":"1028.0", "pressurei":"30.36","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"-2539.7", "precip_ratei":"-99.99","precip_totalm":"-2539.7", "precip_totali":"-99.99","solarradiation":"","UV":"1279","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "12:30 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "12",
		"min": "30",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "11:30 AM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "11",
		"min": "30",
		"tzname": "UTC"
		},
		"tempm":"10.3", "tempi":"50.6","dewptm":"10.2", "dewpti":"50.3","hum":"99","wspdm":"0.0", "wspdi":"0.0","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"0","wdire":"North","pressurem":"1028.0", "pressurei":"30.36","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"0.0", "precip_ratei":"0.00","precip_totalm":"0.0", "precip_totali":"0.00","solarradiation":"","UV":"1394","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "12:35 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "12",
		"min": "35",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "11:35 AM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "11",
		"min": "35",
		"tzname": "UTC"
		},
		"tempm":"10.4", "tempi":"50.7","dewptm":"10.3", "dewpti":"50.5","hum":"99","wspdm":"0.0", "wspdi":"0.0","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"0","wdire":"North","pressurem":"1028.0", "pressurei":"30.36","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"0.0", "precip_ratei":"0.00","precip_totalm":"0.0", "precip_totali":"0.00","solarradiation":"","UV":"1394","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "12:41 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "12",
		"min": "41",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "11:41 AM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "11",
		"min": "41",
		"tzname": "UTC"
		},
		"tempm":"10.4", "tempi":"50.8","dewptm":"10.3", "dewpti":"50.5","hum":"99","wspdm":"0.5", "wspdi":"0.3","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"315","wdire":"NW","pressurem":"1028.0", "pressurei":"30.36","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"0.0", "precip_ratei":"0.00","precip_totalm":"0.0", "precip_totali":"0.00","solarradiation":"","UV":"1336","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "12:46 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "12",
		"min": "46",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "11:46 AM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "11",
		"min": "46",
		"tzname": "UTC"
		},
		"tempm":"10.6", "tempi":"51.0","dewptm":"10.4", "dewpti":"50.7","hum":"99","wspdm":"3.1", "wspdi":"1.9","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"270","wdire":"West","pressurem":"1028.0", "pressurei":"30.36","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"0.0", "precip_ratei":"0.00","precip_totalm":"0.0", "precip_totali":"0.00","solarradiation":"","UV":"1336","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "12:51 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "12",
		"min": "51",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "11:51 AM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "11",
		"min": "51",
		"tzname": "UTC"
		},
		"tempm":"10.6", "tempi":"51.0","dewptm":"10.4", "dewpti":"50.7","hum":"99","wspdm":"3.5", "wspdi":"2.2","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"225","wdire":"SW","pressurem":"1028.0", "pressurei":"30.36","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"0.0", "precip_ratei":"0.00","precip_totalm":"0.0", "precip_totali":"0.00","solarradiation":"","UV":"1334","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "12:56 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "12",
		"min": "56",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "11:56 AM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "11",
		"min": "56",
		"tzname": "UTC"
		},
		"tempm":"10.6", "tempi":"51.1","dewptm":"10.4", "dewpti":"50.8","hum":"99","wspdm":"3.2", "wspdi":"2.0","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"270","wdire":"West","pressurem":"1028.0", "pressurei":"30.36","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"0.0", "precip_ratei":"0.00","precip_totalm":"0.0", "precip_totali":"0.00","solarradiation":"","UV":"1345","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "1:01 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "13",
		"min": "01",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "12:01 PM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "12",
		"min": "01",
		"tzname": "UTC"
		},
		"tempm":"10.6", "tempi":"51.1","dewptm":"10.4", "dewpti":"50.8","hum":"99","wspdm":"-1608.8", "wspdi":"-999.9","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"-9999","wdire":"North","pressurem":"1028.0", "pressurei":"30.36","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"-2539.7", "precip_ratei":"-99.99","precip_totalm":"-2539.7", "precip_totali":"-99.99","solarradiation":"","UV":"1266","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "1:06 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "13",
		"min": "06",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "12:06 PM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "12",
		"min": "06",
		"tzname": "UTC"
		},
		"tempm":"10.6", "tempi":"51.1","dewptm":"10.5", "dewpti":"50.9","hum":"99","wspdm":"-1608.8", "wspdi":"-999.9","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"-9999","wdire":"North","pressurem":"1028.0", "pressurei":"30.36","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"-2539.7", "precip_ratei":"-99.99","precip_totalm":"-2539.7", "precip_totali":"-99.99","solarradiation":"","UV":"1246","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "1:12 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "13",
		"min": "12",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "12:12 PM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "12",
		"min": "12",
		"tzname": "UTC"
		},
		"tempm":"10.7", "tempi":"51.2","dewptm":"10.5", "dewpti":"50.9","hum":"99","wspdm":"-1608.8", "wspdi":"-999.9","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"-9999","wdire":"North","pressurem":"1028.0", "pressurei":"30.36","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"-2539.7", "precip_ratei":"-99.99","precip_totalm":"-2539.7", "precip_totali":"-99.99","solarradiation":"","UV":"1269","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "1:20 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "13",
		"min": "20",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "12:20 PM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "12",
		"min": "20",
		"tzname": "UTC"
		},
		"tempm":"10.8", "tempi":"51.4","dewptm":"10.7", "dewpti":"51.2","hum":"99","wspdm":"-1608.8", "wspdi":"-999.9","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"-9999","wdire":"North","pressurem":"1028.0", "pressurei":"30.36","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"-2539.7", "precip_ratei":"-99.99","precip_totalm":"-2539.7", "precip_totali":"-99.99","solarradiation":"","UV":"1266","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "1:25 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "13",
		"min": "25",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "12:25 PM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "12",
		"min": "25",
		"tzname": "UTC"
		},
		"tempm":"10.8", "tempi":"51.4","dewptm":"10.6", "dewpti":"51.1","hum":"99","wspdm":"-1608.8", "wspdi":"-999.9","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"-9999","wdire":"North","pressurem":"1028.0", "pressurei":"30.36","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"-2539.7", "precip_ratei":"-99.99","precip_totalm":"-2539.7", "precip_totali":"-99.99","solarradiation":"","UV":"1247","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "1:30 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "13",
		"min": "30",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "12:30 PM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "12",
		"min": "30",
		"tzname": "UTC"
		},
		"tempm":"10.8", "tempi":"51.5","dewptm":"10.7", "dewpti":"51.2","hum":"99","wspdm":"-1608.8", "wspdi":"-999.9","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"-9999","wdire":"North","pressurem":"1028.0", "pressurei":"30.36","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"-2539.7", "precip_ratei":"-99.99","precip_totalm":"-2539.7", "precip_totali":"-99.99","solarradiation":"","UV":"1252","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "1:36 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "13",
		"min": "36",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "12:36 PM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "12",
		"min": "36",
		"tzname": "UTC"
		},
		"tempm":"10.8", "tempi":"51.5","dewptm":"10.7", "dewpti":"51.2","hum":"99","wspdm":"-1608.8", "wspdi":"-999.9","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"-9999","wdire":"North","pressurem":"1028.0", "pressurei":"30.36","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"-2539.7", "precip_ratei":"-99.99","precip_totalm":"-2539.7", "precip_totali":"-99.99","solarradiation":"","UV":"1275","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "1:41 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "13",
		"min": "41",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "12:41 PM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "12",
		"min": "41",
		"tzname": "UTC"
		},
		"tempm":"10.8", "tempi":"51.5","dewptm":"10.7", "dewpti":"51.3","hum":"99","wspdm":"-1608.8", "wspdi":"-999.9","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"-9999","wdire":"North","pressurem":"1028.0", "pressurei":"30.36","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"-2539.7", "precip_ratei":"-99.99","precip_totalm":"-2539.7", "precip_totali":"-99.99","solarradiation":"","UV":"1282","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "1:46 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "13",
		"min": "46",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "12:46 PM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "12",
		"min": "46",
		"tzname": "UTC"
		},
		"tempm":"10.8", "tempi":"51.5","dewptm":"10.7", "dewpti":"51.2","hum":"99","wspdm":"-1608.8", "wspdi":"-999.9","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"-9999","wdire":"North","pressurem":"1027.0", "pressurei":"30.33","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"-2539.7", "precip_ratei":"-99.99","precip_totalm":"-2539.7", "precip_totali":"-99.99","solarradiation":"","UV":"1255","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "1:51 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "13",
		"min": "51",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "12:51 PM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "12",
		"min": "51",
		"tzname": "UTC"
		},
		"tempm":"10.8", "tempi":"51.4","dewptm":"10.6", "dewpti":"51.1","hum":"99","wspdm":"-1608.8", "wspdi":"-999.9","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"-9999","wdire":"North","pressurem":"1028.0", "pressurei":"30.36","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"-2539.7", "precip_ratei":"-99.99","precip_totalm":"-2539.7", "precip_totali":"-99.99","solarradiation":"","UV":"1260","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "1:56 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "13",
		"min": "56",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "12:56 PM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "12",
		"min": "56",
		"tzname": "UTC"
		},
		"tempm":"10.8", "tempi":"51.4","dewptm":"10.6", "dewpti":"51.1","hum":"99","wspdm":"-1608.8", "wspdi":"-999.9","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"-9999","wdire":"North","pressurem":"1028.0", "pressurei":"30.36","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"-2539.7", "precip_ratei":"-99.99","precip_totalm":"-2539.7", "precip_totali":"-99.99","solarradiation":"","UV":"1261","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "2:01 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "14",
		"min": "01",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "1:01 PM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "13",
		"min": "01",
		"tzname": "UTC"
		},
		"tempm":"10.8", "tempi":"51.4","dewptm":"10.6", "dewpti":"51.1","hum":"99","wspdm":"-1608.8", "wspdi":"-999.9","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"-9999","wdire":"North","pressurem":"1028.0", "pressurei":"30.36","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"-2539.7", "precip_ratei":"-99.99","precip_totalm":"-2539.7", "precip_totali":"-99.99","solarradiation":"","UV":"1259","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "2:07 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "14",
		"min": "07",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "1:07 PM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "13",
		"min": "07",
		"tzname": "UTC"
		},
		"tempm":"10.8", "tempi":"51.4","dewptm":"10.6", "dewpti":"51.1","hum":"99","wspdm":"-1608.8", "wspdi":"-999.9","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"-9999","wdire":"North","pressurem":"1027.0", "pressurei":"30.33","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"-2539.7", "precip_ratei":"-99.99","precip_totalm":"-2539.7", "precip_totali":"-99.99","solarradiation":"","UV":"1256","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "2:16 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "14",
		"min": "16",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "1:16 PM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "13",
		"min": "16",
		"tzname": "UTC"
		},
		"tempm":"11.0", "tempi":"51.8","dewptm":"10.9", "dewpti":"51.6","hum":"99","wspdm":"-1608.8", "wspdi":"-999.9","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"-9999","wdire":"North","pressurem":"1027.0", "pressurei":"30.33","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"-2539.7", "precip_ratei":"-99.99","precip_totalm":"-2539.7", "precip_totali":"-99.99","solarradiation":"","UV":"1257","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "2:22 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "14",
		"min": "22",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "1:22 PM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "13",
		"min": "22",
		"tzname": "UTC"
		},
		"tempm":"10.9", "tempi":"51.6","dewptm":"10.8", "dewpti":"51.4","hum":"99","wspdm":"-1608.8", "wspdi":"-999.9","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"-9999","wdire":"North","pressurem":"1027.0", "pressurei":"30.33","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"-2539.7", "precip_ratei":"-99.99","precip_totalm":"-2539.7", "precip_totali":"-99.99","solarradiation":"","UV":"1258","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "2:31 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "14",
		"min": "31",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "1:31 PM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "13",
		"min": "31",
		"tzname": "UTC"
		},
		"tempm":"10.9", "tempi":"51.6","dewptm":"10.8", "dewpti":"51.4","hum":"99","wspdm":"-1608.8", "wspdi":"-999.9","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"-9999","wdire":"North","pressurem":"1027.0", "pressurei":"30.33","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"-2539.7", "precip_ratei":"-99.99","precip_totalm":"-2539.7", "precip_totali":"-99.99","solarradiation":"","UV":"1257","softwaretype":"BloomSky" },
		{
		"date": {
		"pretty": "2:36 PM CET on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "14",
		"min": "36",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "1:36 PM GMT on November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "13",
		"min": "36",
		"tzname": "UTC"
		},
		"tempm":"10.9", "tempi":"51.6","dewptm":"10.8", "dewpti":"51.4","hum":"99","wspdm":"-1608.8", "wspdi":"-999.9","wgustm":"-1607.4", "wgusti":"-999.0","wdird":"-9999","wdire":"North","pressurem":"1027.0", "pressurei":"30.33","windchillm":"-999", "windchilli":"-999","heatindexm":"-9999", "heatindexi":"-9999","precip_ratem":"-2539.7", "precip_ratei":"-99.99","precip_totalm":"-2539.7", "precip_totali":"-99.99","solarradiation":"","UV":"1258","softwaretype":"BloomSky" }
		],
		"dailysummary": [
		{ "date": {
		"pretty": "November 15, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "15",
		"hour": "00",
		"min": "00",
		"tzname": "Europe/Amsterdam"
		},
		"utcdate": {
		"pretty": "November 14, 2017",
		"year": "2017",
		"mon": "11",
		"mday": "14",
		"hour": "23",
		"min": "00",
		"tzname": "UTC"
		},
		"meantempm":"10.6", "meantempi":"51.0","meandewptm":"10.5", "meandewpti":"50.9","meanwindspdm":"0.4", "meanwindspdi":"0.2","meanwdire":"WSW","meanwdird":"","humidity":"99","maxtempm":"11.0", "maxtempi":"51.8","mintempm":"10.1", "mintempi":"50.2","maxhumidity":"99","minhumidity":"99","maxdewptm":"10.9", "maxdewpti":"51.6","mindewptm":"10.0", "mindewpti":"50.0","maxpressurem":"1028.0", "maxpressurei":"30.36","minpressurem":"1027.0", "minpressurei":"30.33","maxwspdm":"3.5", "maxwspdi":"2.2","precipm":"0.0", "precipi":"0.00" }
		]
	}
}
Jumper3126
Posts: 105
Joined: Thursday 31 December 2015 15:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Decoding JSON to LUA

Post by Jumper3126 »

This is by the way the script I'm working on. To calculate daily the gas usage per degreesdays.
Except for this issue, it should be working.

Code: Select all

return {
	active = true,

	on = {
		timer = {
			--'at 23:58'
			'every minute'
		}
	},
	execute = function(domoticz)
        local Date = domoticz.time.year..domoticz.time.month..domoticz.time.day

	    json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() 
        local key = "CODE"
        local location = "pws:IDIEMEN13" --plaatsnaam of personal weather station invoeren
	    local file=assert(io.popen('curl http://api.wunderground.com/api/'..key..'/history_'..Date..'/q/CA/'..location..'.json')) 
	    local raw = file:read('*all')
	    file:close()
	    local deviceinfo = json:decode(raw)
        local meantempOutside = 5  -- temporary untill issue of collecting data is solved

--	    local meantempOutside = deviceinfo['history']['dailysummary'][0]['meantempm']
--      local meantempOutside = deviceinfo.history.dailysummary.0.meantempm
--      local meantempOutside = deviceinfo['history']['dailysummary'][0]['meantempm']
        domoticz.log('test meantemp:'..meantempOutside)

        local meantempInside = 18
        local factor = 1
        local degreesdays = meantempInside - meantempOutside
        if (domoticz.time.month >= 4 and domoticz.time.month <= 9) then factor = 0.8 end
        if (domoticz.time.month >= 11 or domoticz.time.month <= 2) then factor = 1.1 end
        degreesdays = degreesdays * factor
--        domoticz.log('test degreesday:'..degreesdays)
        local GasUsage = domoticz.devices('P1 Gas meter').counterToday
        local GasUsageperDegreesday = GasUsage / degreesdays
        if (degreesdays <= 0) then 
            degreesday = 0 
            GasUsageperDegreesday = 0
        end
        domoticz.log('test GasUsage:'..GasUsage)
        domoticz.log('test degreesday:'..degreesdays)
        domoticz.log('test GasUsageperDegreesday:'..GasUsageperDegreesday)
        
        domoticz.devices('Daily Gas/DG').updateCustomSensor(GasUsageperDegreesday)
        domoticz.devices('Degreesdays').updateTemperature(degreesdays)

	end
}
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest