Show ALL attributes data

#1

I am tracking packages from a carrier which works perfectly.
At the moment i receive a Telegram message with the info: "you have X packages".

But i want to see where the packages comes from. I can see this info in the attributes data but i don't know how to extract it because the company is named different everytime.
WqYFXmm

In this example I want to send the text "999 Games" and "Memory-Garden vof" to my phone.

Is this possible without knowing upfront what's in the attributes data?

0 Likes

#2

If you don't know what the attributes are how would you determine which attributes to send?
[Edit] just noticed the post title says All attributes, but the example you gave you say you only want some of them.

0 Likes

#3

Is the order of the data the same every time? i.e. is the company name the key of the second part of the attributes object?

0 Likes

#4

Do you have other examples? It looks like "friendly_name:" "Unit_of_measurement" "attribution" and "icon" would be always present or at the very least not company names. You might be able to use a split node to send all parts of the attributes object as individual messages and use the switch node to filter out any unwanted parts.

0 Likes

#5

I have found a way myself. It's probably not the most efficient way but I am a random guy having fun with node-red, not a Software developer.

@zenofmud I think it depends on how many packages i will receive so no.
@JayDickson This is how i fixed it. I filtered all the attributes that are always present (friendly_name, unit_of_measurement, icon, attribution). And the usefull data will be filtered with "otherwise" on the switch node.

 [{"id":"fab7ae08.18ab9","type":"server-state-changed","z":"3d3f5fac.25e4b","name":"PostNL","server":"cac0837e.bc9b7","entityidfilter":"sensor.postnl","entityidfiltertype":"substring","haltifstate":"","outputinitially":true,"x":129,"y":244,"wires":[["a2999169.d632a"]]},{"id":"ec925256.48edc","type":"comment","z":"3d3f5fac.25e4b","name":"PostNL","info":"","x":129,"y":194.43750190734863,"wires":[]},{"id":"a2999169.d632a","type":"switch","z":"3d3f5fac.25e4b","name":">=1 pakketjes","property":"payload","propertyType":"msg","rules":[{"t":"gte","v":"1","vt":"num"}],"checkall":"false","repair":false,"outputs":1,"x":304.2421989440918,"y":243.4375114440918,"wires":[["1a24f4a9.43fc7b"]]},{"id":"1a24f4a9.43fc7b","type":"function","z":"3d3f5fac.25e4b","name":"Atttributes splitsen","func":"var attributes = msg.data.new_state.attributes;\n\nreturn[{payload:attributes}]","outputs":1,"noerr":0,"x":536.8333911895752,"y":243.66671562194824,"wires":[["4aa1f7e4.eefaf8"]]},{"id":"48048c31.bb49c4","type":"switch","z":"3d3f5fac.25e4b","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Information provided by PostNL","vt":"str"},{"t":"eq","v":"packages","vt":"str"},{"t":"eq","v":"postnl","vt":"str"},{"t":"eq","v":"mdi:package-variant-closed","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":5,"x":864.1668510437012,"y":243.6666717529297,"wires":[[],[],[],[],["9388a722.c832b8"]]},{"id":"9388a722.c832b8","type":"split","z":"3d3f5fac.25e4b","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":1054.0001907348633,"y":240.00004291534424,"wires":[["af1394e9.909098"]]},{"id":"af1394e9.909098","type":"function","z":"3d3f5fac.25e4b","name":"Bericht","func":"msg.payload = \n{ text: `Op ${msg.payload} komt er een pakketje van ${msg.parts.parts.key} `, \nparse_mode: \"Markdown\" };\n\nreturn msg;","outputs":1,"noerr":0,"x":1210.0000343322754,"y":240.00004768371582,"wires":[["d5ebfbd3.0bc0f8"]]},{"id":"d5ebfbd3.0bc0f8","type":"telegrambot-payload","z":"3d3f5fac.25e4b","name":"Versturen","bot":"76d092d0.28022c","chatId":"667099650","sendMethod":"sendMessage","payload":"","x":1381.0000381469727,"y":240.00004768371582,"wires":[[]]},{"id":"4aa1f7e4.eefaf8","type":"split","z":"3d3f5fac.25e4b","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":728.1667671203613,"y":244.00009536743164,"wires":[["48048c31.bb49c4"]]},{"id":"cac0837e.bc9b7","type":"server","z":"","name":"Home Assistant","legacy":false,"hassio":false,"rejectUnauthorizedCerts":true},{"id":"76d092d0.28022c","type":"telegrambot-config","z":"","botname":"Kevintelegrambot","usernames":"","chatIds":"667099650","pollInterval":"300"}]
0 Likes

#6

This is possible by utilizing a JSONata expression that navigates to the part of the structure with the attributes object, and then returns only the field names that are not recognized (i.e. icon, friendly_name, etc).

The first step is to hover the cursor over that attributes object, and when the little buttons appear on the right side, select the first one to copy the "path" to that object. From the picture you posted, it looks to be msg.data.new_state.attributes -- so assuming that's correct, you can use this expression to first strip off the "recognized" fields, and return only the keys (field names) that are left:

$keys(data.new_state.attributes ~> | $ | {}, ["attribution", "unit_of_measurement", "friendly_name", "icon"] | )[]

Another approach that may be less cryptic would be to get all the keys, and then filter out the ones that you recognize as not being store names:

$keys(data.new_state.attributes)[$ != "attribution" and $ != "unit_of_measurement" and $ != "friendly_name" and $ != "icon"][]

Still quite an ugly expression, especially if there are lots of field names that you need to exclude. From this little bit of data, it appears like we can return only the field names that contain valid date strings -- in which case, we can use a regular expression to look for matches, like so:

[$sift(data.new_state.attributes, /\d{4}-\d{2}-\d{2}/) ~> $keys()]

Try using one of these expressions in a change node -- just be sure to select the J: JSONata expression option, not the {} JSON text option... it even has a built-in expression tester that you can use against your actual data copied from the debug side panel.

0 Likes

#7

Unfortunately none of the 3 options work for me. I don't have any coding skills so I really don't know how to change them correctly.

0 Likes