homebridge-mqttthing icon indicating copy to clipboard operation
homebridge-mqttthing copied to clipboard

return false from topic apply function?

Open rbswift opened this issue 4 years ago • 4 comments

Is there any way to return false from a javascript apply function and have the accessory ignore the message?

Something like this?

            "type": "statelessProgrammableSwitch",
            "topics": {
                "getSwitch": [
                    {
                        "topic": "zigbee2mqtt/ikeaswitch1",
                        "apply": "return (JSON.parse(message).action=='on' ? '1' : JSON.parse(message).action=='brightness_move_up' ? 'L' : false);"
                    },
                    {
                        "topic": "zigbee2mqtt/ikeaswitch1",
                        "apply": "return (JSON.parse(message).action=='off' ? '1' : JSON.parse(message).action=='brightness_move_down' ? 'L' : false);"
                    }
                ],

without logging errors? Cheers

rbswift avatar Jul 13 '21 00:07 rbswift

I think returning null or undefined might work.

arachnetech avatar Jul 13 '21 06:07 arachnetech

Thank you @arachnetech. null crashed homebridge but undefined seems to work well. Cheers

Jul 13 16:34:06 alarm homebridge[2439]: [7/13/2021, 4:34:06 PM] TypeError: Cannot read property 'toString' of null
Jul 13 16:34:06 alarm homebridge[2439]:     at /usr/lib/node_modules/homebridge-mqttthing/index.js:1282:44
Jul 13 16:34:06 alarm homebridge[2439]:     at Array.handler (/usr/lib/node_modules/homebridge-mqttthing/libs/mqttlib.js:237:32)
Jul 13 16:34:06 alarm homebridge[2439]:     at MqttClient.<anonymous> (/usr/lib/node_modules/homebridge-mqttthing/libs/mqttlib.js:116:34)
Jul 13 16:34:06 alarm homebridge[2439]:     at MqttClient.emit (events.js:314:20)
Jul 13 16:34:06 alarm homebridge[2439]:     at MqttClient._handlePublish (/usr/lib/node_modules/homebridge-mqttthing/node_modules/mqtt/lib/client.js:1277:12)
Jul 13 16:34:06 alarm homebridge[2439]:     at MqttClient._handlePacket (/usr/lib/node_modules/homebridge-mqttthing/node_modules/mqtt/lib/client.js:410:12)
Jul 13 16:34:06 alarm homebridge[2439]:     at work (/usr/lib/node_modules/homebridge-mqttthing/node_modules/mqtt/lib/client.js:321:12)
Jul 13 16:34:06 alarm homebridge[2439]:     at Writable.writable._write (/usr/lib/node_modules/homebridge-mqttthing/node_modules/mqtt/lib/client.js:335:5)
Jul 13 16:34:06 alarm homebridge[2439]:     at doWrite (/usr/lib/node_modules/homebridge-mqttthing/node_modules/readable-stream/lib/_stream_writable.js:409:139)
Jul 13 16:34:06 alarm homebridge[2439]:     at writeOrBuffer (/usr/lib/node_modules/homebridge-mqttthing/node_modules/readable-stream/lib/_stream_writable.js:398:5)
Jul 13 16:34:06 alarm homebridge[2439]: [7/13/2021, 4:34:06 PM] Got SIGTERM, shutting down Homebridge...

rbswift avatar Jul 13 '21 06:07 rbswift

Thank you for this. Can confirm returning undefined didn't throw any err or warn in log

shahrilamrias avatar Nov 14 '21 03:11 shahrilamrias

Thanks. I should fix the crash on null.

arachnetech avatar Nov 14 '21 13:11 arachnetech