Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable json payload attribute #67

Closed
hobbyquaker opened this issue Feb 9, 2018 · 11 comments
Closed

Configurable json payload attribute #67

hobbyquaker opened this issue Feb 9, 2018 · 11 comments
Milestone

Comments

@hobbyquaker
Copy link
Owner

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.

@hobbyquaker hobbyquaker added this to the v1.0 milestone Feb 9, 2018
@hobbyquaker
Copy link
Owner Author

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?

@NovaGL
Copy link

NovaGL commented Feb 16, 2018

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

@hobbyquaker
Copy link
Owner Author

hobbyquaker commented Feb 19, 2018

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.

@NovaGL
Copy link

NovaGL commented Feb 20, 2018

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.

@PorschefanRoel
Copy link

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.

@hobbyquaker
Copy link
Owner Author

hobbyquaker commented Jul 16, 2018

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]);
  });
});

@hobbyquaker
Copy link
Owner Author

hobbyquaker commented Aug 25, 2018

implemented in v1.1.0
Access nested properties via dot notation, e.g. {"lamp1":{"bri":100}} via lamp1.bri.

@drhill78
Copy link

drhill78 commented Sep 3, 2018

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

@stoinov
Copy link

stoinov commented Oct 21, 2018

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

@leramleram
Copy link

leramleram commented Nov 19, 2018

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

@fwangg
Copy link

fwangg commented Jan 4, 2019

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
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants