Calendar Message Separation

Hi, i managed to let my Office Speaker say when the Birthday Calendar contains an entry. Nest Mini then announces the Name of the entry. Very often this is only the name of the person having birthday. In some cases i have the Name and the Year of Birth in the Google calendar (to know the age of the person), like "Chester Thompson (1956)".

[{"id":"b68b2b61.836ef8","type":"inject","z":"50076042.9e6b9","name":"Stündlich 13 - 19 Uhr","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"*/2 11-18 * * *","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":180,"y":1000,"wires":[["d9ab86b2.e67d08"]]},{"id":"d9ab86b2.e67d08","type":"api-current-state","z":"50076042.9e6b9","name":"Geburtstag Eintrag","server":"d6fce7c.2c79718","version":2,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"calendar.geburtstage","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"x":440,"y":1000,"wires":[["29bcde8e.0ecd12"],[]]},{"id":"29bcde8e.0ecd12","type":"api-call-service","z":"50076042.9e6b9","name":"Sprachausgabe Büro","server":"d6fce7c.2c79718","version":3,"debugenabled":false,"service_domain":"tts","service":"google_translate_say","entityId":"media_player.buro","data":"{\t    \"message\": \"Heute hat {{data.attributes.message}} Geburtstag\" }","dataType":"json","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":700,"y":1000,"wires":[[]]},{"id":"d6fce7c.2c79718","type":"server","name":"Home Assistant","version":1,"legacy":false,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

Now i want the script to split the calendar.birthday.data.attributes.messages when it contains brackets "(" and change the output to a calculated age, like "today is Chester Thomposon birthday, turning 65 years.")

How can I manage this with Node-Red?

Feed the message you need to manipulate into a debug node and show us what it says.

Z3n32LITlo

Demo flow...

[{"id":"4f61066c5bc18716","type":"inject","z":"553814a2.1248ec","name":"Chester Thompson (1956)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Chester Thompson (1956)","payloadType":"str","x":1150,"y":2860,"wires":[["dda456ca1b45374e"]]},{"id":"dda456ca1b45374e","type":"function","z":"553814a2.1248ec","name":"","func":"msg.originalPayload = msg.payload;\nconst str = (msg.payload + \"\").trim();\n\nif (/(.*)\\((\\d+)\\)/gm.test(str)) {\n    let parts = str.split(\"(\");\n    if(parts.length == 2) {\n        let name = (parts[0] + \"\").trim();\n        let birthYear = parseInt(parts[1]);\n        let nowYear = (new Date()).getFullYear()\n        let age = nowYear - birthYear;\n        msg.payload = `Today is ${name}'s birthday, turning ${age} years.`\n\n        msg.name = name;\n        msg.birthYear = birthYear;\n        msg.age = age;\n    }\n} else {\n    msg.payload = `Today is ${str}'s birthday.`\n}\nnode.status({text:msg.payload});\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1360,"y":2940,"wires":[["c37d26b34775e847"]]},{"id":"a61b30efd7639d26","type":"inject","z":"553814a2.1248ec","name":"Billy","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Billy","payloadType":"str","x":1090,"y":2940,"wires":[["dda456ca1b45374e"]]},{"id":"1525291b6c00ed18","type":"inject","z":"553814a2.1248ec","name":"Fred Fredrikson (1972)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Fred Fredrikson (1972)","payloadType":"str","x":1140,"y":2900,"wires":[["dda456ca1b45374e"]]},{"id":"c37d26b34775e847","type":"debug","z":"553814a2.1248ec","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1520,"y":2940,"wires":[]}]
1 Like

Wow @Steve-Mcl, that is absolutely perfect. Thanks soooo much !!! :kissing_heart:

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