akiifall / homebridge-mqtt-watervalve

ISC License
0 stars 0 forks source link

MQTT topicCommand not sending to MQTT Broker #1

Open beanonymous opened 2 years ago

beanonymous commented 2 years ago

After several attempts, I am unable to see any commands from this plugin that sends back to the MQTT broker.

The subscriber topicStatus works fine.

Any help would be appreciated.

akiifall commented 2 years ago

I tested again and works good.

this is my config.

    {
        "accessory": "WaterValue",
        "name": "Waterpump",
        "manufacturer": "ELEC Co.",
        "model": "PH-125A",
        "serialNumber": "2021.11",
        "deviceType": "1",
        "mqttUrl": "mqtt://localhost",
        "mqttUser": "mqtt",
        "mqttPass": "mqttpass",
        "topicStatus": "home/pump/status",
        "topicCommand": "home/pump/command",
        "onCommand": "{\"Command\":\"setOn\"}",
        "offCommand": "{\"Command\": \"setOff\"}",
        "onValue": "ON",
        "offValue": "OFF"
    }

subscribe topicCommand, then on or off this homekit switch and you can see onCommand or offCommand.

{ "DevicsStatus":"ON" } (or OFF)

this is a Status message format. publish to topicStatus this message, you can change on or off status of this accessory.

beanonymous commented 2 years ago

I tried your settings and no success.

Here are my settings:

{
    "accessory": "WaterValue",
    "name": "Valve 1",
    "manufacturer": "manufacturer",
    "model": "model",
    "serialNumber": "serialNumber",
    "deviceType": "1",
    "mqttUrl": "192.168.0.1:1883",
    "mqttUser": "mqtt",
    "mqttPass": "mqttpass",
    "topicStatus": "zero/sprinkler/v1",
    "topicCommand": "zero/sprinkler/cmd/v1",
    "onCommand": "{\"Command\":\"on\"}",
    "offCommand": "{\"Command\":\"off\"}",
    "onValue": "ON",
    "offValue": "OFF"
}

I should note that my MQTT broker is not on the same device host.

Could that be why I can listen to the broker fine but sending out commands to external mqtt broker is not working?

akiifall commented 2 years ago

I tried to your config like this

    {
        "accessory": "WaterValue",
        "name": "Valve 1",
        "manufacturer": "manufacturer",
        "model": "model",
        "serialNumber": "serialNumber",
        "deviceType": "1",
        "mqttUrl": "mqtt://192.168.0.50:1883",
        "mqttUser": "mqtt",
        "mqttPass": "elechomemqtt",
        "topicStatus": "zero/sprinkler/v1",
        "topicCommand": "zero/sprinkler/cmd/v1",
        "onCommand": "{\"Command\":\"on\"}",
        "offCommand": "{\"Command\":\"off\"}",
        "onValue": "ON",
        "offValue": "OFF"
    }

test deivce ip : 192.168.0.35 mqtt server : 192.168.0.50

it works fine too. I edit your mqttUrl "192.168.0.1:1883" to "mqtt://192.168....." "mqtt://" means "use mqtt protocol". edit mqttUrl and try again! :)

beanonymous commented 2 years ago

Thanks for the quick reply.

The reason i removed the mqtt:// at the start was because my homebridge service crashs if I add the prefex at the start of the URL.

I get the following error before the HB service crashs.

[3/23/2022, 10:25:44 PM] SyntaxError: Unexpected token O in JSON at position 0
    at JSON.parse (<anonymous>)
    at MqttClient.<anonymous> (/usr/local/lib/node_modules/homebridge-mqtt-watervalve/src/accessory.ts:141:29)
    at MqttClient.emit (events.js:314:20)
    at MqttClient._handlePublish (/usr/local/lib/node_modules/homebridge-mqtt-watervalve/node_modules/mqtt/lib/client.js:1547:12)
    at MqttClient._handlePacket (/usr/local/lib/node_modules/homebridge-mqtt-watervalve/node_modules/mqtt/lib/client.js:535:12)
    at work (/usr/local/lib/node_modules/homebridge-mqtt-watervalve/node_modules/mqtt/lib/client.js:438:12)
    at Writable.writable._write (/usr/local/lib/node_modules/homebridge-mqtt-watervalve/node_modules/mqtt/lib/client.js:452:5)
    at doWrite (/usr/local/lib/node_modules/homebridge-mqtt-watervalve/node_modules/readable-stream/lib/_stream_writable.js:409:139)
    at writeOrBuffer (/usr/local/lib/node_modules/homebridge-mqtt-watervalve/node_modules/readable-stream/lib/_stream_writable.js:398:5)
    at Writable.write (/usr/local/lib/node_modules/homebridge-mqtt-watervalve/node_modules/readable-stream/lib/_stream_writable.js:307:11)

Not sure what I am missing here but I suppose if I fix this and use the URL with the mqtt:// prefex at the start my send command issue will be resolved.

Any help on this one would be appreciated.

akiifall commented 2 years ago

what is your HB version? I used HB v 1.4.0 on Linux and MacOS.

this error means when message in topicStatus is not a JSON format.

topicStatus JSON format is like this { "DeviceStatus":"ON" } (or OFF).

just add "mqtt://" at mqttUrl and try again.