Checking for a key with a space inside it?

Hi everyone,

I'm getting an object with a key that got a space in its name and I'm trying to figure out how to create a switch rule for that. Here you can see an example output what I mean:

image

And this is what I'm trying to achieve:

I wouldn't ask if this will work, tryed it also with ["filament_switch_sensor Sensor"] but still not working.

So anyone knows how to solve this?

I've only dealt with this using function nodes I'm afraid. There are several ways to check whether a key exists using JavaScript.

// This is the SAFEST way
if ( Object.prototype.hasOwnProperty.call(OBJECT, 'key') ) {
    // Key exists so do something
}

The space should not be an issue , what property is the switch node set to?

has key and string as the others (see my screenshot in the original post)

that was my thought as well to get it done with a function node... but my hope is still to get this solved with the default switch node :slight_smile:

mhhh tryed it but this solution seems not to work:

var OBJECT = msg.payload;

// This is the SAFEST way
if ( Object.prototype.hasOwnProperty.call(OBJECT, 'filament_switch_sensor Sensor') ) {
    return msg;
}

That's not what i'm after. What is the main property the switch is set to, the one under name.
Also, can we get a complete debug output, of the input to the switch,.

[edit , is the switch also set to check all options or find the first?]

ah it is on msg.payload :slight_smile: sry for that
and checking ALL rules

Looking at your image, the key is in msg.results.status, but can not tell as the image hides alot, a complete debug output would help.

image

Does it find fan? if it does can I have a debug I can copy and paste so i can do some testing.

yep all others work, just that one with the space not.

{"jsonrpc":"2.0","result":{"status":{"heater_bed":{"temperature":59.70476091770062,"target":60},"filament_switch_sensor Sensor":{"filament_detected":true},"gcode":{},"fan":{"speed":1,"rpm":null},"display_status":{"progress":0.3},"print_stats":{"print_duration":9321.11139888801,"total_duration":9559.235056339006,"filename":"Trunk_Tall_0.16mm_PLA__7h39m.gcode","state":"printing"},"toolhead":{"homed_axes":"xyz","estimated_print_time":61695.956782333335,"print_time":61698.806354975015,"extruder":"extruder"},"extruder":{"target":210,"temperature":209.99919772313908}},"eventtime":96164.416848014},"id":"3041544688"}

oh and thank you for spending time helping me out :slight_smile:

That's odd - I've just tested locally and it's working fine for me:

Here's a simple inject->switch->debug that uses has key on filament_switch_sensor Sensor and the message passes through.

[{"id":"fd2dd455.7d85d8","type":"inject","z":"3422d1.25b52d3","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"jsonrpc\":\"2.0\",\"result\":{\"status\":{\"heater_bed\":{\"temperature\":59.70476091770062,\"target\":60},\"filament_switch_sensor Sensor\":{\"filament_detected\":true},\"gcode\":{},\"fan\":{\"speed\":1,\"rpm\":null},\"display_status\":{\"progress\":0.3},\"print_stats\":{\"print_duration\":9321.11139888801,\"total_duration\":9559.235056339006,\"filename\":\"Trunk_Tall_0.16mm_PLA__7h39m.gcode\",\"state\":\"printing\"},\"toolhead\":{\"homed_axes\":\"xyz\",\"estimated_print_time\":61695.956782333335,\"print_time\":61698.806354975015,\"extruder\":\"extruder\"},\"extruder\":{\"target\":210,\"temperature\":209.99919772313908}},\"eventtime\":96164.416848014},\"id\":\"3041544688\"}","payloadType":"json","x":110,"y":300,"wires":[["dba78653.d53ac8"]]},{"id":"dba78653.d53ac8","type":"switch","z":"3422d1.25b52d3","name":"","property":"payload.result.status","propertyType":"msg","rules":[{"t":"hask","v":"filament_switch_sensor Sensor","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":370,"y":300,"wires":[["b7d541b3.632ae"]]},{"id":"b7d541b3.632ae","type":"debug","z":"3422d1.25b52d3","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":590,"y":300,"wires":[]}]

What version of Node-RED are you using?

1.3.2 (no update available, because I'm running it inside ioBroker)

There is a difference between yours and mine:

property of the switch node

[{"id":"8de88255.c03ed","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"fd2dd455.7d85d8","type":"inject","z":"8de88255.c03ed","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"jsonrpc\":\"2.0\",\"result\":{\"status\":{\"heater_bed\":{\"temperature\":59.70476091770062,\"target\":60},\"filament_switch_sensor Sensor\":{\"filament_detected\":true},\"gcode\":{},\"fan\":{\"speed\":1,\"rpm\":null},\"display_status\":{\"progress\":0.3},\"print_stats\":{\"print_duration\":9321.11139888801,\"total_duration\":9559.235056339006,\"filename\":\"Trunk_Tall_0.16mm_PLA__7h39m.gcode\",\"state\":\"printing\"},\"toolhead\":{\"homed_axes\":\"xyz\",\"estimated_print_time\":61695.956782333335,\"print_time\":61698.806354975015,\"extruder\":\"extruder\"},\"extruder\":{\"target\":210,\"temperature\":209.99919772313908}},\"eventtime\":96164.416848014},\"id\":\"3041544688\"}","payloadType":"json","x":590,"y":260,"wires":[["dba78653.d53ac8"]]},{"id":"dba78653.d53ac8","type":"switch","z":"8de88255.c03ed","name":"","property":"payload","propertyType":"msg","rules":[{"t":"hask","v":"filament_switch_sensor Sensor","vt":"str"}],"checkall":"true","repair":false,"outputs":1,"x":850,"y":260,"wires":[["b7d541b3.632ae"]]},{"id":"b7d541b3.632ae","type":"debug","z":"8de88255.c03ed","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1070,"y":260,"wires":[]}]

Ah I guess the switchnode can't look deep enough inside the subobjects right?

EDIT: My filament will runout in just a min or so, I will report back (because then the property changes a bit, right now it is just the message from websocket that is published every 10 sec for status update and not for "status changed").

Right - it only looks at the object you've configure it to look at.

1 Like

Ok setting to payload.result.status returns fan and filament_switch_sensor Sensor. Setting to payload does not return fan or filament_switch_sensor Sensor.

[{"id":"b7bd45fb.2b9a8","type":"inject","z":"9b3f9f31.c45298","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"jsonrpc\":\"2.0\",\"result\":{\"status\":{\"heater_bed\":{\"temperature\":59.70476091770062,\"target\":60},\"filament_switch_sensor Sensor\":{\"filament_detected\":true},\"gcode\":{},\"fan\":{\"speed\":1,\"rpm\":null},\"display_status\":{\"progress\":0.3},\"print_stats\":{\"print_duration\":9321.11139888801,\"total_duration\":9559.235056339006,\"filename\":\"Trunk_Tall_0.16mm_PLA__7h39m.gcode\",\"state\":\"printing\"},\"toolhead\":{\"homed_axes\":\"xyz\",\"estimated_print_time\":61695.956782333335,\"print_time\":61698.806354975015,\"extruder\":\"extruder\"},\"extruder\":{\"target\":210,\"temperature\":209.99919772313908}},\"eventtime\":96164.416848014},\"id\":\"3041544688\"}","payloadType":"json","x":190,"y":680,"wires":[["9ed57356.2c6a78"]]},{"id":"9ed57356.2c6a78","type":"switch","z":"9b3f9f31.c45298","name":"","property":"payload.result.status","propertyType":"msg","rules":[{"t":"hask","v":"fan","vt":"str"},{"t":"hask","v":"filament_switch_sensor Sensor","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":3,"x":340,"y":680,"wires":[["8d284cfa.0fd74"],["8d284cfa.0fd74"],[]]},{"id":"8d284cfa.0fd74","type":"debug","z":"9b3f9f31.c45298","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":570,"y":660,"wires":[]}]

If the keys you are after are on different object properties you could nest the switch node with another using the a know property that always exists to link to the other switch node.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.