Configurable json payload attribute
hobbyquaker opened this issue · comments
until now - if json parsing isn't disabled - homekit2mqtt tries to use the attribute val
(following https://github.com/mqtt-smarthome/mqtt-smarthome/blob/master/Architecture.md). This should be configurable to allow non-mqtt-smarthome json payloads.
as suggested by @matsekberg json-path could be used. Or to keep it simple (json-path seems a bit too much for just selecting a specific property) smth like https://github.com/sindresorhus/dot-prop could be sufficient?
I would love JSONPath, at the moment I manually have to seperate it into more than one topic which is a bit of a pain
Do you really need json path or would it be sufficient to just select properties/subproperties via dot-notation? E.g. lamp.brightness
to select 100 from {"lamp":{"brightness":100, ...}, ...}
?
I think json-path with all its powerful possibilities to query more than one property, recursive queries, select array members and so on is oversized, I would prefer a simple solution here.
I think dot notation is fine.
I generally do it like this in MQTT {type}/{room}/{action}
So the JSON for an Airconditioner could be as simple as this.
I just want to grab one of the values
{
"mode": 1,
"temp": 22,
"set_temp": 20
}
So I could just do .mode
to grab the mode? I think any further than that is overkill for the moment.
Not sure if I should add this here, but I've got another example where the current JSON implementation isn't working.
I have a Sonoff TH16 which has a Temperature and Humidity sensor. It has Tasmota installed on it, and the temperature and Humidity are sent like this over MQTT:
tele/th1/SENSOR = {"Time":"2017-02-08T14:19:04", "DHT":{"Temperature":"21.4", "Humidity":"42.3"}}
Now I have configured a Temperature sensor in homekit2mqtt, but I cannot extract the "Temperature" value. And for the Humidty sensor I've configured I cannot extract the "Humidity" value.
Yes, that's exactly what a configurable json property would be for. Until it's implemented you can only work around that by e.g. a Node-RED flow that subscribes to tele/th1/SENSOR
, extracts the wanted properties and republish them as plain values on distinct topics, e.g. tele/th1/SENSOR/Temperature
and tele/th1/SENSOR/Humidity
.
Example js code for a Node-RED function node (untested):
const data = JSON.parse(msg.payload);
Object.keys(data).forEach(key => {
node.send({
topic: msg.topic + '/' + key,
payload: String(data[key]);
});
});
implemented in v1.1.0
Access nested properties via dot notation, e.g. {"lamp1":{"bri":100}}
via lamp1.bri
.
Hi, i've installed homekit2mqtt 1.1.2 and i try to comunicate with my son off sensor.
json = tele/sonoff/SENSOR = {"Time":"2018-09-03T16:59:03", "DHT11":{"Temperature":"26.0", "Humidity":"50.0"}}
How to parse only temperature value?
I read that Access nested properties was implemented on version 1.1.0 but i can't parse the value.
Thanx in advance
In your case I think you can use the JSON Property in the websever editor just under statusTemperature for the specific accessory and put DHT11.Temperature
Hi,
I tried like described to parse a value from a nested Json on the topic "tele/sonoff_th1/SENSOR".
JSON I get is:
{"Time":"2018-11-18T21:42:16","SI7021":{"Temperature":1.9,"Humidity":85.4},"TempUnit":"C"}
In the webinterface I have set "Status Temperature" to tele/sonoff_th1/SENSOR, and "JSON Property" to SI7021.Temperature
I can´t get the value to show up in Homekit
Can you help to point out what´s wrong here?
p.s.: homekit2mqtt version is 1.1.2
Best,
Christian
To use 'JSON Property' in TemperatureSensor and HumiditySensor, you have to modify it as follows.
service file
../node_modules/homekit2mqtt/services.json
...
...
"HumiditySensor": {
"topic": [
{"name": "statusHumidity "}, <-FIX statusHumidity ->statusCurrentRelativeHumidity
{"name": "statusLowBattery", "optional": true},
{"name": "statusTampered", "optional": true},
{"name": "statusActive", "optional": true},
{"name": "statusFault", "optional": true}
],
...
...
"TemperatureSensor": {
"topic": [
{"name": "statusTemperature "}, <- FIX statusTemperature->statusCurrentTemperature
{"name": "statusLowBattery", "optional": true},
{"name": "statusTampered", "optional": true},
{"name": "statusActive", "optional": true},
{"name": "statusFault", "optional": true}
],
...
...
sensor payload
topic = office/aw1485a/status
msg.payload = {"battery":96,"temperature":25.8,"humidity":41}
mapfile
node_modules/homekit2mqtt/example-homekit2mqtt.json
{
"사무실온도계": {
"name": "사무실",
"manufacturer": "iHWANGYU",
"model": "aw1485a",
"services": [
{
"name": "온도",
"service": "TemperatureSensor",
"topic": {
"statusTemperature": "office/aw1485a/status", # FIX statusTemperature -> statusCurrentTemperature
"statusLowBattery": "",
"statusTampered": "",
"statusActive": "",
"statusFault": ""
},
"json": {
"statusCurrentTemperature": "temperature",
"statusLowBattery": "",
"statusTampered": "",
"statusActive": "",
"statusFault": ""
},
"payload": {
"fahrenheit": false,
"invertLowBattery": false,
"invertTampered": false,
"invertActive": false,
"invertFault": false
},
"config": {},
"props": {
"CurrentTemperature": {
"minValue": 0,
"maxValue": 35
}
}
},
{
"name": "습도",
"service": "HumiditySensor",
"topic": {
"statusHumidity": "office/aw1485a/status", # FIX statusHumidity -> statusCurrentRelativeHumidity
"statusLowBattery": "",
"statusTampered": "",
"statusActive": "",
"statusFault": ""
},
"json": {
"statusCurrentRelativeHumidity": "humidity",
"statusLowBattery": "",
"statusTampered": "",
"statusActive": "",
"statusFault": ""
},
"payload": {
"invertLowBattery": false,
"invertTampered": false,
"invertActive": false,
"invertFault": false
},
"config": {},
"props": {}
}
],
"id": "사무실온도계",
"payload": {},
"config": {},
"category": 1
}
}