1technophile / OpenMQTTGateway

MQTT gateway for ESP8266 or ESP32 with bidirectional 433mhz/315mhz/868mhz, Infrared communications, BLE, Bluetooth, beacons detection, mi flora, mi jia, LYWSD02, LYWSD03MMC, Mi Scale, TPMS, BBQ thermometer compatibility & LoRa.
https://docs.openmqttgateway.com
GNU General Public License v3.0
3.6k stars 793 forks source link

Lack of valueAsASubject in ZgatewayPilight messages #408

Closed wired-dev closed 4 years ago

wired-dev commented 5 years ago

I resolved the issue for myself by adding ` RFPiLightdata.set("message", (char *)message.c_str());

ifdef valueAsASubject

    StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer2;
    JsonObject& RFPiLightdatain=jsonBuffer2.parseObject((char *)message.c_str());
    RFPiLightdata.set("value", RFPiLightdatain["id"]);
  #endif

` to ZgatewayPilight; this also made it easier to read the Digoo-DG-R8S device.

wired-dev commented 5 years ago

in ZgatewayPilight.ino roughly line 60 in the void pilightCallback(const String &protocol, const String &message, int status, size_t repeats, const String &deviceID) function replace void pilightCallback(const String &protocol, const String &message, int status, size_t repeats, const String &deviceID) { if (status == VALID) { trc(F("Creating RF PiLight buffer")); StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer; JsonObject& RFPiLightdata = jsonBuffer.createObject(); trc(F("Rcv. Pilight")); RFPiLightdata.set("message", (char *)message.c_str()); RFPiLightdata.set("protocol",(char *)protocol.c_str()); with

void pilightCallback(const String &protocol, const String &message, int status, size_t repeats, const String &deviceID) { if (status == VALID) { trc(F("Creating RF PiLight buffer")); StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer; JsonObject& RFPiLightdata = jsonBuffer.createObject(); StaticJsonBuffer<JSON_MSG_BUFFER> jsonBuffer2; JsonObject& msg=jsonBuffer2.parseObject(message); trc(F("Rcv. Pilight")); RFPiLightdata.set("message", (char *)message.c_str()); if(isDigit(msg["id"])){ RFPiLightdata.set("value", msg["id"]); } else { RFPiLightdata.set("value", msg["id"].as<String>()); } RFPiLightdata.set("protocol",(char *)protocol.c_str());

wired-dev commented 5 years ago

then in the OpenMQTTGateway.ino file I changed it to do strings too. around line 1020 in the void pub(char * topicori, JsonObject& data) function

`void pub(char * topicori, JsonObject& data){

digitalWrite(led_receive, HIGH);

String topic = topicori;
#ifdef valueAsASubject
  unsigned long value = data["value"];
  if (value != 0){
    topic = topic + "/"+ String(value);
  }
#endif`

replace with

`void pub(char * topicori, JsonObject& data){

digitalWrite(led_receive, HIGH);

String topic = topicori;
#ifdef valueAsASubject
  if(isDigit(data["value"])){
    unsigned long value = data["value"];

// if (value != 0){ topic = topic + "/"+ String(value); } else if(data["value"].as()!= 0) { topic = topic + "/"+ data["value"].as(); }

endif`

this makes it work for more devices on pilight.

akasma74 commented 5 years ago

Where did you add it and how does it help? I own some DG-R8S sensors and use them with HA - could you share your relevant HA config? I tried to use valueAsASubject, but didn't find it useful and currently use demultiplexers.

p.s maybe it's more reasonable to create a PR? ;)

wired-dev commented 5 years ago

not sure how to do a PR; but yeah just starting with git went from home/OMG_PiLight/PilighttoMQTT/message {"id":42,"temperature":23.60,"humidity":43.00,"battery":1,"channel":1} home/OMG_PiLight/PilighttoMQTT/message {"id":"E3","unit":37,"state":"on"} to home/OMG_PiLight/PilighttoMQTT/42/message {"id":42,"temperature":23.60,"humidity":43.00,"battery":1,"channel":1} home/OMG_PiLight/PilighttoMQTT/E3/message {"id":"E3","unit":37,"state":"on"} allowing me to parse my switches and ha config sensor:

switch:

I have two omg units one with pilight one without due to some of my devices not working with pilight. also I've found the pilight unit to be flooding the 433mhz band making it hard to work with anything; the normal rf dosn't seem to do this.

I've not tried demultiplexers though. want to post the config on that?

1technophile commented 5 years ago

@wired-dev if you need help for the PR don't hesitate to ask questions, in some simple steps: