Sending numbers as payload type in actions enhancement
-
I'm controlling a HVAC device through the MQTTController. While setting up the entity config in reactor.yaml, I started with this on the action block:
set_setpoint: topic: "ezlo_mqtt/set/item/%topic%/set/Tsetpoint" payload: expr: "parameters.setpoint" type: raw
However, as the setpoint is a number in the UI, things were not fully parseable as a number and nothing was outputted to mqtt.
So I've added this code to MQTTController.js at line 1449 to add the payload type 'number':
} else if ( 'number' === act.payload.type ) { payload = String( payload ); } else if (''raw' !== ( act.payload.type || 'raw' ) ) {
After this change, I was able to set the payload type to number:
set_setpoint: topic: "ezlo_mqtt/set/item/%topic%/set/Tsetpoint" payload: expr: "parameters.setpoint" type: number
And things worked as intended, being able to send numbers in the actions bit of a rule set.
-
You can just send it raw, which has the same effect as your (unnecessary) code addition. In fact, you can just send it JSON, too, since a JSON-ified number is just the number as a string, same-same. Alternately, you can also use:
payload: expr: "str(parameters.setpoint)"
-
I was expecting raw to work, however using raw throws an error in my reactor.log file:
[stable-23078]2023-05-16T11:27:22.250Z <MQTTController:WARN> expression has run, result 20 [stable-23078]2023-05-16T11:27:22.250Z <MQTTController:INFO> MQTTController#mqtt perform hvac_cooling_unit.set_setpoint on MHI_AC_1: publishing ezlo_mqtt/set/item/MHI-AC-Woonkamer/set/Tsetpoint 20 [stable-23078]2023-05-16T11:27:22.254Z <wsapi:CRIT> TypeError: The "string" argument must be of type string or an instance of Buffer or ArrayBuffer. Received type number (20) [-] TypeError [ERR_INVALID_ARG_TYPE]: The "string" argument must be of type string or an instance of Buffer or ArrayBuffer. Received type number (20) at new NodeError (node:internal/errors:372:5) at Function.byteLength (node:buffer:735:11) at publish (/var/reactor/ext/MQTTController/node_modules/mqtt-packet/writeToStream.js:334:51) at Object.generate [as writeToStream] (/var/reactor/ext/MQTTController/node_modules/mqtt-packet/writeToStream.js:34:14) at sendPacket (/var/reactor/ext/MQTTController/node_modules/mqtt/lib/client.js:176:29) at MqttClient._sendPacket (/var/reactor/ext/MQTTController/node_modules/mqtt/lib/client.js:1250:7) at publishProc (/var/reactor/ext/MQTTController/node_modules/mqtt/lib/client.js:659:14) at MqttClient.publish (/var/reactor/ext/MQTTController/node_modules/mqtt/lib/client.js:665:74) at /var/reactor/ext/MQTTController/MQTTController.js:616:25 at new Promise (<anonymous>)
As you can see, I try to publish the raw value 20 to topic ezlo/.../Tsetpoint which fails as the number is not a string (so far for untyped behaviour of js..)
Both your alternatives, using json or raw with the str() expression work flawlessly. I'll remove my unneeded changes in the code!
-
OK. I can address the error message. Best of both worlds.