Combining multiple entity values from HA to form an email message body

Hi all, I’m new to Node-RED, using it on the Home Assistant platform which I’m also fairly new to. Since HA has exposed my printer’s toner cartridge levels as entities I thought a nice first Node-RED project would be to send a weekly email to myself with the different toner cartridge levels.

I can see that the inject node can be set to trigger at a specific time and day so that looks like a good starting point. At the other end of the flow I have an email node that I have configured with SMTP details and emails send fine.

At the moment, in the middle of the flow, I have current-state nodes which I’ve configured to retrieve the values of the toner cartridge entities. I can get a single current-state nodes to pass its value through to the email body but I’m struggling to combine multiple current-state nodes. I’ve been trying to use the join node but I can only get it to work if I have separate inject nodes working in conjunction with each current-state node. Great that it works, but I don’t want to have to set the trigger interval on every inject node.

A further consideration that I haven’t begun to look at yet is that once I have the key/value pairs of the toner cartridges and their toner levels it would be nice to wrap all this with some friendly message text.

In summary, it feels like the ‘architecture’ of my flow isn’t quite right, so grateful for any pointers about the best way to approach this. Thanks!

Just a guess as I do not use HA and you have not shown your flow.

Connect the current state nodes in series. After each current state node move the payload/response to a holding variable then at the end use the holding variable to create the email. You can use a template node to create a template email.

Thanks for that thought about processing in series @E1cid - I'll give it a go.

I was in two minds about sharing my flow but decided against it as at the moment it's just a jumble of nodes that I'm linking / unlinking as I test different approaches. If I need any further help on this I'll be sure to include my flow.

Thanks again @E1cid for your suggestions. I got everything working using your 'in series' suggestion. Here's the flow in case it helps anyone else:

[{"id":"423144e8.afb2ec","type":"tab","label":"Kyocera Status Email ","disabled":false,"info":""},{"id":"5e68b053.a8876","type":"e-mail","z":"423144e8.afb2ec","server":"smtp.gmail.com","port":"465","secure":true,"tls":false,"name":"someone@gmail.com","dname":"Send Email","x":590,"y":580,"wires":[]},{"id":"3f5e0e7e.1170a2","type":"api-current-state","z":"423144e8.afb2ec","name":"Black","server":"","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"sensor.ecosys_m5526cdw_black_tk_5240k","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":150,"y":340,"wires":[["1c55de1e.f4aa12"]]},{"id":"fc14ecd9.50a8e","type":"api-current-state","z":"423144e8.afb2ec","name":"Cyan","server":"","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"sensor.ecosys_m5526cdw_cyan_tk_5240cs","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":150,"y":420,"wires":[["b5c2c714.867db8"]]},{"id":"e9d70752.152be8","type":"inject","z":"423144e8.afb2ec","name":"Trigger","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"00 08 * * 2","once":false,"onceDelay":0.1,"topic":"Kyocera Toner Status","payload":"","payloadType":"date","x":100,"y":260,"wires":[["3f5e0e7e.1170a2"]]},{"id":"c2cd77bc.4c7c78","type":"template","z":"423144e8.afb2ec","name":"HTML","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html>\n<head>\n<style>\n    table, th, td {\n        border: 1px solid black;\n        border-collapse: collapse;\n    }\n    th, td {\n        padding: 15px;\n    }\n</style>\n</head>\n<body>\n    <p>Current toner cartridge levels are:</p>\n    <table>\n        <tr>\n            <th>Cartridge</th>\n            <th>Level</th>\n        </tr>\n        <tr>\n            <td>Black</td>\n            <td>{{flow.black}}%</td>\n        </tr>\n        <tr>\n            <td>Cyan</td>\n            <td>{{flow.cyan}}%</td>\n        </tr>\n        <tr>\n            <td>Magenta</td>\n            <td>{{flow.magenta}}%</td>\n        </tr>\n        <tr>\n            <td>Yellow</td>\n            <td>{{flow.yellow}}%</td>\n        </tr>        \n    </table>\n</body>\n\n\n\n\n","output":"str","x":450,"y":580,"wires":[["5e68b053.a8876"]]},{"id":"8069b415.1bb5d8","type":"api-current-state","z":"423144e8.afb2ec","name":"Magenta","server":"","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"sensor.ecosys_m5526cdw_magenta_tk_5240ms","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":160,"y":500,"wires":[["3174e245.bac53e"]]},{"id":"7a8bb93.68d7148","type":"api-current-state","z":"423144e8.afb2ec","name":"Yellow","server":"","version":1,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"sensor.ecosys_m5526cdw_yellow_tk_5240ys","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":150,"y":580,"wires":[["ad3f8bc0.dc5218"]]},{"id":"1c55de1e.f4aa12","type":"change","z":"423144e8.afb2ec","name":"Black","rules":[{"t":"set","p":"payload","pt":"msg","to":"black","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":340,"wires":[["fc14ecd9.50a8e"]]},{"id":"b5c2c714.867db8","type":"change","z":"423144e8.afb2ec","name":"Cyan","rules":[{"t":"set","p":"payload","pt":"msg","to":"cyan","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":420,"wires":[["8069b415.1bb5d8"]]},{"id":"3174e245.bac53e","type":"change","z":"423144e8.afb2ec","name":"Magenta","rules":[{"t":"set","p":"payload","pt":"msg","to":"magenta","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":500,"wires":[["7a8bb93.68d7148"]]},{"id":"ad3f8bc0.dc5218","type":"change","z":"423144e8.afb2ec","name":"Yellow","rules":[{"t":"set","p":"payload","pt":"msg","to":"yellow","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":580,"wires":[["c2cd77bc.4c7c78"]]}]

Essentially I used a single input node as the trigger and then for each toner cartridge entity captured the value with a current state node and then wrote the value to a flow context variable (ie one variable for each entity) using a change node. (I started off using a function node with some JavaScript before realising the change node could do this.) Finally, I used a template node to extract the flow context variables in to an HTML table for passing to an email node.

It's all working very nicely but since I'm completely new to this if there are improvements I'd love to know about them. Thanks!

I would ask in the node-red HA forum I would think there may be a way to get all the info in one request. As I don't use HA I can not help more.

Thanks for the suggestion! I actually figured it out myself:

[{"id":"5e68b053.a8876","type":"e-mail","z":"423144e8.afb2ec","server":"smtp.gmail.com","port":"465","secure":true,"tls":false,"name":"someone@gmail.com","dname":"Send Email","x":950,"y":260,"wires":[]},{"id":"9a39108f.48a4a","type":"ha-get-entities","z":"423144e8.afb2ec","server":"","name":"Get Entities","rules":[{"property":"entity_id","logic":"starts_with","value":"sensor.ecosys_m5526cdw","valueType":"str"},{"property":"state","logic":"gt","value":"0","valueType":"num"}],"output_type":"array","output_empty_results":false,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":580,"y":160,"wires":[["46b7108a.2c947"]]},{"id":"1e872044.5ef56","type":"debug","z":"423144e8.afb2ec","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":930,"y":160,"wires":[]},{"id":"46d2db8d.4b02b4","type":"inject","z":"423144e8.afb2ec","name":"Trigger","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"05 08 * * 2","once":false,"onceDelay":0.1,"topic":"Kyocera Toner Status","payload":"","payloadType":"date","x":410,"y":160,"wires":[["9a39108f.48a4a"]]},{"id":"46b7108a.2c947","type":"template","z":"423144e8.afb2ec","name":"HTML","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<html>\n<head>\n<style>\n    table, th, td {\n        border: 1px solid black;\n        border-collapse: collapse;\n    }\n    th, td {\n        padding: 15px;\n    }\n    th {\n        text-align: left;\n    }\n</style>\n</head>\n<body>\n    <p>Current toner cartridge levels are:</p>\n    <table>\n        <tr>\n            <th>Cartridge</th>\n            <th>Remaining (%)</th>\n        </tr>\n        {{#payload}}\n        <tr>\n            <td>{{attributes.friendly_name}}</td>\n            <td>{{state}}</td>\n        </tr>\n        {{/payload}}\n        </tr>        \n    </table>\n</body>","output":"str","x":750,"y":160,"wires":[["1e872044.5ef56","5e68b053.a8876"]]}]

There is an ha-get-entities node in my Node-RED installation in which one can configure a query to return an array of entities. Feels like that could be a node that was created for HA?

I've then set the template node to iterate over the array, pulling out the desired properties to construct a similar HTML table to my original flow.

My original flow was 11 nodes, the flow above just 4. Neat!

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