Decode JSON from pilight

Moderator: leecollings

Post Reply
Schween
Posts: 2
Joined: Sunday 06 December 2020 16:12
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Decode JSON from pilight

Post by Schween »

Hi,

i want decode this json-file from pilight with domoticz_applyJsonPath:

Code: Select all

[
   {
    "type": 3,
    "devices": [
      "Aussen"
    ],
    "values": {
      "timestamp": 1607253494,
      "battery": 1,
      "humidity": 91,
      "temperature": 5.77
    }
  },
  {
    "type": 3,
    "devices": [
      "Schlafzimmer"
    ],
    "values": {
      "timestamp": 0,
      "battery": 1,
      "humidity": 55,
      "temperature": 18.61
    }
  },
  {
    "type": 3,
    "devices": [
      "Badezimmer"
    ],
    "values": {
      "timestamp": 0,
      "battery": 1,
      "humidity": 102,
      "temperature": -55
    }
  },
  {
    "type": 3,
    "devices": [
      "Hannes"
    ],
    "values": {
      "timestamp": 0,
      "battery": 1,
      "humidity": 37,
      "temperature": -64.16
    }
  },
  {
    "type": 3,
    "devices": [
      "Mika"
    ],
    "values": {
      "timestamp": 1607255498,
      "battery": 1,
      "humidity": 54,
      "temperature": 18.22
    }
  },
  {
    "type": 3,
    "devices": [
      "Wohnzimmer"
    ],
    "values": {
      "timestamp": 1607255401,
      "battery": 1,
      "humidity": 45,
      "temperature": 21.94
    }
  }
]
i need help for the right syntax to get e.g. the humidity from Sensor Type 3 "Aussen" and send it to the virtual Sensor IDX 11 :

Code: Select all

s = request['content'];
local hum = domoticz_applyJsonPath(s,'.3.Aussen.values.humidity')
domoticz_updateDevice(11,hum, '')
this JSONPath ist wrong....i found not the right path :cry:

please help me..... :)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Decode JSON from pilight

Post by waaren »

Schween wrote: Sunday 06 December 2020 16:36 this JSONPath ist wrong....i found not the right path :cry:
See below for a basic approach to use the default JSON decoder for classic Lua and dzVents.
Json is decoded and loaded into a Lua table. Function tprint dumps the table to domoticz log.

Code: Select all

commandArray = {}

local  function tprint (t, indent, done)
  local done = done or {}
  indent = indent or 0
  for key, value in pairs (t) do
    pre = (string.rep (" ", indent)) -- indent it
    if type (value) == "table" and not done[value] then
      done [value] = true
      print (pre .. tostring (key) .. ":");
      tprint (value, indent + 2, done)
    elseif type(value) == 'function' and not done[value] then
      print( pre .. (tostring (key) .. "()"))
    else
      pre = pre .. (tostring (key) .. ": ")
      print (pre .. tostring(value))
    end
  end
end
 
package.path = 'scripts/lua/?.lua;' .. package.path -- prepend to package path to allow use of mudules in scripts/lua

jsonRaw = '[{"type": 3,"devices": ["Aussen"],"values": {"timestamp": 1607253494,"battery": 1,"humidity": 91,"temperature": 5.77}},{"type": 3,"devices": ["Schlafzimmer"],"values": {"timestamp": 0,"battery": 1,"humidity": 55,"temperature": 18.61}},{"type": 3,"devices": ["Badezimmer"],"values": {"timestamp": 0,"battery": 1,"humidity": 102,"temperature": -55}},{"type": 3,"devices": ["Hannes"],"values": {"timestamp": 0,"battery": 1,"humidity": 37,"temperature": -64.16}},{"type": 3,"devices": ["Mika"],"values": {"timestamp": 1607255498,"battery": 1,"humidity": 54,"temperature": 18.22}},{"type": 3,"devices": ["Wohnzimmer"],"values": {"timestamp": 1607255401,"battery": 1,"humidity": 45,"temperature": 21.94}}]'

 -- Using existing JSON decoder
 local JSON = require "JSON"     -- use generic JSON.lua
 local jsonParse = JSON:decode( jsonRaw )
 
 tprint (jsonParse)
 
 return commandArra
result:
Spoiler: show

Code: Select all

 
 2020-12-06 23:32:39.588  Status: LUA: 1:
2020-12-06 23:32:39.588  Status: LUA:   type: 3
2020-12-06 23:32:39.588  Status: LUA:   values:
2020-12-06 23:32:39.588  Status: LUA:     humidity: 91
2020-12-06 23:32:39.588  Status: LUA:     temperature: 5.77
2020-12-06 23:32:39.588  Status: LUA:     timestamp: 1607253494
2020-12-06 23:32:39.588  Status: LUA:     battery: 1
2020-12-06 23:32:39.588  Status: LUA:   devices:
2020-12-06 23:32:39.588  Status: LUA:     1: Aussen
2020-12-06 23:32:39.588  Status: LUA: 2:
2020-12-06 23:32:39.588  Status: LUA:   type: 3
2020-12-06 23:32:39.588  Status: LUA:   values:
2020-12-06 23:32:39.588  Status: LUA:     humidity: 55
2020-12-06 23:32:39.588  Status: LUA:     temperature: 18.61
2020-12-06 23:32:39.588  Status: LUA:     timestamp: 0
2020-12-06 23:32:39.588  Status: LUA:     battery: 1
2020-12-06 23:32:39.588  Status: LUA:   devices:
2020-12-06 23:32:39.588  Status: LUA:     1: Schlafzimmer
2020-12-06 23:32:39.588  Status: LUA: 3:
2020-12-06 23:32:39.588  Status: LUA:   type: 3
2020-12-06 23:32:39.588  Status: LUA:   values:
2020-12-06 23:32:39.589  Status: LUA:     humidity: 102
2020-12-06 23:32:39.589  Status: LUA:     temperature: -55
2020-12-06 23:32:39.589  Status: LUA:     timestamp: 0
2020-12-06 23:32:39.589  Status: LUA:     battery: 1
2020-12-06 23:32:39.589  Status: LUA:   devices:
2020-12-06 23:32:39.589  Status: LUA:     1: Badezimmer
2020-12-06 23:32:39.589  Status: LUA: 4:
2020-12-06 23:32:39.589  Status: LUA:   type: 3
2020-12-06 23:32:39.589  Status: LUA:   values:
2020-12-06 23:32:39.589  Status: LUA:     humidity: 37
2020-12-06 23:32:39.589  Status: LUA:     temperature: -64.16
2020-12-06 23:32:39.589  Status: LUA:     timestamp: 0
2020-12-06 23:32:39.589  Status: LUA:     battery: 1
2020-12-06 23:32:39.589  Status: LUA:   devices:
2020-12-06 23:32:39.589  Status: LUA:     1: Hannes
2020-12-06 23:32:39.589  Status: LUA: 5:
2020-12-06 23:32:39.589  Status: LUA:   type: 3
2020-12-06 23:32:39.589  Status: LUA:   values:
2020-12-06 23:32:39.589  Status: LUA:     humidity: 54
2020-12-06 23:32:39.589  Status: LUA:     temperature: 18.22
2020-12-06 23:32:39.589  Status: LUA:     timestamp: 1607255498
2020-12-06 23:32:39.589  Status: LUA:     battery: 1
2020-12-06 23:32:39.589  Status: LUA:   devices:
2020-12-06 23:32:39.589  Status: LUA:     1: Mika
2020-12-06 23:32:39.589  Status: LUA: 6:
2020-12-06 23:32:39.589  Status: LUA:   type: 3
2020-12-06 23:32:39.589  Status: LUA:   values:
2020-12-06 23:32:39.589  Status: LUA:     humidity: 45
2020-12-06 23:32:39.589  Status: LUA:     temperature: 21.94
2020-12-06 23:32:39.589  Status: LUA:     timestamp: 1607255401
2020-12-06 23:32:39.589  Status: LUA:     battery: 1
2020-12-06 23:32:39.589  Status: LUA:   devices:
2020-12-06 23:32:39.589  Status: LUA:     1: Wohnzimmer

 
 
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Schween
Posts: 2
Joined: Sunday 06 December 2020 16:12
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Contact:

Re: Decode JSON from pilight

Post by Schween »

that's it! :D

Code: Select all

 hum = (jsonParse[14]['values']['humidity'])
 temp = (jsonParse[14]['values']['temperature'])
 temphum = temp..';'..hum..';0'
 
 domoticz_updateDevice(10, 0, temphum)
thanks for your script. now my cheap temperature/humidity 433Mhz-Sensors works via pilight with domoticz!

thank you!!!!

Code: Select all

commandArray = {}

local  function tprint (t, indent, done)
  local done = done or {}
  indent = indent or 0
  for key, value in pairs (t) do
    pre = (string.rep (" ", indent)) -- indent it
    if type (value) == "table" and not done[value] then
      done [value] = true
      print (pre .. tostring (key) .. ":");
      tprint (value, indent + 2, done)
    elseif type(value) == 'function' and not done[value] then
      print( pre .. (tostring (key) .. "()"))
    else
      pre = pre .. (tostring (key) .. ": ")
      print (pre .. tostring(value))
    end
  end
end
 
package.path = 'scripts/lua/?.lua;' .. package.path -- prepend to package path to allow use of mudules in scripts/lua

jsonRaw = request['content'];

 -- Using existing JSON decoder
 local JSON = require "JSON"     -- use generic JSON.lua
 local jsonParse = JSON:decode( jsonRaw )
 
 -- tprint (jsonParse)
 
 hum = (jsonParse[14]['values']['humidity'])
 temp = (jsonParse[14]['values']['temperature'])
 temphum = temp..';'..hum..';0'
 
 domoticz_updateDevice(10, 0, temphum)
 
 hum = (jsonParse[13]['values']['humidity'])
 temp = (jsonParse[13]['values']['temperature'])
 temphum = temp..';'..hum..';0'
 
 domoticz_updateDevice(11, 0, temphum)
 
 hum = (jsonParse[12]['values']['humidity'])
 temp = (jsonParse[12]['values']['temperature'])
 temphum = temp..';'..hum..';0'
 
 domoticz_updateDevice(12, 0, temphum)
 
 hum = (jsonParse[11]['values']['humidity'])
 temp = (jsonParse[11]['values']['temperature'])
 temphum = temp..';'..hum..';0'
 
 domoticz_updateDevice(13, 0, temphum)
 
 hum = (jsonParse[10]['values']['humidity'])
 temp = (jsonParse[10]['values']['temperature'])
 temphum = temp..';'..hum..';0'
 
 domoticz_updateDevice(14, 0, temphum)
 
 hum = (jsonParse[9]['values']['humidity'])
 temp = (jsonParse[9]['values']['temperature'])
 temphum = temp..';'..hum..';0'
 
 domoticz_updateDevice(15, 0, temphum)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest