Formatting array output for email

I have an array of strings ( sentences telling the sensor and battery level ) for each device that has a battery level. I use get_entities to get the array of objects, then use split to break them into separate messages, then use change to get the entity and battery level into the payload of the msg, add join to put them all back together in one msg so that I get one email instead of forty. The problem is it comes over as a json formatted list as follows

[“sensor.closetapc_battery_charge battery level 100\n”,“sensor.apc_battery_charge battery level 100\n”,“sensor.lurch_battery battery level 100\n”,“sensor.rosey_battery battery level 100\n”,“sensor.kitchen_pantry_trap_battery_level battery level 78\n”,“sensor.chips_phone_battery_level battery level 90\n”,“sensor.4_in_1_sensor_battery_level_3 battery level 60.0\n”,“sensor.4_in_1_sensor_battery_level_2 battery level 60.0\n”,“sensor.4_in_1_sensor_battery_level_4 battery level 40.0\n”,“sensor.master_bath_sensor_battery_level battery level 30.0\n”,“sensor.garage_sensor_battery_level battery level 60.0\n”,“sensor.sunroom_window_sensor_1_battery_level battery level 0.0\n”,“sensor.sunroom_window_sensor_2_battery_level battery level 57.0\n”,“sensor.sunroom_window_sensor_3_battery_level battery level 78.0\n”,“sensor.sunroom_window_sensor_5_battery_level battery level 0.0\n”,“sensor.sunroom_window_sensor_6_battery_level battery level 100.0\n”,“sensor.4_in_1_sensor_battery_level_6 battery level 100.0\n”,“sensor.node_91_battery_level battery level 100.0\n”,“sensor.water_leak_xs_sensor_battery_level battery level 16.0\n”,“sensor.4_in_1_sensor_battery_level_5 battery level 20.0\n”,“sensor.sunroom_window_sensor_4_battery_level battery level 100.0\n”,“sensor.4_in_1_sensor_battery_level battery level 100.0\n”,“sensor.chips_bed_sensor_battery_level battery level 100.0\n”,“sensor.chips_medicine_switch_battery_level battery level 100.0\n”,“sensor.double_gate_battery battery level 100\n”,“sensor.single_gate_battery battery level 100\n”]

sorry about the way that looks, it looks that way in my inbox too which is the problem.
How do I change the format of the output to something more readable?

Please paste the list into a code box (</> button).

That is exactly what shows up in my inbox. When I try using the </> button to encapsulate the lines, I get an invalid code section error.
I’m not sure where you are going with your text about “How to help us help you”. I appreciate any help I can get. While I hope to get a useful answer, I understand that I may not get any answers, or I may get answers that are completely useless like yours. Please check the attitude at the door and try to be helpful, or just don’t reply.

If you clicked the “how to help us” link, you’d see that it is directed at a section of the article on formatting code correctly. I understand that the forum “link preview” doesn’t make that clear, but at least click on the link and you’ll see “where I’m going”.

Sadly, this forum software converts any quotes into “smart” ones if you write / paste as normal text. I was trying to get you to help us by pasting something starting like this, so that I could help you (despite my alleged attitude) by demonstrating a solution:

["sensor.closetapc_battery_charge battery level 100\n","sensor.apc_battery_charge battery level 100\n","sensor.lurch_battery battery level 100\n"...

Please also include the (Jinja?) code for the steps you describe to generate the email text, again, formatted as code.

1 Like

First, let me apologize for my response. It’s not an excuse, but yesterday was a really bad day. I should not have popped off like that. Below is the jinja code. I’ve tried several different things to get it to re-format so the last bit where I have the function was just another attempt at getting the \r\n recognized. I appreciate any help anyone can give. Please forgive my earlier outburst.

["sensor.closetapc_battery_charge battery level 100.\r\n","sensor.apc_battery_charge battery level 100.\r\n","sensor.lurch_battery battery level 100.\r\n","sensor.rosey_battery battery level 100.\r\n","sensor.kitchen_pantry_trap_battery_level battery level 76.\r\n","sensor.chips_phone_battery_level battery level 65.\r\n","sensor.4_in_1_sensor_battery_level_3 battery level 60.0.\r\n","sensor.4_in_1_sensor_battery_level_2 battery level 60.0.\r\n","sensor.4_in_1_sensor_battery_level_4 battery level 40.0.\r\n","sensor.master_bath_sensor_battery_level battery level 30.0.\r\n","sensor.garage_sensor_battery_level battery level 60.0.\r\n","sensor.sunroom_window_sensor_1_battery_level battery level 0.0.\r\n","sensor.sunroom_window_sensor_2_battery_level battery level 57.0.\r\n","sensor.sunroom_window_sensor_3_battery_level battery level 78.0.\r\n","sensor.sunroom_window_sensor_5_battery_level battery level 0.0.\r\n","sensor.sunroom_window_sensor_6_battery_level battery level 100.0.\r\n","sensor.4_in_1_sensor_battery_level_6 battery level 100.0.\r\n","sensor.node_91_battery_level battery level 100.0.\r\n","sensor.water_leak_xs_sensor_battery_level battery level 16.0.\r\n","sensor.4_in_1_sensor_battery_level_5 battery level 20.0.\r\n","sensor.sunroom_window_sensor_4_battery_level battery level 100.0.\r\n","sensor.4_in_1_sensor_battery_level battery level 100.0.\r\n","sensor.chips_bed_sensor_battery_level battery level 100.0.\r\n","sensor.chips_medicine_switch_battery_level battery level 100.0.\r\n","sensor.double_gate_battery battery level 100.\r\n","sensor.single_gate_battery battery level 100.\r\n"]

[{"id":"3d6698da5b972216","type":"server-state-changed","z":"d31bf3ae8018a0d0","name":"time","server":"cd6b10ad.21ada","version":5,"outputs":2,"exposeAsEntityConfig":"","entityId":"sensor.time","entityIdType":"exact","outputInitially":false,"stateType":"str","ifState":"22:08","ifStateType":"str","ifStateOperator":"is","outputOnlyOnStateChange":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":70,"y":1100,"wires":[["7af21e61cc56fbac"],[]]},{"id":"6678ce913dd6088b","type":"debug","z":"d31bf3ae8018a0d0","name":"debug 11","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":400,"y":1100,"wires":[]},{"id":"45e5a8e03cf08204","type":"inject","z":"d31bf3ae8018a0d0","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":80,"y":1000,"wires":[["7af21e61cc56fbac"]]},{"id":"2e5fe5fdd0821ed3","type":"split","z":"d31bf3ae8018a0d0","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":410,"y":1020,"wires":[["fd0ff3a6e8f6aca1","142cafee3ed79759"]]},{"id":"fd0ff3a6e8f6aca1","type":"change","z":"d31bf3ae8018a0d0","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.entity_id & \" battery level \" & payload.state &\".\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":980,"wires":[["5fc885efb16dfc8f"]]},{"id":"142cafee3ed79759","type":"debug","z":"d31bf3ae8018a0d0","name":"debug 13","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.attributes","targetType":"msg","statusVal":"","statusType":"auto","x":590,"y":1080,"wires":[]},{"id":"7af21e61cc56fbac","type":"ha-get-entities","z":"d31bf3ae8018a0d0","name":"","server":"cd6b10ad.21ada","version":1,"rules":[{"property":"entity_id","logic":"starts_with","value":"sensor","valueType":"str"},{"property":"attributes.device_class","logic":"is","value":"battery","valueType":"str"}],"outputType":"array","outputEmptyResults":true,"outputLocationType":"msg","outputLocation":"payload","outputResultsCount":1,"x":250,"y":1020,"wires":[["2e5fe5fdd0821ed3","6678ce913dd6088b"]]},{"id":"5fc885efb16dfc8f","type":"function","z":"d31bf3ae8018a0d0","name":"function 1","func":"msg.payload = msg.payload + \"\\r\\n\"; \nreturn msg;\n//\\r - Carriage Return 0x0D - 13\n//\\n - Line Feed 0x0A - 10\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":760,"y":980,"wires":[["de1faebee7302f59"]]},{"id":"de1faebee7302f59","type":"join","z":"d31bf3ae8018a0d0","name":"","mode":"auto","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":910,"y":980,"wires":[["e5ba5c16cacb8a10"]]},{"id":"e5ba5c16cacb8a10","type":"e-mail","z":"d31bf3ae8018a0d0","server":"smtp.gmail.com","port":"465","authtype":"BASIC","saslformat":true,"token":"payload","secure":true,"tls":true,"name”:”me@mydomain”,”dname":"","x":1090,"y":1040,"wires":[]},{"id":"cd6b10ad.21ada","type":"server","name":"Home Assistant","addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":true,"heartbeatInterval":"30","statusSeparator":"","enableGlobalContextStore":false}]

OK, so this is generated with Node-Red?

I don’t have any experience with that, but if you are able to use Jinja templates in the output, this may help:

I’ve read the list into a variable a, then got rid of your “Windows” '\r' characters, then joined them up.

You probably want a Node-Red expert to help though. If you still can, it might be worth adding the node-red tag to this topic.

sadly node-red isn’t an approved tag. LOL. Thanks for your help.

Looks like it’s a sub-category of the third-party one:

image

Found it. If anyone else needs to insert special characters into a string check out node-red-contrib-string. It brings string functions into node-red. I was able to replace a “|” with a “\n” which gave me the carriage return, line feed I needed in my payload going into the mail node.