Lua 3D array

Moderator: leecollings

JustMitchie
Posts: 15
Joined: Saturday 17 March 2018 23:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Lua 3D array

Post by JustMitchie »

Hello, i am trying to assing the value from my p1 meter, which is a json array

Code: Select all

	
ActTime	1521732455
AstrTwilightEnd	"20:48"
AstrTwilightStart	"04:37"
CivTwilightEnd	"19:26"
CivTwilightStart	"05:59"
DayLength	"12:19"
NautTwilightEnd	"20:06"
NautTwilightStart	"05:19"
ServerTime	"2018-03-22 16:27:35"
SunAtSouth	"12:05"
Sunrise	"06:33"
Sunset	"18:52"
result	
        0	
                AddjMulti	1
                AddjMulti2	1
                AddjValue	0
                AddjValue2	0
                BatteryLevel	255
                Counter	"2323.313"
                CounterToday	"5.603 m3"
                CustomImage	0
                Data	"2323.313"
                Description	""
                Favorite	1
                HardwareID	9
                HardwareName	"Power usage"
                HardwareType	"P1 Smart Meter USB"
                HardwareTypeVal	4
                HaveTimeout	false
                ID	"1"
                LastUpdate	"2018-03-22 16:25:45"
                Name	"Gas"
                Notifications	"false"
                PlanID	"0"
                PlanIDs	
                0	0
                Protected	false
                ShowNotifications	true
                SignalLevel	"-"
                SubType	"Gas"
                SwitchTypeVal	1
                Timers	"false"
                Type	"P1 Smart Meter"
                TypeImg	"counter"
                Unit	2
                Used	0
                XOffset	"0"
                YOffset	"0"
                idx	"20"
status	"OK"
title	"Devices"
In PHP i would use explode i think, but no clue how to do it in Lua. I want to assign the value of CounterToday to otherdevices[Gas}

Can someone point me in the right direction? Kind regards, Michel
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: Lua 3D array

Post by freijn »

not sure how the values are split?

Just by CR/LF ?

here are a lot of examples

http://lua-users.org/wiki/SplitJoin
JustMitchie
Posts: 15
Joined: Saturday 17 March 2018 23:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua 3D array

Post by JustMitchie »

Ah ik zag dat je nederlands bent. Die P1 meter geeft dus een mooie array, maar ik wil dus alleen de currentvalue hebben met de GasMeter heet 'ie CounterToday, Als het een php array was zou je gaan zo van $data[result[0][CounterToday] die wil ik in een virtuele gasmeter stoppen. Bij de stroommeter is er nog eentje, de usage. Die is in php $data[result][0][usage]

Maar bij die stroommeter moeten er dus twee values in (sValue voor die tweede?) nogmaals, geen idee hoe lua werkt ;( thanks for the help
JustMitchie
Posts: 15
Joined: Saturday 17 March 2018 23:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua 3D array

Post by JustMitchie »

print(' test gas')
commandArray = {}
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() -- For Linux
local config=assert(io.popen('curl "http://username:password:192.168.2.15:8080/json.htm?type=devices&rid=20"')) -- notice double quotes
local data = config:read('*all')
config:close()
local jsonValue = json:decode(data)

value = jsonValue.result.CounterToday

print(value)
return commandArray
This is what i tried in Lua, however i get a Error: EventSystem: in Gas_Update: [string "--..."]:27: attempt to call a nil value
in the log.
User avatar
jvdz
Posts: 2335
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Lua 3D array

Post by jvdz »

Can you share the actual JSON data as the posted data in not in JSON format?

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
JustMitchie
Posts: 15
Joined: Saturday 17 March 2018 23:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua 3D array

Post by JustMitchie »

http://forum:forum:ip:port/json.htm?type=devices&rid=19

Is the url, it has viewing rights on the utilities page. However, when i try it, it goes straight to google. I am probably looking at a typo here, but the ip is
84.81.105.118:8080 the pass is forum the user is forum and the idx is 19 for the power meter, 20 for the gas meter.

I hope that this is more convenient than uploading a imgurl....

Regards, michel

mag in het nederlands, engels maakt mij niet uit. ;)
Last edited by JustMitchie on Thursday 29 March 2018 13:09, edited 1 time in total.
JustMitchie
Posts: 15
Joined: Saturday 17 March 2018 23:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua 3D array

Post by JustMitchie »

https://imgur.com/a/C8h2F <- imgur link with screenshots made from the browser of the kwh meter.
it has both values. CounterToday and Usage.

regards, michel
User avatar
jvdz
Posts: 2335
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Lua 3D array

Post by jvdz »

English is fine ...no worries. ;)

This should work to decode:

Code: Select all

local jsonData = json:decode(Stringjson)
print(jsonData.result[1].CounterToday)
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
JustMitchie
Posts: 15
Joined: Saturday 17 March 2018 23:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua 3D array

Post by JustMitchie »

jvdz wrote: Thursday 22 March 2018 19:42 English is fine ...no worries. ;)

This should work to decode:

Code: Select all

local jsonData = json:decode(Stringjson)
print(jsonData.result[1].CounterToday)
Jos
This now makes

Code: Select all

print(' test gas')
commandArray = {}
   json = (loadfile "/home/michel/domoticz/scripts/lua/JSON.lua")  -- For Linux
   local config=assert(io.popen('curl "http://forum:forum:192.168.2.15:8080/json.htm?type=devices&rid=20"')) -- notice double quotes
   local Stringjson = config:read('*all')
   print(Stringjson)
   config:close()
   local jsonData = json:decode(Stringjson)
   print(jsonData.result[1].CounterToday)
return commandArray
and results in

Code: Select all

2018-03-22 20:00:50.557 LUA: test gas
2018-03-22 20:00:50.780 LUA:
2018-03-22 20:00:50.780 Error: EventSystem: in Gas_Update: [string "--..."]:32: attempt to index global 'json' (a function value) 
i am getting better at this.

Kind regards,

Michel
User avatar
jvdz
Posts: 2335
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Lua 3D array

Post by jvdz »

Think the URL needs to be:

Code: Select all

local config=assert(io.popen('curl "http://forum:[email protected]:8080/json.htm?type=devices&rid=20"')) -- notice double quotes
it works for me with:

Code: Select all

local config=assert(io.popen('curl "http://forum:[email protected]:yyyy/json.htm?type=devices&rid=19"')) -- notice double quotes
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
JustMitchie
Posts: 15
Joined: Saturday 17 March 2018 23:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua 3D array

Post by JustMitchie »

jvdz wrote: Thursday 22 March 2018 20:13 Think the URL needs to be:

Code: Select all

local config=assert(io.popen('curl "http://forum:[email protected]:8080/json.htm?type=devices&rid=20"')) -- notice double quotes
it works for me with:

Code: Select all

local config=assert(io.popen('curl "http://forum:[email protected]:8080/json.htm?type=devices&rid=19"')) -- notice double quotes
Jos
Now its breaking at line 8 (i removed the top comments so the script is shorter)

Code: Select all

[string "print(' test gas')..."]:8: attempt to index local 'json' (a function value)
local jsonData = json:decode(Stringjson)

local json = (loadfile "/home/michel/domoticz/scripts/lua/JSON.lua")  -- For Linux <-straight from domoticz lua example it there was without local in front of it
i thought i was getting it, im totally lost here.... :(
Last edited by JustMitchie on Thursday 29 March 2018 13:18, edited 1 time in total.
User avatar
jvdz
Posts: 2335
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Lua 3D array

Post by jvdz »

I don't know where you have stored the JSON.lua lib but this is what works for me:

Code: Select all

json = (loadfile "/usr/local/share/lua/5.2/JSON.lua") ()
local config=assert(io.popen('curl "http://xx:yy/json.htm?type=devices&rid=19"')) -- notice double quotes
local Stringjson = config:read('*all')
print(Stringjson)
config:close()
local jsonData = json:decode(Stringjson)
print(jsonData.result[1].CounterToday)
Jos
Last edited by jvdz on Thursday 05 April 2018 14:21, edited 1 time in total.
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
JustMitchie
Posts: 15
Joined: Saturday 17 March 2018 23:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua 3D array

Post by JustMitchie »

jvdz wrote: Thursday 22 March 2018 20:32 I don't know where you have stored the JSON.lua lib but this is what works for me:

Code: Select all

json = (loadfile "/usr/local/share/lua/5.2/JSON.lua") ()
local config=assert(io.popen('curl "http://forum:[email protected]:8080/json.htm?type=devices&rid=19"')) -- notice double quotes
local Stringjson = config:read('*all')
print(Stringjson)
config:close()
local jsonData = json:decode(Stringjson)
print(jsonData.result[1].CounterToday)
Jos
i dont know why, but changing the string to manually typing the same loadfile works.

now it hangs on

Code: Select all

print(jsonData.result[1].CounterToday)

 Error: EventSystem: in Gas_Update: [string "print(' test gas')..."]:9: attempt to index local 'jsonData' (a nil value)
with this as value
so, now its back, to how get the specific value.

Code: Select all

print(' test gas')
commandArray = {}
   json = (loadfile "/home/michel/domoticz/scripts/lua/JSON.lua") ()
   local config=assert(io.popen('curl "http://forum:forum:192.168.2.15:8080/json.htm?type=devices&rid=20"')) -- notice double quotes
   local Stringjson = config:read('*all')
   print(Stringjson)
   config:close()
   local jsonData = json:decode(Stringjson)
   print(jsonData)
   print(jsonData.result[0].CounterToday)
return commandArray
printing the stringJson gives out a proper whitespace printed
and if i do print(jsonValue) it gets the same attempt to index nill value...
Last edited by JustMitchie on Thursday 05 April 2018 10:11, edited 1 time in total.
User avatar
jvdz
Posts: 2335
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Lua 3D array

Post by jvdz »

You still have the wrong url in there. The : after userid:password needs to change to @
So it should look like:

Code: Select all

local config=assert(io.popen('curl "http://forum:forum@xx:yy/json.htm?type=devices&rid=20"')) -- notice double quotes
Jos
Last edited by jvdz on Thursday 05 April 2018 14:21, edited 1 time in total.
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
JustMitchie
Posts: 15
Joined: Saturday 17 March 2018 23:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua 3D array

Post by JustMitchie »

DAT WAS HEM :oops:
explains the straight to google search when entering the url in the browser...
I tried a different way tho, i base64encoded the username and pass, voilla. It works :D which is kinda the same right? thanks for the help Jos! <3
User avatar
jvdz
Posts: 2335
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Lua 3D array

Post by jvdz »

It's always easy and simple when it makes sense. ;)
Enjoy.
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
JustMitchie
Posts: 15
Joined: Saturday 17 March 2018 23:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua 3D array

Post by JustMitchie »

jvdz wrote: Thursday 22 March 2018 21:52 It's always easy and simple when it makes sense. ;)
Enjoy.
Jos
Was het maar zo'n feest....

Code: Select all

 commandArray['Gas'] = jsonData.result[1].CounterToday
does'nt set the Gas meter.... The event fires without errors tho, so i dont have a clue (again :shock: ) about whats going wrong this time..
User avatar
jvdz
Posts: 2335
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Lua 3D array

Post by jvdz »

What about?:

Code: Select all

commandArray['UpdateDevice']=otherdevices_idx['gas']..'|0|'..jsonData.result[1].CounterToday
I think these devices need to SValue.
What I don't understand is why you call it gas while it is a kWh value for electricity. The other thing to consider is that you probably only want the value and not the kWh fed into the device.

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
User avatar
jvdz
Posts: 2335
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Lua 3D array

Post by jvdz »

Check here for the proper format of Electricity devices with JSON but also explains how to code the UpdateDevice:
https://www.domoticz.com/wiki/Domoticz_ ... mart_meter
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
JustMitchie
Posts: 15
Joined: Saturday 17 March 2018 23:21
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Lua 3D array

Post by JustMitchie »

jvdz wrote: Thursday 22 March 2018 22:15 What about?:

Code: Select all

commandArray['UpdateDevice']=otherdevices_idx['gas']..'|0|'..jsonData.result[1].CounterToday
I think these devices need to SValue.
What I don't understand is why you call it gas while it is a kWh value for electricity. The other thing to consider is that you probably only want the value and not the kWh fed into the device.

Jos
I want to split the values, and i dont want to have to look at the usage of the previous homeowner ;)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest