Httpmod - Nested JSON Extraktion

Begonnen von Tsturm, 07 Oktober 2018, 13:02:42

Vorheriges Thema - Nächstes Thema

Tsturm

Hallo Zusammen,

ich habe eine verschachtelte JSON-Liste, die ich in Readings parsen will. Extract allJSON funktioniert, einen definierten Wert - hier "sec-seit_reset" auch. Aber wie bekomme ich die Werte in dem Array heraus, die ich nur indirekt benennen kann (zum Beispiel "wiffi_vorlauftemp")? Ein Zugriff über den definierten Wert geht "vars_04_value" - das zeigt auf den 4. Wert im Array, der der Vorlauftemperatur entspricht - aber das hängt dann voo der Reihenfolge im JSON-Telegramm ab. Wenn sich die Reihenfolge ändert, kommt Mist raus...

Any ideas?
VG timmo

Hier die JSON-Liste:

   "modultyp":"wiffi-pump",
   "vars":[ 
      { 
         "name":"0",
         "homematic_name":"wiffi_pump_ip",
         "desc":"ip des wiffi-pump",
         "type":"string",
         "unit":"",
         "value":"192.168.178.65"
      },
      { 
         "name":"4",
         "homematic_name":"wiffi_ntc",
         "desc":"Temperatur_NTC",
         "type":"number",
         "unit":"%",
         "value":34.83
      },
      { 
         "name":"2",
         "homematic_name":"wiffi_ww_temp",
         "desc":"Temperatur_A",
         "type":"number",
         "unit":"gradC",
         "value":125.0
      },
      { 
         "name":"3",
         "homematic_name":"wiffi_vorlauftemp",
         "desc":"Temperatur_B",
         "type":"number",
         "unit":"gradC",
         "value":125.0
      },
      { 
         "name":"1",
         "homematic_name":"wiffi_pump",
         "desc":"Zirkulationspumpe",
         "type":"boolean",
         "unit":"",
         "value":false
      }
   ],
   "Systeminfo":{ 
      "MAC-Adresse":"39:5e:d9:7f:cf:5c",
      "Homematic_CCU_ip":"my_ccuip",
      "WLAN_ssid":"sturmnetz",
      "WLAN_Signal_dBm":-44,
      "sec_seit_reset":15307,
      "zeitpunkt":" 1 12:47",
      "firmware":"wiffi_pump2_89"
   }
}


Hier die Definition:
defmod wiffiJ HTTPMOD http://192.168.178.65/?json 35
attr wiffiJ userattr get01JSON get01Name getHeader1 getHeader2 getURL reading02JSON
attr wiffiJ extractAllJSON 1
attr wiffiJ getHeader1 Content-Type: application/json
attr wiffiJ getHeader2 Accept: */*
attr wiffiJ getURL http://192.168.178.65/?json
attr wiffiJ reading02JSON Systeminfo_sec_seit_reset
attr wiffiJ room 6.1_Rain,9.6_Umwelt
attr wiffiJ timeout 20
attr wiffiJ verbose 5

setstate wiffiJ 2018-10-07 13:00:39 Systeminfo_Homematic_CCU_ip my_ccuip
setstate wiffiJ 2018-10-07 13:00:39 Systeminfo_MAC-Adresse 39:5e:d9:7f:cf:5c
setstate wiffiJ 2018-10-07 13:00:39 Systeminfo_WLAN_Signal_dBm -43
setstate wiffiJ 2018-10-07 13:00:39 Systeminfo_WLAN_ssid sturmnetz
setstate wiffiJ 2018-10-07 13:00:39 Systeminfo_firmware wiffi_pump2_89
setstate wiffiJ 2018-10-07 13:00:39 Systeminfo_sec_seit_reset 16082
setstate wiffiJ 2018-10-07 13:00:39 Systeminfo_zeitpunkt  1 13:00
setstate wiffiJ 2018-10-07 13:00:39 modultyp wiffi-pump
setstate wiffiJ 2018-10-07 13:00:39 vars_01_desc ip des wiffi-pump
setstate wiffiJ 2018-10-07 13:00:39 vars_01_homematic_name wiffi_pump_ip
setstate wiffiJ 2018-10-07 13:00:39 vars_01_name 0
setstate wiffiJ 2018-10-07 13:00:39 vars_01_type string
setstate wiffiJ 2018-10-07 13:00:39 vars_01_unit
setstate wiffiJ 2018-10-07 13:00:39 vars_01_value 192.168.178.65
setstate wiffiJ 2018-10-07 13:00:39 vars_02_desc Temperatur_NTC
setstate wiffiJ 2018-10-07 13:00:39 vars_02_homematic_name wiffi_ntc
setstate wiffiJ 2018-10-07 13:00:39 vars_02_name 4
setstate wiffiJ 2018-10-07 13:00:39 vars_02_type number
setstate wiffiJ 2018-10-07 13:00:39 vars_02_unit %
setstate wiffiJ 2018-10-07 13:00:39 vars_02_value 34.43
setstate wiffiJ 2018-10-07 13:00:39 vars_03_desc Temperatur_A
setstate wiffiJ 2018-10-07 13:00:39 vars_03_homematic_name wiffi_ww_temp
setstate wiffiJ 2018-10-07 13:00:39 vars_03_name 2
setstate wiffiJ 2018-10-07 13:00:39 vars_03_type number
setstate wiffiJ 2018-10-07 13:00:39 vars_03_unit gradC
setstate wiffiJ 2018-10-07 13:00:39 vars_03_value 125
setstate wiffiJ 2018-10-07 13:00:39 vars_04_desc Temperatur_B
setstate wiffiJ 2018-10-07 13:00:39 vars_04_homematic_name wiffi_vorlauftemp
setstate wiffiJ 2018-10-07 13:00:39 vars_04_name 3
setstate wiffiJ 2018-10-07 13:00:39 vars_04_type number
setstate wiffiJ 2018-10-07 13:00:39 vars_04_unit gradC
setstate wiffiJ 2018-10-07 13:00:39 vars_04_value 125
setstate wiffiJ 2018-10-07 13:00:39 vars_05_desc Zirkulationspumpe
setstate wiffiJ 2018-10-07 13:00:39 vars_05_homematic_name wiffi_pump
setstate wiffiJ 2018-10-07 13:00:39 vars_05_name 1
setstate wiffiJ 2018-10-07 13:00:39 vars_05_type boolean
setstate wiffiJ 2018-10-07 13:00:39 vars_05_unit
setstate wiffiJ 2018-10-07 13:00:39 vars_05_value 0



Hier das log:
2018.10.07 12:57:07 4: wiffiJ: GetUpdate called (update)
2018.10.07 12:57:07 4: wiffiJ: update timer modified: will call GetUpdate in 35.0 seconds at 2018-10-07 12:57:42
2018.10.07 12:57:07 4: wiffiJ: AddToQueue adds update, initial queue len: 0
2018.10.07 12:57:07 5: wiffiJ: AddToQueue adds type update to URL http://192.168.178.65/?json, no data, no headers, retry 0
2018.10.07 12:57:07 5: wiffiJ: HandleSendQueue called, qlen = 1
2018.10.07 12:57:07 4: wiffiJ: HandleSendQueue sends request type update to URL http://192.168.178.65/?json, No Data, No Header
timeout 20
2018.10.07 12:57:07 5: HttpUtils url=http://192.168.178.65/?json
2018.10.07 12:57:07 5: HttpUtils request header:
GET /?json HTTP/1.0
Host: 192.168.178.65
User-Agent: fhem
Accept-Encoding: gzip,deflate
Content-Length: 0
Content-Type: application/x-www-form-urlencoded

2018.10.07 12:57:08 4: http://192.168.178.65/?json: HTTP response code 200
2018.10.07 12:57:08 5: HttpUtils http://192.168.178.65/?json: Got data, length: 805
2018.10.07 12:57:08 5: HttpUtils response header:
HTTP/1.1 200 OK
Content-Type: application/json;charset=utf-8
server: wiffi
Connection: close
2018.10.07 12:57:08 4: wiffiJ: Read callback: request type was update retry 0,
Body: {"modultyp":"wiffi-pump","vars":[{"name":"0","homematic_name":"wiffi_pump_ip","desc":"ip des wiffi-pump","type":"string","unit":"","value":"192.168.178.65"},{"name":"4","homematic_name":"wiffi_ntc","desc":"Temperatur_NTC","type":"number","unit":"%","value":34.54},{"name":"2","homematic_name":"wiffi_ww_temp","desc":"Temperatur_A","type":"number","unit":"gradC","value":125.0},{"name":"3","homematic_name":"wiffi_vorlauftemp","desc":"Temperatur_B","type":"number","unit":"gradC","value":125.0},{"name":"1","homematic_name":"wiffi_pump","desc":"Zirkulationspumpe","type":"boolean","unit":"","value":false}],"Systeminfo":{"MAC-Adresse":"39:5e:d9:7f:cf:5c","Homematic_CCU_ip":"my_ccuip","WLAN_ssid":"sturmnetz","WLAN_Signal_dBm":-43,"sec_seit_reset":15871,"zeitpunkt":" 1 12:57","firmware":"wiffi_pump2_89"}}
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter called : prefix , ref is HASH(0x616c5e0)
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter doing recursion because value is a ARRAY
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter called : prefix vars_, ref is ARRAY(0x6471140)
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter doing recursion because value is a HASH
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter called : prefix vars_01_, ref is HASH(0x5dbcdf0)
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_01_desc to ip des wiffi-pump
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_01_homematic_name to wiffi_pump_ip
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_01_value to 192.168.178.65
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_01_unit to
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_01_type to string
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_01_name to 0
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter doing recursion because value is a HASH
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter called : prefix vars_02_, ref is HASH(0x629a6e8)
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_02_desc to Temperatur_NTC
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_02_homematic_name to wiffi_ntc
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_02_unit to %
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_02_value to 34.54
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_02_type to number
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_02_name to 4
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter doing recursion because value is a HASH
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter called : prefix vars_03_, ref is HASH(0x62296b0)
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_03_homematic_name to wiffi_ww_temp
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_03_desc to Temperatur_A
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_03_value to 125
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_03_unit to gradC
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_03_name to 2
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_03_type to number
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter doing recursion because value is a HASH
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter called : prefix vars_04_, ref is HASH(0x64f3cb8)
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_04_unit to gradC
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_04_value to 125
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_04_type to number
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_04_name to 3
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_04_desc to Temperatur_B
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_04_homematic_name to wiffi_vorlauftemp
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter doing recursion because value is a HASH
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter called : prefix vars_05_, ref is HASH(0x5cb7c38)
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_05_homematic_name to wiffi_pump
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_05_desc to Zirkulationspumpe
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_05_type to boolean
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_05_value to 0
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_05_unit to
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets vars_05_name to 1
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets modultyp to wiffi-pump
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter doing recursion because value is a HASH
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter called : prefix Systeminfo_, ref is HASH(0x646f230)
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets Systeminfo_WLAN_Signal_dBm to -43
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets Systeminfo_MAC-Adresse to 39:5e:d9:7f:cf:5c
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets Systeminfo_zeitpunkt to  1 12:57
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets Systeminfo_sec_seit_reset to 15871
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets Systeminfo_WLAN_ssid to sturmnetz
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets Systeminfo_Homematic_CCU_ip to my_ccuip
2018.10.07 12:57:08 5: wiffiJ: JSON Flatter sets Systeminfo_firmware to wiffi_pump2_89
2018.10.07 12:57:08 4: wiffiJ: extracted JSON values to internal
2018.10.07 12:57:08 5: wiffiJ: ExtractSid called, context reading, num
2018.10.07 12:57:08 4: wiffiJ: CheckAuth decided no authentication required
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_04_value as reading vars_04_value to value 125
2018.10.07 12:57:08 5: wiffiJ: Read set JSON modultyp as reading modultyp to value wiffi-pump
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_03_type as reading vars_03_type to value number
2018.10.07 12:57:08 5: wiffiJ: Read set JSON Systeminfo_firmware as reading Systeminfo_firmware to value wiffi_pump2_89
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_01_value as reading vars_01_value to value 192.168.178.65
2018.10.07 12:57:08 5: wiffiJ: Read set JSON Systeminfo_WLAN_ssid as reading Systeminfo_WLAN_ssid to value sturmnetz
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_04_unit as reading vars_04_unit to value gradC
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_04_homematic_name as reading vars_04_homematic_name to value wiffi_vorlauftemp
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_04_name as reading vars_04_name to value 3
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_02_homematic_name as reading vars_02_homematic_name to value wiffi_ntc
2018.10.07 12:57:08 5: wiffiJ: Read set JSON Systeminfo_sec_seit_reset as reading Systeminfo_sec_seit_reset to value 15871
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_01_desc as reading vars_01_desc to value ip des wiffi-pump
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_02_value as reading vars_02_value to value 34.54
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_02_desc as reading vars_02_desc to value Temperatur_NTC
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_05_name as reading vars_05_name to value 1
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_02_name as reading vars_02_name to value 4
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_05_type as reading vars_05_type to value boolean
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_01_homematic_name as reading vars_01_homematic_name to value wiffi_pump_ip
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_03_homematic_name as reading vars_03_homematic_name to value wiffi_ww_temp
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_01_type as reading vars_01_type to value string
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_05_desc as reading vars_05_desc to value Zirkulationspumpe
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_02_unit as reading vars_02_unit to value %
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_03_unit as reading vars_03_unit to value gradC
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_04_type as reading vars_04_type to value number
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_05_value as reading vars_05_value to value 0
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_03_desc as reading vars_03_desc to value Temperatur_A
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_03_name as reading vars_03_name to value 2
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_05_homematic_name as reading vars_05_homematic_name to value wiffi_pump
2018.10.07 12:57:08 5: wiffiJ: Read set JSON Systeminfo_WLAN_Signal_dBm as reading Systeminfo_WLAN_Signal_dBm to value -43
2018.10.07 12:57:08 5: wiffiJ: Read set JSON Systeminfo_zeitpunkt as reading Systeminfo_zeitpunkt to value  1 12:57
2018.10.07 12:57:08 5: wiffiJ: Read set JSON Systeminfo_MAC-Adresse as reading Systeminfo_MAC-Adresse to value 39:5e:d9:7f:cf:5c
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_03_value as reading vars_03_value to value 125
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_02_type as reading vars_02_type to value number
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_05_unit as reading vars_05_unit to value
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_01_unit as reading vars_01_unit to value
2018.10.07 12:57:08 5: wiffiJ: Read set JSON Systeminfo_Homematic_CCU_ip as reading Systeminfo_Homematic_CCU_ip to value my_ccuip
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_01_name as reading vars_01_name to value 0
2018.10.07 12:57:08 5: wiffiJ: Read set JSON vars_04_desc as reading vars_04_desc to value Temperatur_B
2018.10.07 12:57:08 5: wiffiJ: Read starts parsing response to update with defined readings: 02
2018.10.07 12:57:08 5: wiffiJ: ExtractReading Systeminfo_sec_seit_reset with json Systeminfo_sec_seit_reset ...
2018.10.07 12:57:08 4: wiffiJ: ExtractReading for reading02-1 sets Systeminfo_sec_seit_reset to 15871
2018.10.07 12:57:08 4: wiffiJ: Read response to update matched Reading(s) vars_04_value modultyp vars_03_type Systeminfo_firmware vars_01_value Systeminfo_WLAN_ssid vars_04_unit vars_04_homematic_name vars_04_name vars_02_homematic_name Systeminfo_sec_seit_reset vars_01_desc vars_02_value vars_02_desc vars_05_name vars_02_name vars_05_type vars_01_homematic_name vars_03_homematic_name vars_01_type vars_05_desc vars_02_unit vars_03_unit vars_04_type vars_05_value vars_03_desc vars_03_name vars_05_homematic_name Systeminfo_WLAN_Signal_dBm Systeminfo_zeitpunkt Systeminfo_MAC-Adresse vars_03_value vars_02_type vars_05_unit vars_01_unit Systeminfo_Homematic_CCU_ip vars_01_name vars_04_desc Systeminfo_sec_seit_reset
2018.10.07 12:57:08 5: wiffiJ: HandleSendQueue called, qlen = 0

amenomade

In dem Fall wird es wahrscheinlich einfacher mit Regex.
Pi 3B, Alexa, CUL868+Selbstbau 1/2λ-Dipol-Antenne, USB Optolink / Vitotronic, Debmatic und HM / HmIP Komponenten, Rademacher Duofern Jalousien, Fritz!Dect Thermostaten, Proteus

Tsturm

Schade... gerade da hatte ich die Hoffung, dass mich JSON vor Formatänderungen und deren Auswirkungen bewahren würde ..;-(

Dennoch danke
Timmo