Page 1 of 1

Radiation monitor value from json to Domoticz, how to?

Posted: Sunday 24 April 2016 13:26
by costo
Hi, I have a radiation-monitor that shows the local radiation in a browser, on 192.168.178.20 showing this:
uRADMonitor 12000059
type:1 hw:108 sw:111 SI29BGradiation:13CPM
average:9.29CPM
temperature:29.44C

voltage:377V (39%)
uptime:150350s
wdt:49s

ip:192.168.178.20
server:23.239.13.18
http:200
and also a json string on 192.168.178.20/j like this:

{"data":{ "id":"12000059","type":"1","detector":"SI29BG","cpm":9,"temperature":28.44,"uptime": 151444}}

I'd like to import at least the cpm value in Domoticz but I cannot figure out how to parse the json and/or import the data into Domoticz. I'm more a hardware guy , not a software guy.
I hope someone can help me a bit in showing how to import the measurements into Domoticz.

b.t.w. I'm running Domoticz on a RPi 2B+

Re: Radiation monitor value from json to Domoticz, how to?

Posted: Sunday 24 April 2016 18:52
by Westcott
Hi Costo,
Have a look at this topic, it shows how to use a Lua script to read, decode, and upload JSON data.
http://www.domoticz.com/forum/viewtopic.php?t=11450

Re: Radiation monitor value from json to Domoticz, how to?

Posted: Thursday 05 May 2016 18:28
by costo
Westcott wrote:Hi Costo,
Have a look at this topic, it shows how to use a Lua script to read, decode, and upload JSON data.
http://www.domoticz.com/forum/viewtopic.php?t=11450
Thank you for the response Westcott. It took some time before I jumped into the topic but it is so much more complicated than what I want that it does not make any sense to me.

I come up to this script:

Code: Select all

commandArray={}

json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()  -- For Linux
-- json = (loadfile "D:\\Domoticz\\scripts\\lua\\json.lua")()  -- For Windows

local cpm = " "
local uptime = " "

local jsondata    = assert(io.popen('curl http://192.168.178.20/j')
local jsondevices = jsondata:read('*all')
jsondata:close()

print(cpm)
print(uptime)

return commandArray
I named it "cpm_data.lua" and copied it in the lua script folder but now it makes even less sense to me. I made a virtual sensor of the type 'custom sensor' that got idx 425. I do not see the connection between the script and the device.
I just get a headache trying to understand this :?

Re: Radiation monitor value from json to Domoticz, how to?

Posted: Thursday 05 May 2016 18:53
by Westcott
Yes, it's not easy, is it!

First, if you run '192.168.178.20/j' in a browser, do you get data?

Second, do you have the loadfile JSON.lua in the folder?

Third, there's a certain naming convention for Lua scripts to be run by Domoticz from the lua folder.
Yours should be called 'script_time_cpm_data.lua' to run every minute.

Fourth, add a 'print("cpm_data") - or whatever text you fancy - somewhere in your script to show that it actually executing.

Then we can go on to decoding the JSON data, and updating devices.

Re: Radiation monitor value from json to Domoticz, how to?

Posted: Thursday 05 May 2016 19:43
by costo
O.K. I try to do the 4 steps:

A: yes I get this data when I run 'http://192.168.178.20/j' in a browser, the data looks like:
{"data":{ "id":"12000059","type":"1","detector":"SI29BG","cpm":6,"temperature":26.50,"uptime": 1124204}}

B: No I do not see JSON.lua in the JSON.lua in the ... home/pi/domoticz/scripts/lua folder on the RPi.

C: I renamed the script to \script_time_cpm_data.lua

D: I get this error message every minute:
Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_cpm_data.lua: /home/pi/domoticz/scripts/lua/script_time_cpm_data.lua:3: attempt to call a nil value


edit: after searching I found at: https://www.domoticz.com/wiki/Lua_-_json.lua the file JSON.lua.
I copied it to the script/lua folder and now the error_messages are gone.
In the log file I see now the printed sentence : LUA: cpm_data every minute.

I guess I make some progress

Re: Radiation monitor value from json to Domoticz, how to?

Posted: Friday 06 May 2016 19:34
by costo
Westcott wrote: Then we can go on to decoding the JSON data, and updating devices.
Hi Westcott,

Java script like language is very abstract that's why I cannot figure out how to decode the value.
The code I have prints the lines : <LUA: cpm> and the just <LUA:> without a value

Code: Select all

commandArray={}
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()  -- For Linux
local cpm = " "
local uptime = " "
local jsondata    = assert(io.popen('curl http://192.168.178.20/j'))
local jsondevices = jsondata:read('*all')
jsondata:close()

local jsoncpm = json:decode(cpm)

print('cpm')
print (cpm)
return commandArray
Some help would be very much appreciated.

Re: Radiation monitor value from json to Domoticz, how to?

Posted: Friday 06 May 2016 20:13
by Westcott
Have you installed JSON.lua yet?

Re: Radiation monitor value from json to Domoticz, how to?

Posted: Friday 06 May 2016 21:18
by costo
Westcott wrote:Have you installed JSON.lua yet?
I copied the file JSON.lua into the scripts/lua folder. That seems to work because the the line print('cpm') prints LUA: cpm into the log.

JSON.lua is a large file , 35kb including all the comments, I got it from here: https://www.domoticz.com/wiki/Lua_-_json.lua

Re: Radiation monitor value from json to Domoticz, how to?

Posted: Friday 06 May 2016 21:29
by Westcott
I think we are getting there.
In your script, after the line -
local jsoncpm = json:decode(cpm)

insert these 2 lines -
cpm=jsoncpm.cpm
uptime=jsoncpm.uptime

Re: Radiation monitor value from json to Domoticz, how to?

Posted: Friday 06 May 2016 21:49
by costo
Westcott wrote:I think we are getting there.
In your script, after the line -
local jsoncpm = json:decode(cpm)

insert these 2 lines -
cpm=jsoncpm.cpm
uptime=jsoncpm.uptime
I inserted the 2 lines, it does not make any difference in what is printed into the logfile.

print('cpm') prints LUA: cpm
print (cpm) prints LUA:

my feeling say that cpm should hava become the value cpm from the string that was decoded

edit: Stupid me !! I copied the script into the wrong folder
experimenting now to see what happens.

Re: Radiation monitor value from json to Domoticz, how to?

Posted: Friday 06 May 2016 22:09
by Westcott
Try -
print(jsondevices)
local jsoncpm = json:decode(jsondevices)

Re: Radiation monitor value from json to Domoticz, how to?

Posted: Friday 06 May 2016 22:13
by costo
I get errormessage in this line: cpm=jsoncpm.cpm


2016-05-06 22:14:00.487 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_cpm_data.lua: /home/pi/domoticz/scripts/lua/script_time_cpm_data.lua:14: attempt to index local 'jsoncpm' (a nil value)

Re: Radiation monitor value from json to Domoticz, how to?

Posted: Friday 06 May 2016 23:56
by Westcott
Did 'print(jsondevices)' print anything?

Re: Radiation monitor value from json to Domoticz, how to?

Posted: Saturday 07 May 2016 0:31
by costo
Yes < print(jsondevices) > did print the line that I used to see in the browser.

The line looks like:
2016-05-06 23:13:00.486 LUA: {"data":{ "id":"12000059","type":"1","detector":"SI29BG","cpm":11,"temperature":27.00,"uptime": 1224308}}

Re: Radiation monitor value from json to Domoticz, how to?

Posted: Saturday 07 May 2016 1:14
by costo
This is driving me nuts.
I have

Code: Select all

commandArray={}

json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()

local cpm = " "
local uptime = " "

local jsondata    = assert(io.popen('curl http://192.168.178.20/j'))
local jsondevices = jsondata:read('*all')
jsondata:close()
 
local jsonCPM = json:decode(jsondevices)

cpm = jsonCPM.cpm
uptime = jsonCPM.uptime

print(jsondevices) -- ok
print (jsonCPM)
print (cpm)
print (uptime)

return commandArray
4 print commands , only the line <print(jsondevices)> prints the json undecoded line.
The decoding is not taking place.
I have been trying many changes to see if it can print a decoded value but I get either no output or get a error message.

Re: Radiation monitor value from json to Domoticz, how to?

Posted: Saturday 07 May 2016 7:46
by robpow
I'm not home to test it right now myself but shouldn't the variables be read like this:

cpm = jsonCPM.data.cpm
Etc...

'Cpm' being under 'data'?

Matt

Re: Radiation monitor value from json to Domoticz, how to?

Posted: Saturday 07 May 2016 20:23
by costo
Thank you Matt.
I am very happy :D

cpm = jsonCPM.data.cpm
uptime = jsonCPM.data.uptime

that is the solution for decoding the json string.

Next step is need to do is couple the data to the device IDX in Domoticz, so I can see my data on the dashboard.
I hope someone can help me with that.