arachnetech / homebridge-mqttthing

A plugin for Homebridge allowing the integration of many different accessory types using MQTT.
Apache License 2.0
462 stars 104 forks source link

using wildcard in json.parse(message) #516

Open gandalfk7 opened 2 years ago

gandalfk7 commented 2 years ago

Hi!

This is a request for help on the configuration, I don't think it's a bug, I am not very keen on Json..

I am very happily using homebridge with many devices over MQTT and wanted to refine my config.

I have some Zigbee devices, mainly thermometers, seen via MQTT, like:

        {
            "accessory": "mqttthing",
            "type": "humiditySensor",
            "name": "Zigbee_Sonoff_Temp02 - HUM",
            "url": "mqtt://192.168.10.10:1883",
            "username": "user",
            "password": "password",
            "topics": {
                "getCurrentRelativeHumidity": {
                    "topic": "tele/tasmota_ABCDEF/ZB_Sonoff_Temp02/SENSOR",
                    "apply": "return JSON.parse(message).ZbReceived['0x4EF9'].Humidity;"
                }
            }
        },

Since my topic already defines the device, I wanted to tune the json parsing to avoid specifing the device ID (this is beacause I have friendly names on Tasmota ZBBridge and sometimes the IDs change when reassociating a device).

So I wanted ot use a wildcard, from this: "apply": "return JSON.parse(message).ZbReceived['0x4EF9'].Humidity;" to this: "apply": "return JSON.parse(message).ZbReceived[].Humidity;"

but it does not work.

I've already read the json path syntax and tried with an online parser which confirms that ZbReceived[].Humidity should work (I am not very keen on json).

Unfortunately when using [] I receive an error:

[12/25/2021, 2:37:39 AM] [ZB - ZB_Sonoff_Temp02 - HUM] Initializing mqttthing accessory...
[12/25/2021, 2:37:39 AM] [ZB - ZB_Sonoff_Temp02 - HUM] Exception while creating services: SyntaxError: Unexpected token ']'
[12/25/2021, 2:37:39 AM] [ZB - ZB_Sonoff_Temp02 - HUM] SyntaxError: Unexpected token ']'
    at Function (<anonymous>)
    at Object.subscribe (/usr/lib/node_modules/homebridge-mqttthing/libs/mqttlib.js:249:31)
    at mqttSubscribe (/usr/lib/node_modules/homebridge-mqttthing/index.js:50:17)
    at floatCharacteristic (/usr/lib/node_modules/homebridge-mqttthing/index.js:1195:21)
    at characteristic_CurrentRelativeHumidity (/usr/lib/node_modules/homebridge-mqttthing/index.js:1634:17)
    at configToServices (/usr/lib/node_modules/homebridge-mqttthing/index.js:2830:17)
    at createServices (/usr/lib/node_modules/homebridge-mqttthing/index.js:3397:24)
    at new makeThing (/usr/lib/node_modules/homebridge-mqttthing/index.js:3429:23)
    at /usr/lib/node_modules/homebridge/src/server.ts:350:50
    at Array.forEach (<anonymous>)
[12/25/2021, 2:37:39 AM] [ZB - ZB_Sonoff_Temp02 - HUM] Accessory mqttthing returned empty set of services; not adding it to the bridge.

same when I try to use [*]:

[12/25/2021, 2:39:31 AM] [ZB - ZB_Sonoff_Temp02 - HUM] Initializing mqttthing accessory...
[12/25/2021, 2:39:31 AM] [ZB - ZB_Sonoff_Temp02 - HUM] Exception while creating services: SyntaxError: Unexpected token '*'
[12/25/2021, 2:39:31 AM] [ZB - ZB_Sonoff_Temp02 - HUM] SyntaxError: Unexpected token '*'
    at Function (<anonymous>)
    at Object.subscribe (/usr/lib/node_modules/homebridge-mqttthing/libs/mqttlib.js:249:31)
    at mqttSubscribe (/usr/lib/node_modules/homebridge-mqttthing/index.js:50:17)
    at floatCharacteristic (/usr/lib/node_modules/homebridge-mqttthing/index.js:1195:21)
    at characteristic_CurrentRelativeHumidity (/usr/lib/node_modules/homebridge-mqttthing/index.js:1634:17)
    at configToServices (/usr/lib/node_modules/homebridge-mqttthing/index.js:2830:17)
    at createServices (/usr/lib/node_modules/homebridge-mqttthing/index.js:3397:24)
    at new makeThing (/usr/lib/node_modules/homebridge-mqttthing/index.js:3429:23)
    at /usr/lib/node_modules/homebridge/src/server.ts:350:50
    at Array.forEach (<anonymous>)
[12/25/2021, 2:39:31 AM] [ZB - ZB_Sonoff_Temp02 - HUM] Accessory mqttthing returned empty set of services; not adding it to the bridge.

I am a little lost, could you help me please?

Thank you very much and happy holidays!!

ceraz68 commented 2 years ago

Hard to help without seeing the incoming JSON string. In my setup I have something similar:

"apply": "return JSON.parse(message)[\"DS18B20-3\"].Temperature;"

I'm not JSON savvy and find the syntax very hit or miss, but try:

"apply": "return JSON.parse(message)['0x4EF9'].Humidity;" or "apply": "return JSON.parse(message)[\"0x4EF9\"].Humidity;"

gandalfk7 commented 1 year ago

I've noticed that I solved my issue but never updated this thread, without the "apply" it works well, like:

        {
            "accessory": "mqttthing",
            "type": "humiditySensor",
            "name": "Zigbee_Sonoff_Temp02 - HUM",
            "url": "mqtt://192.168.10.10:1883",
            "username": "user",
            "password": "password",
            "topics": {
                "getCurrentTemperature": "tele/tasmota_ABCDEF/ZB_Sonoff_Temp02/SENSOR$.ZbReceived[*].Temperature"
            }
            }
        },

right now I am trying to make it work in the "apply" block but searching for the problem on google led me to this issue, so I don't keep my hopes low :D