Too stupid to use switch node and/or function with Aqara magic cube T1 Pro :(

Hi!

I hate to admit it, but I'm too stupid for the "switch-node". :frowning:
I receive a msg.payload from my new "Aqara magic cube T1 Pro" that contains a msg.payload object with

payload.side : int (1...6)
payload.action : string
payload.operation_mode : string (scene, action)
[...]

I would like to switch depending on the "side" value. So I added a switch node with "msg.payload.side" as proptery. Then I added six outputs with number cases "== 1" till "== 6". But regardless of which value "side" has, the first output (side == 1) is always triggered.

I thought that I used the "switch-node" a wrong way. So I created a function node with a switch/case for "msg.payload.side" and six outputs. But again, the case of msg.payload.side seems to be always "1". The debug output of msg.payload.side clearly says otherwise. :frowning:

This is an example of the msg.payload, sent by the cube:

msg.payload =
{
	"action": "1_min_inactivity",
	"battery": 100,
	"current": 0,
	"device": {
		[...]
	},
	"last_seen": "2023-12-28T21:17:47+01:00",
	"linkquality": 134,
	"operation_mode": "scene_mode",
	"power": 1,
	"power_outage_count": 6,
	"side": 3,
	"voltage": 3177
}

Any ideas? :wink:

Are you sure your using a number (int) ? and not a string

care to share your flow so we can import it :slight_smile:

Yes, I am using "number". I would upload the flow, but as a new user I am not allowed to. :frowning:
Here the inline version:


[{"id":"bf510de9c34b4fc2","type":"tab","label":"T1 tests","disabled":false,"info":"","env":[]},{"id":"16725a2ced4bf3d4","type":"debug","z":"bf510de9c34b4fc2","name":"Output Cube","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":950,"y":40,"wires":[]},{"id":"d5a17cf54d1a5eb3","type":"mqtt in","z":"bf510de9c34b4fc2","name":"","topic":"zigbee2mqtt/CubeT1","qos":"2","datatype":"json","broker":"c9d3faa6.3fe16","nl":false,"rap":true,"rh":"1","inputs":0,"x":180,"y":60,"wires":[["16725a2ced4bf3d4","6888240a1a195272"]]},{"id":"421b575a6df35bf8","type":"debug","z":"bf510de9c34b4fc2","name":"Seite 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.side","targetType":"msg","statusVal":"","statusType":"auto","x":940,"y":80,"wires":[]},{"id":"6888240a1a195272","type":"switch","z":"bf510de9c34b4fc2","name":"Seite 1-6","property":"payload.side","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"eq","v":"3","vt":"num"},{"t":"eq","v":"4","vt":"num"},{"t":"eq","v":"5","vt":"num"},{"t":"eq","v":"6","vt":"num"},{"t":"else"}],"checkall":"false","repair":false,"outputs":7,"x":580,"y":120,"wires":[["421b575a6df35bf8"],["4158c6d1a3366f6f"],[],[],[],["18ba52d91de89334"],[]]},{"id":"4158c6d1a3366f6f","type":"debug","z":"bf510de9c34b4fc2","name":"Seite 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.side","targetType":"msg","statusVal":"","statusType":"auto","x":940,"y":120,"wires":[]},{"id":"801a4591ca0efcea","type":"inject","z":"bf510de9c34b4fc2","name":"","props":[{"p":"payload.side","v":"6","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":230,"y":120,"wires":[["6888240a1a195272"]]},{"id":"18ba52d91de89334","type":"debug","z":"bf510de9c34b4fc2","name":"Seite 6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.side","targetType":"msg","statusVal":"","statusType":"auto","x":940,"y":160,"wires":[]},{"id":"c9d3faa6.3fe16","type":"mqtt-broker","name":"MQTT Server","broker":"10.10.0.42","port":"1883","clientid":"","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"5","keepalive":"30","cleansession":true,"autoUnsubscribe":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""}]

O.O

grafik

This is not a valid JSON, can we see a expanded debug of the payload.
Scratch that i see you have posted one.

Your flow seems to work fine with the payload added in the inject node.

That is the recommended way to post flows here.

1 Like

Your flow works fine for me too, once I put a debug node on the msg.payload.side == 3 output

Isn't it msg.payload.object.side?

There is no object named object in that sample payload.

To avoid typos and simplify things, the op should use the tools provided:

Canned text

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

The debug output is possibly a bit confusing there. Arguably it might be better if it said payload there.

1 Like

Interesting. Output 1 is always triggered for me, even if msg.payload.side==3.

grafik

It is working ok for me

[{"id":"421b575a6df35bf8","type":"debug","z":"bf510de9c34b4fc2","name":"Seite 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.side","targetType":"msg","statusVal":"","statusType":"auto","x":940,"y":120,"wires":[]},{"id":"6888240a1a195272","type":"switch","z":"bf510de9c34b4fc2","name":"Seite 1-6","property":"payload.side","propertyType":"msg","rules":[{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"eq","v":"3","vt":"num"},{"t":"eq","v":"4","vt":"num"},{"t":"eq","v":"5","vt":"num"},{"t":"eq","v":"6","vt":"num"},{"t":"else"}],"checkall":"false","repair":false,"outputs":7,"x":580,"y":160,"wires":[["421b575a6df35bf8"],["4158c6d1a3366f6f"],["72ad560e4064d430"],[],[],["18ba52d91de89334"],[]]},{"id":"4158c6d1a3366f6f","type":"debug","z":"bf510de9c34b4fc2","name":"Seite 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.side","targetType":"msg","statusVal":"","statusType":"auto","x":940,"y":160,"wires":[]},{"id":"801a4591ca0efcea","type":"inject","z":"bf510de9c34b4fc2","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"action\":\"1_min_inactivity\",\"battery\":100,\"current\":0,\"last_seen\":\"2023-12-28T21:17:47+01:00\",\"linkquality\":134,\"operation_mode\":\"scene_mode\",\"power\":1,\"power_outage_count\":6,\"side\":3,\"voltage\":3177}","payloadType":"json","x":230,"y":160,"wires":[["6888240a1a195272"]]},{"id":"18ba52d91de89334","type":"debug","z":"bf510de9c34b4fc2","name":"Seite 6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.side","targetType":"msg","statusVal":"","statusType":"auto","x":940,"y":240,"wires":[]},{"id":"72ad560e4064d430","type":"debug","z":"bf510de9c34b4fc2","name":"Seite 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.side","targetType":"msg","statusVal":"","statusType":"auto","x":940,"y":200,"wires":[]}]

It works fpr me, too if I inject the captured payload.
But every time the device sends its own message output 1 and output 3 is triggered.
Output 1 seems to be triggered by "msg.payload.side == 3" and output 3 is triggered by "zigbee2mqtt/Cube1: msg.payload.side == 3".

it doesn't matter whether side is 1,2,3,4,5 or 6. Output 1 reacts every time. :frowning:

Can you use a debug node to capture and share a message that doesn't work?

And expand the device object please, at least to the next level

This is a compltete captured message from the device itself:

{
	"topic": "zigbee2mqtt/CubeT1",
	"payload": {
		"action": "1_min_inactivity",
		"battery": 100,
		"current": 0,
		"device": {
			"applicationVersion": 26,
			"dateCode": "20230303",
			"friendlyName": "CubeT1",
			"hardwareVersion": 1,
			"ieeeAddr": "0x54ef44100093e119",
			"manufacturerID": 4447,
			"manufacturerName": "LUMI",
			"model": "CTP-R01",
			"networkAddress": 35391,
			"powerSource": "Battery",
			"softwareBuildID": "0.0.0_0026",
			"stackVersion": 2,
			"type": "EndDevice",
			"zclVersion": 3
		},
		"last_seen": "2023-12-29T14:56:14+01:00",
		"linkquality": 87,
		"operation_mode": "scene_mode",
		"power": 1,
		"power_outage_count": 6,
		"side": 4,
		"voltage": 3141
	},
	"qos": 0,
	"retain": false,
	"_msgid": "cb2f205a85615b5f"
}

That works for me if I paste it into an Inject mode set to {}.
Can you stop node-red and start it again in a command window and paste the full output here please.

Also could you change the debug node Seite 1 to output complete message and see what it shows.

Your image shows the output to debug node named Side 3

Not Side 1

Looks like the switch node is functioning correctly. Unless output 1 is connected to debug Side 3?