Radiation monitor value from json to Domoticz, how to? Topic is solved

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.

Moderators: leecollings, remb0

Post Reply
costo
Posts: 71
Joined: Tuesday 11 August 2015 17:35
Target OS: Linux
Domoticz version: 2025.1
Location: Netherlands
Contact:

Radiation monitor value from json to Domoticz, how to?

Post 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+
User avatar
Westcott
Posts: 423
Joined: Tuesday 09 December 2014 17:04
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: UK - Glos
Contact:

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

Post 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
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
costo
Posts: 71
Joined: Tuesday 11 August 2015 17:35
Target OS: Linux
Domoticz version: 2025.1
Location: Netherlands
Contact:

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

Post 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 :?
User avatar
Westcott
Posts: 423
Joined: Tuesday 09 December 2014 17:04
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: UK - Glos
Contact:

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

Post 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.
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
costo
Posts: 71
Joined: Tuesday 11 August 2015 17:35
Target OS: Linux
Domoticz version: 2025.1
Location: Netherlands
Contact:

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

Post 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
costo
Posts: 71
Joined: Tuesday 11 August 2015 17:35
Target OS: Linux
Domoticz version: 2025.1
Location: Netherlands
Contact:

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

Post 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.
User avatar
Westcott
Posts: 423
Joined: Tuesday 09 December 2014 17:04
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: UK - Glos
Contact:

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

Post by Westcott »

Have you installed JSON.lua yet?
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
costo
Posts: 71
Joined: Tuesday 11 August 2015 17:35
Target OS: Linux
Domoticz version: 2025.1
Location: Netherlands
Contact:

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

Post 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
User avatar
Westcott
Posts: 423
Joined: Tuesday 09 December 2014 17:04
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: UK - Glos
Contact:

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

Post 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
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
costo
Posts: 71
Joined: Tuesday 11 August 2015 17:35
Target OS: Linux
Domoticz version: 2025.1
Location: Netherlands
Contact:

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

Post 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.
User avatar
Westcott
Posts: 423
Joined: Tuesday 09 December 2014 17:04
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: UK - Glos
Contact:

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

Post by Westcott »

Try -
print(jsondevices)
local jsoncpm = json:decode(jsondevices)
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
costo
Posts: 71
Joined: Tuesday 11 August 2015 17:35
Target OS: Linux
Domoticz version: 2025.1
Location: Netherlands
Contact:

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

Post 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)
User avatar
Westcott
Posts: 423
Joined: Tuesday 09 December 2014 17:04
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: UK - Glos
Contact:

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

Post by Westcott »

Did 'print(jsondevices)' print anything?
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
costo
Posts: 71
Joined: Tuesday 11 August 2015 17:35
Target OS: Linux
Domoticz version: 2025.1
Location: Netherlands
Contact:

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

Post 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}}
costo
Posts: 71
Joined: Tuesday 11 August 2015 17:35
Target OS: Linux
Domoticz version: 2025.1
Location: Netherlands
Contact:

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

Post 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.
User avatar
robpow
Posts: 38
Joined: Friday 19 July 2013 19:28
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta :)
Location: .se
Contact:

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

Post 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
costo
Posts: 71
Joined: Tuesday 11 August 2015 17:35
Target OS: Linux
Domoticz version: 2025.1
Location: Netherlands
Contact:

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

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest