How can I get the Node name and passing it to a notification

Good morning,

I am new in this so be patient if I am not using the correct words.
I am trying to get notification from several sensors about their battery levels. The picture below shows my flow, which it is working and provides me with a notification if the percentage of the battery is below what I set in the switch node.
As it is now, I can only get the numeric value of the battery sensors but not the name of the sensor (or node) where the information are caming from. Althought is feasible, I would like to NOT repeat the same for each sensor and hardcoded the sensor's node name in the Function node.
I would like to know and how can the node name be collected and passed together with the battery level so the notification will have the sensor (node Name) and the battery value.
Is this possible?

Thank you for your help
M

[{"id":"9731ebee.c6c868","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"c91a5df8.a2bc58","type":"function","z":"9731ebee.c6c868","name":"Data","func":"msg.payload = \n{\n  \"data\": \n  {\n    \"message\": msg.payload + \" %\"\n  }\n}\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":490,"y":240,"wires":[["5c61ed5.44ce994","6bd72ad6.8219cc"]]},{"id":"5c61ed5.44ce994","type":"api-call-service","z":"9731ebee.c6c868","name":"Notify IOS","server":"8ff04ac7.39cc7","version":1,"debugenabled":true,"service_domain":"notify","service":"mobile_app_mauros_iphone","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":650,"y":200,"wires":[[]]},{"id":"383943bb.98c064","type":"debug","z":"9731ebee.c6c868","name":"Battery","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":280,"y":180,"wires":[]},{"id":"5dbe0b68.ff7314","type":"switch","z":"9731ebee.c6c868","name":"Check Battery","property":"payload","propertyType":"msg","rules":[{"t":"lte","v":"90","vt":"str"},{"t":"gte","v":"100","vt":"str"}],"checkall":"true","repair":true,"outputs":2,"x":300,"y":240,"wires":[["c91a5df8.a2bc58","383943bb.98c064"],[]]},{"id":"50a1bbb9.b782cc","type":"inject","z":"9731ebee.c6c868","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"30","payloadType":"num","x":80,"y":140,"wires":[["5dbe0b68.ff7314"]]},{"id":"6bd72ad6.8219cc","type":"debug","z":"9731ebee.c6c868","name":"TS data","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":640,"y":260,"wires":[]},{"id":"20d1f13.c57c50e","type":"server-state-changed","z":"9731ebee.c6c868","name":"guest battery","server":"8ff04ac7.39cc7","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.ble_battery_guest","entityidfiltertype":"exact","outputinitially":false,"state_type":"num","haltifstate":"","halt_if_type":"num","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":70,"y":200,"wires":[["5dbe0b68.ff7314"]]},{"id":"25840ef.5bcc872","type":"server-state-changed","z":"9731ebee.c6c868","name":"Family Battery","server":"8ff04ac7.39cc7","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.ble_battery_family","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"num","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":70,"y":280,"wires":[["5dbe0b68.ff7314"]]},{"id":"8ff04ac7.39cc7","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

Hi & welcome to the forum.

Could you please read this post - How to share code or flow json and then edit your post above accordingly (use the pencil icon).
Without those changes, the forum software will make subtle changes to your flow code, and other users will not be able to import it, and therefore help you.

Thank you

1 Like

Set the first debug node to show Complete Message and paste here what you see for the two input types.

here the message

{"topic":"sensor.ble_battery_guest","payload":72,"data":{"entity_id":"sensor.ble_battery_guest","old_state":{"entity_id":"sensor.ble_battery_guest","state":72,"attributes":{"sensor type":"LYWSD03MMC","mac address":"A4:C1:38:56:F3:AC","last packet id":144,"rssi":-88,"unit_of_measurement":"%","friendly_name":"ble battery Guest","device_class":"battery"},"last_changed":"2021-01-06T17:16:03.343632+00:00","last_updated":"2021-01-06T17:16:03.343632+00:00","context":{"id":"881ac5d81252e58883c59a1731d0b4d2","parent_id":null,"user_id":null},"original_state":72,"timeSinceChangedMs":741355},"new_state":{"entity_id":"sensor.ble_battery_guest","state":72,"attributes":{"sensor type":"LYWSD03MMC","mac address":"A4:C1:38:56:F3:AC","last packet id":144,"rssi":-88,"unit_of_measurement":"%","friendly_name":"ble battery Guest","device_class":"battery"},"last_changed":"2021-01-06T17:16:03.343632+00:00","last_updated":"2021-01-06T17:16:03.343632+00:00","context":{"id":"881ac5d81252e58883c59a1731d0b4d2","parent_id":null,"user_id":null},"original_state":72,"timeSinceChangedMs":741355}},"_msgid":"5217527c.78ac0c"}

This is the best that I came up with.... I am sure you guys can do better than me..

in RED the part MUST be duplicated for each sensor

[{"id":"9731ebee.c6c868","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"c91a5df8.a2bc58","type":"function","z":"9731ebee.c6c868","name":"Data","func":"msg.payload = \n{\n  \"data\": \n  {\n    \"message\": msg.payload\n  }\n}\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":750,"y":280,"wires":[["6bd72ad6.8219cc","5c61ed5.44ce994"]]},{"id":"5c61ed5.44ce994","type":"api-call-service","z":"9731ebee.c6c868","name":"Notify IOS","server":"8ff04ac7.39cc7","version":1,"debugenabled":true,"service_domain":"notify","service":"mobile_app_mauros_iphone","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":950,"y":280,"wires":[[]]},{"id":"383943bb.98c064","type":"debug","z":"9731ebee.c6c868","name":"Battery","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":460,"y":160,"wires":[]},{"id":"5dbe0b68.ff7314","type":"switch","z":"9731ebee.c6c868","name":"Check Battery","property":"payload","propertyType":"msg","rules":[{"t":"lte","v":"98","vt":"str"},{"t":"gte","v":"100","vt":"str"}],"checkall":"true","repair":true,"outputs":2,"x":300,"y":240,"wires":[["383943bb.98c064","545fe0c9.60eaf"],[]]},{"id":"6bd72ad6.8219cc","type":"debug","z":"9731ebee.c6c868","name":"TS data","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":880,"y":180,"wires":[]},{"id":"20d1f13.c57c50e","type":"server-state-changed","z":"9731ebee.c6c868","name":"Guest battery","server":"8ff04ac7.39cc7","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.ble_battery_guest","entityidfiltertype":"exact","outputinitially":false,"state_type":"num","haltifstate":"","halt_if_type":"num","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":70,"y":240,"wires":[["5dbe0b68.ff7314","4120fb86.d7e014"]]},{"id":"25840ef.5bcc872","type":"server-state-changed","z":"9731ebee.c6c868","name":"Family Battery","server":"8ff04ac7.39cc7","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.ble_battery_family","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"num","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":70,"y":300,"wires":[["e91f9979.34fc9","b7b4a03c.ace258"]]},{"id":"e91f9979.34fc9","type":"switch","z":"9731ebee.c6c868","name":"Check Battery","property":"payload","propertyType":"msg","rules":[{"t":"lte","v":"98","vt":"str"},{"t":"gte","v":"100","vt":"str"}],"checkall":"true","repair":true,"outputs":2,"x":300,"y":300,"wires":[["b35ca79e.f2cb28"],[]]},{"id":"b35ca79e.f2cb28","type":"string","z":"9731ebee.c6c868","name":"Build notif. string","methods":[{"name":"toString","params":[]},{"name":"append","params":[{"type":"str","value":" %"}]},{"name":"append","params":[{"type":"str","value":" "}]},{"name":"append","params":[{"type":"flow","value":"payload.sensor2"}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":530,"y":300,"wires":[["c91a5df8.a2bc58"]]},{"id":"b7b4a03c.ace258","type":"string","z":"9731ebee.c6c868","name":"Sensor Location string","methods":[{"name":"chompLeft","params":[{"type":"str","value":"ble battery "}]}],"prop":"data.new_state.attributes.friendly_name","propout":"payload.sensor2","object":"msg","objectout":"flow","x":260,"y":380,"wires":[[]]},{"id":"4120fb86.d7e014","type":"string","z":"9731ebee.c6c868","name":"Sensor Location string","methods":[{"name":"chompLeft","params":[{"type":"str","value":"ble battery "}]}],"prop":"data.new_state.attributes.friendly_name","propout":"payload.sensor1","object":"msg","objectout":"flow","x":260,"y":140,"wires":[["c6725581.62383"]]},{"id":"545fe0c9.60eaf","type":"string","z":"9731ebee.c6c868","name":"Build notif. string","methods":[{"name":"toString","params":[]},{"name":"append","params":[{"type":"str","value":" %"}]},{"name":"append","params":[{"type":"str","value":" "}]},{"name":"append","params":[{"type":"flow","value":"payload.sensor1"}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":530,"y":240,"wires":[["c91a5df8.a2bc58","42950ecc.a5b848"]]},{"id":"42950ecc.a5b848","type":"debug","z":"9731ebee.c6c868","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":700,"y":120,"wires":[]},{"id":"c6725581.62383","type":"debug","z":"9731ebee.c6c868","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":460,"y":80,"wires":[]},{"id":"8ff04ac7.39cc7","type":"server","name":"Home Assistant","legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

Notice that the msg.topic includes the sensor name, so all you have to do is to include that in the notification message.

something like this at the function node ?

msg.payload = 
{
  "data": 
  {
    "message": msg.payload + msg.topic
  }
}
return msg;

Something like that, but you might like to format it a bit more meaningfully. Try that first though to check it works.

Thank you for your help, I really appreciated .

I ended up with this

I don't know if this is "kosher" enough but my Function node has this in it:
(topic: "sensor.ble_battery_guest")

var myDataArray = msg.topic.split('_');
var ble      = myDataArray[0];
var battery  = myDataArray[1];
var location = myDataArray[2];

msg.payload = 
{
  "data": 
  {
    "message": location + " " + msg.payload + " % " 
  }
}
return msg;

I am sure there is a better way to extract the last word in the msg.topict... I will find a cleaner way to do it.

1 Like

If you are sure that all your topics will be of that format then that is the way I would do it. You don't actually need the lines setting ble and battery as you are not using them.

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