Telegram message open windows when alarm is activated

I am trying to get messaged when I activate alarm and forgot to close all windows/doors.

let payload = {chatId: xxx,type: "message",content: $count(payload) > 0 ? "Der Alarm wurde aktiviert, aber folgende Fenster / Türen stehen noch offen: " & $join(payload.attributes.friendly_name, ", ") & "." : "Der Alarm wurde aktiviert."}
return {payload}

But what comes back is

"ReferenceError: $count is not defined (line 1, col 60)"

Any ideas what I am doing wrong? Here’s my flow where I xxx’ed my chatId and deleted the telegram sender node.

[{"id":"e14572a07035c591","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"8d7f7a80c9974c2b","type":"ha-get-entities","z":"e14572a07035c591","name":"","server":"86616542.19f378","version":0,"rules":[{"property":"entity_id","logic":"is","value":"binary_sensor\\.(?:door|window)_.+|cover\\.garage_door","valueType":"re"},{"property":"state","logic":"is","value":"on","valueType":"str"}],"output_type":"split","output_empty_results":true,"output_location_type":"msg","output_location":"payload","output_results_count":1,"x":290,"y":100,"wires":[["3b1d79b397891cd4"]]},{"id":"9df263e40d4a89b1","type":"inject","z":"e14572a07035c591","name":"","props":[{"p":"time","v":"","vt":"date"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":110,"y":100,"wires":[["8d7f7a80c9974c2b"]]},{"id":"36eb30304963baaa","type":"debug","z":"e14572a07035c591","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":80,"wires":[]},{"id":"3b1d79b397891cd4","type":"function","z":"e14572a07035c591","name":"","func":"let payload = {chatId: xxx,type: \"message\",content: $count(payload) > 0 ? \"Der Alarm wurde aktiviert, aber folgende Fenster / Türen stehen noch offen: \" & $join(payload.attributes.friendly_name, \", \") & \".\" : \"Der Alarm wurde aktiviert.\"}\nreturn {payload}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":530,"y":120,"wires":[["36eb30304963baaa"]]},{"id":"86616542.19f378","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":"30"}]

You’re trying to use JSONata functions inside a function node that only accepts javascript.

const names = payload.map((e) => e.attributes.friendly_name).join(", ");
const payload = {
    chatId: xxx,
    type: "message",
    content: payload.length ? `Der Alarm wurde aktiviert, aber folgende Fenster / Türen stehen noch offen: ${names}.` : "Der Alarm wurde aktiviert."
};
return {payload};

Thanks for your reply. Now I’m receiving this:

"ReferenceError: Cannot access 'payload' before initialization (line 1, col 15)"
const names = msg.payload.map((e) => e.attributes.friendly_name).join(", ");
const payload = {
    chatId: xxx,
    type: "message",
    content: msg.payload.length ? `Der Alarm wurde aktiviert, aber folgende Fenster / Türen stehen noch offen: ${names}.` : "Der Alarm wurde aktiviert."
};
return {payload};

EDIT: It works perfectly! Thank you!

Since a call service node accepts JSONata functions, I tried a modification of the code above so that my HomePod minis notify me via tts, which works immediately. Hope this will help others who are interested.

{"message":$count(payload) > 0 ? "Die Alarmanlage wurde aktiviert, aber folgende Türen oder Fenster sind noch offen: " & $join(payload.attributes.friendly_name, ", ") & "." : "Die Alarmanlage wurde aktiviert."}

Thanks again because the code also comes from you from another thread.