String formatting in a function

I am trying to log everything that Alexa hears using the "On Device Activity" node.

I am pretty sure that everything is a string. This is how I am saving name, for example:

var a={payload:msg.payload.name};
flow.set("name",a);
return [a];

In the failing function I am trying to build a single line: "timestamp, name, command"

var devicetime = flow.get('timestamp');
var devicename = flow.get('name');
var devicecommand = flow.get('command');

var str = devicetime + ',' + devicename + ',' + devicecommand;
msg.payload = str;
return msg;

But this is what I get: "Friday, July 15th 2022, 5:10:26 pm",[object Object],[object Object]

So, what did I do wrong?
Thanks

What does the Alexa output look like?, can you provide the output you get from the node? (using the debug node)

Var a is an object, so flow name is an object
flow.set("name",a.payload)

@SteveMann What is the actual name of that node (node-red-contrib-?????). Your idea looks interesting.

As per @E1cid comment, the name is an object, but..

Friday, July 15th 2022, 5:10:26 pm",[object Object],[object Object]

doesn't explain the command property.
it will help if you could paste here, what the output is, before you process it.

It's node-res-contrib-alexaremote2-appelstrudel i believe.

@marcus-j-davies , the OP is probably setting the command context the same way.

@E1cid That's what I'm betting also.

@SteveMann
Not knowing your flow, something like below should achieve your goal.

flow.set("timestamp",msg.payload.timestamp);
flow.set("name",msg.payload.name);
flow.set("command",msg.payload.command);

return msg

later....

const devicetime = flow.get('timestamp');
const devicename = flow.get('name');
const devicecommand = flow.get('command');

const str = `${devicetime},${devicename},${devicecommand}`;
msg.payload = str;
return msg;

All terrific input and much appreciated.
It was a doh' moment when @e1cid pointed out that I was saving objects. But I am still having a problem with extracting the command.

@zenofmud - The node is "Alexa Event" from "node-red-contrib-alexa-remote2", V3.10.4

The flow:

[{"id":"3bcee00cd20bb90b","type":"function","z":"cc2b243f.efbf58","name":"Build the log file line","func":"const devicetime = flow.get('timestamp');\nconst devicename = flow.get('name');\nconst devicecommand = flow.get('command');\n\nconst str = `${devicetime},${devicename},${devicecommand}`;\nmsg.payload = str;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":500,"y":740,"wires":[["4eefd537527fd7b6"]]},{"id":"1dcdac75e08018e9","type":"alexa-remote-event","z":"cc2b243f.efbf58","name":"","account":"4627f780.84a6a8","event":"ws-device-activity","x":150,"y":600,"wires":[["f211df871972ea36","b75fa357259ba43c","ac8ec3cc9bf0e97e","97e94defad52ded1"]]},{"id":"f211df871972ea36","type":"function","z":"cc2b243f.efbf58","name":"Get name","func":"flow.set(\"name\",msg.payload.name);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":600,"wires":[["102b0745cea8db28"]]},{"id":"b75fa357259ba43c","type":"function","z":"cc2b243f.efbf58","name":"Get Command","func":"var a={payload:msg.payload.description.summary};\nflow.set(\"command\",a);\nreturn [a];","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":360,"y":640,"wires":[["c8426b41292c2a67"]]},{"id":"c8426b41292c2a67","type":"ui_text","z":"cc2b243f.efbf58","group":"30106f26816982fe","order":2,"width":0,"height":0,"name":"Command","label":"Command","format":"{{msg.payload}}","layout":"row-spread","className":"","x":790,"y":640,"wires":[]},{"id":"102b0745cea8db28","type":"ui_text","z":"cc2b243f.efbf58","group":"30106f26816982fe","order":1,"width":0,"height":0,"name":"","label":"Originating Device","format":"{{msg.payload}}","layout":"row-spread","className":"","x":810,"y":600,"wires":[]},{"id":"ac8ec3cc9bf0e97e","type":"function","z":"cc2b243f.efbf58","name":"Get Timestamp","func":"var a={payload:msg.payload.creationTimestamp.mydate};\nreturn [a];","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":200,"y":680,"wires":[["bedfa4ed716e3f76"]]},{"id":"d71e4d59e2e1b75e","type":"ui_text","z":"cc2b243f.efbf58","group":"30106f26816982fe","order":3,"width":0,"height":0,"name":"Timestamp","label":"Tmestamp","format":"{{msg.payload}}","layout":"row-spread","className":"","x":810,"y":680,"wires":[]},{"id":"bedfa4ed716e3f76","type":"moment","z":"cc2b243f.efbf58","name":"","topic":"","input":"","inputType":"msg","inTz":"America/New_York","adjAmount":0,"adjType":"days","adjDir":"add","format":"\"dddd, MMMM Do YYYY, h:mm:ss a\"","locale":"C","output":"","outputType":"msg","outTz":"America/New_York","x":400,"y":680,"wires":[["f6dc1de10ed24cc1"]]},{"id":"4eefd537527fd7b6","type":"file","z":"cc2b243f.efbf58","name":"","filename":"/share/logs/alexa.log","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"none","x":740,"y":740,"wires":[[]]},{"id":"f6dc1de10ed24cc1","type":"function","z":"cc2b243f.efbf58","name":"Save timestamp","func":"flow.set(\"timestamp\",msg.payload);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":620,"y":680,"wires":[["d71e4d59e2e1b75e"]]},{"id":"97e94defad52ded1","type":"delay","z":"cc2b243f.efbf58","name":"","pauseType":"delay","timeout":"10","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":290,"y":740,"wires":[["3bcee00cd20bb90b"]],"info":"Allows all data to be flow.saved."},{"id":"4627f780.84a6a8","type":"alexa-remote-account","name":"","authMethod":"proxy","proxyOwnIp":"192.168.1.57","proxyPort":"3456","cookieFile":"alexa.txt","refreshInterval":"1","alexaServiceHost":"pitangui.amazon.com","amazonPage":"amazon.com","acceptLanguage":"en-US","userAgent":"","useWsMqtt":"on","autoInit":"off"},{"id":"30106f26816982fe","type":"ui_group","name":"Last Command Summary","tab":"cc8daca91241397e","order":2,"disp":true,"width":"6","collapse":false,"className":""},{"id":"cc8daca91241397e","type":"ui_tab","name":"Alexa","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

However, I am still having a problem extracting the command. In my flow the output of the Get Command function node is the command: "turn on the office light", for example. But in my log file it is: "Friday, July 15th 2022, 7:20:57 pm",Stephen,[object Object]

Here is the output from the Alexa Event node when I say "Alexa turn off the office light"

{"payload":{"data":{"_disambiguationId":null,"activityStatus":"SUCCESS","creationTimestamp":1657927436872,"description":"{\"summary\":\"alexa turn off office light\",\"firstUtteranceId\":\"A4ZP7ZC4PI6TO:1.0/2022/07/15/23/DEVICE_UNSPECIFIED/23:55::TNIH_2V.35e9ccc3-a187-48f0-906b-b44fbe308a6eZXV/0\",\"firstStreamId\":\"A4ZP7ZC4PI6TO:1.0/2022/07/15/23/DEVICE_UNSPECIFIED/23:55::TNIH_2V.35e9ccc3-a187-48f0-906b-b44fbe308a6eZXV\"}","domainAttributes":null,"domainType":null,"feedbackAttributes":null,"id":"A3FOLKQ9ENDI49#1657927436872#A4ZP7ZC4PI6TO#G0911B0592850CN1","intentType":null,"providerInfoDescription":null,"registeredCustomerId":"A3FOLKQ9ENDI49","sourceActiveUsers":null,"sourceDeviceIds":[{"deviceAccountId":null,"deviceType":"A4ZP7ZC4PI6TO","serialNumber":"G0911B0592850CN1"}],"utteranceId":"A4ZP7ZC4PI6TO:1.0/2022/07/15/23/DEVICE_UNSPECIFIED/23:55::TNIH_2V.35e9ccc3-a187-48f0-906b-b44fbe308a6eZXV","version":1},"description":{"summary":"alexa turn off office light","firstUtteranceId":"A4ZP7ZC4PI6TO:1.0/2022/07/15/23/DEVICE_UNSPECIFIED/23:55::TNIH_2V.35e9ccc3-a187-48f0-906b-b44fbe308a6eZXV/0","firstStreamId":"A4ZP7ZC4PI6TO:1.0/2022/07/15/23/DEVICE_UNSPECIFIED/23:55::TNIH_2V.35e9ccc3-a187-48f0-906b-b44fbe308a6eZXV"},"deviceSerialNumber":"G0911B0592850CN1","name":"Stephen","deviceType":"A4ZP7ZC4PI6TO","deviceAccountId":null,"creationTimestamp":1657927436872,"activityStatus":"SUCCESS","domainAttributes":null,"destinationUserId":"A3FOLKQ9ENDI49"},"_msgid":"049d1cff8aa6a4ab"}

Ok, lets take each section on it's own.
Originating Device leg
In the 'Get name' function node you have:

flow.set("name",msg.payload.name);
return msg;

so that stores msg.payload.name in a flow variable
(why is it called 'Get name'? Wouldn't 'Store name' be better?)
then the ui-text node displays 'msg.payload' which is a big blob of data. One of the items in the payload (payload.data.sourceDeviceIds[0].serialNumber) is the serial number. I would build a object of all your device serial numbers and and the room or what you call the device like this:

{ "G0911B0592850CN1": "bedroom",
  "G0911B0592850CN1": "den",
  "G0911B0592850CN1": "pool"
}

Then in the function node use the serial number to lookup in the object to get the location and move that to msg.payload so the ui-text node can display just that.

Command leg
It looks like you just want to display the command summary which (in the sample data) is payload.description.summary and that is working fine.

Timestame leg
In the 'Get Timestamp' function you have:

var a={payload:msg.payload.creationTimestamp.mydate};
return [a];

You have specify the path as payload.creationTimestamp.mydate and the result is that the out going payload is undefined. The actual path is payload.creationTimestamp as part of the path but it should not be there. You need to corect that path (you could have used a change node for this.

You have an error in Date/Time Format node. The Output Format should not have the double quotes!

Also, many of your function nodes could be replaced with change nodes. For example, the 'Get Command' function node:
Screen Shot 2022-07-15 at 9.55.36 PM
could be a change node like this
Screen Shot 2022-07-15 at 9.56.19 PM

I hope this helps you out.

Actually, it did. Everything is working now. Thanks again.

BTW, not a big deal, but can I format the log string with tabs instead of commas? (The double quotes surround the timestamp because I can import the log file into Excel as a CSV file).

const str = ${devicetime},${devicename},${devicecommand};

Why are you using context? as the values are in the msg flowing through your flow. Also i would probably use the template node here to. Also the ui-dashboard nodes can accept a different property other than payload.
Maybe some thing like this, hope it helps.

[{"id":"b7d14e2a.7bccc8","type":"inject","z":"30af2d3e.d94ea2","name":"test","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":60,"wires":[["73c9435a.bb2bec"]]},{"id":"73c9435a.bb2bec","type":"function","z":"30af2d3e.d94ea2","name":"simulate payload","func":"msg= {\"payload\":{\"data\":{\"_disambiguationId\":null,\"activityStatus\":\"SUCCESS\",\"creationTimestamp\":1657927436872,\"description\":\"{\\\"summary\\\":\\\"alexa turn off office light\\\",\\\"firstUtteranceId\\\":\\\"A4ZP7ZC4PI6TO:1.0/2022/07/15/23/DEVICE_UNSPECIFIED/23:55::TNIH_2V.35e9ccc3-a187-48f0-906b-b44fbe308a6eZXV/0\\\",\\\"firstStreamId\\\":\\\"A4ZP7ZC4PI6TO:1.0/2022/07/15/23/DEVICE_UNSPECIFIED/23:55::TNIH_2V.35e9ccc3-a187-48f0-906b-b44fbe308a6eZXV\\\"}\",\"domainAttributes\":null,\"domainType\":null,\"feedbackAttributes\":null,\"id\":\"A3FOLKQ9ENDI49#1657927436872#A4ZP7ZC4PI6TO#G0911B0592850CN1\",\"intentType\":null,\"providerInfoDescription\":null,\"registeredCustomerId\":\"A3FOLKQ9ENDI49\",\"sourceActiveUsers\":null,\"sourceDeviceIds\":[{\"deviceAccountId\":null,\"deviceType\":\"A4ZP7ZC4PI6TO\",\"serialNumber\":\"G0911B0592850CN1\"}],\"utteranceId\":\"A4ZP7ZC4PI6TO:1.0/2022/07/15/23/DEVICE_UNSPECIFIED/23:55::TNIH_2V.35e9ccc3-a187-48f0-906b-b44fbe308a6eZXV\",\"version\":1},\"description\":{\"summary\":\"alexa turn off office light\",\"firstUtteranceId\":\"A4ZP7ZC4PI6TO:1.0/2022/07/15/23/DEVICE_UNSPECIFIED/23:55::TNIH_2V.35e9ccc3-a187-48f0-906b-b44fbe308a6eZXV/0\",\"firstStreamId\":\"A4ZP7ZC4PI6TO:1.0/2022/07/15/23/DEVICE_UNSPECIFIED/23:55::TNIH_2V.35e9ccc3-a187-48f0-906b-b44fbe308a6eZXV\"},\"deviceSerialNumber\":\"G0911B0592850CN1\",\"name\":\"Stephen\",\"deviceType\":\"A4ZP7ZC4PI6TO\",\"deviceAccountId\":null,\"creationTimestamp\":1657927436872,\"activityStatus\":\"SUCCESS\",\"domainAttributes\":null,\"destinationUserId\":\"A3FOLKQ9ENDI49\"},\"_msgid\":\"049d1cff8aa6a4ab\"}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":60,"wires":[["1717e631.c3fbca"]]},{"id":"1717e631.c3fbca","type":"change","z":"30af2d3e.d94ea2","name":"","rules":[{"t":"set","p":"payload.timestamp","pt":"msg","to":"$moment($$.payload.data.creationTimestamp).format(\"dddd, MMMM Do YYYY, h:mm:ss a\")","tot":"jsonata"},{"t":"set","p":"payload.device","pt":"msg","to":"($devices := {\t\"G0911B0592850CN1\": \"alexa_device_name1\",\t\"G0911B0592850BN2\": \"alexa_device_name2\",\t\"G0911B0592850GM3\": \"alexa_device_name3\"\t};\t$lookup($devices, $$.payload.deviceSerialNumber))","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":120,"wires":[["97e94defad52ded1","d71e4d59e2e1b75e","102b0745cea8db28","c8426b41292c2a67"]]},{"id":"1dcdac75e08018e9","type":"alexa-remote-event","z":"30af2d3e.d94ea2","name":"","account":"4627f780.84a6a8","event":"ws-device-activity","x":100,"y":120,"wires":[["1717e631.c3fbca"]]},{"id":"97e94defad52ded1","type":"delay","z":"30af2d3e.d94ea2","name":"","pauseType":"delay","timeout":"10","timeoutUnits":"milliseconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":240,"y":260,"wires":[["a838a11a.f28058"]],"info":"Allows all data to be flow.saved."},{"id":"d71e4d59e2e1b75e","type":"ui_text","z":"30af2d3e.d94ea2","group":"30106f26816982fe","order":3,"width":0,"height":0,"name":"Timestamp","label":"Tmestamp","format":"{{msg.payload.timestamp}}","layout":"row-spread","className":"","x":760,"y":200,"wires":[]},{"id":"102b0745cea8db28","type":"ui_text","z":"30af2d3e.d94ea2","group":"30106f26816982fe","order":1,"width":0,"height":0,"name":"","label":"Originating Device","format":"{{msg.payload.device}}","layout":"row-spread","className":"","x":710,"y":120,"wires":[]},{"id":"c8426b41292c2a67","type":"ui_text","z":"30af2d3e.d94ea2","group":"30106f26816982fe","order":2,"width":0,"height":0,"name":"Command","label":"Command","format":"{{msg.payload.description.summary}}","layout":"row-spread","className":"","x":740,"y":160,"wires":[]},{"id":"a838a11a.f28058","type":"template","z":"30af2d3e.d94ea2","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{{payload.timestamp}}   {{payload.name}}    {{payload.device}}  {{payload.description.summary}} {{payload.activityStatus}}","output":"str","x":450,"y":260,"wires":[["ca575484.486028","4eefd537527fd7b6"]]},{"id":"ca575484.486028","type":"debug","z":"30af2d3e.d94ea2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":700,"y":300,"wires":[]},{"id":"4eefd537527fd7b6","type":"file","z":"30af2d3e.d94ea2","name":"","filename":"/share/logs/alexa.log","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"none","x":690,"y":260,"wires":[[]]},{"id":"4627f780.84a6a8","type":"alexa-remote-account","name":"","authMethod":"proxy","proxyOwnIp":"192.168.1.57","proxyPort":"3456","cookieFile":"alexa.txt","refreshInterval":"1","alexaServiceHost":"pitangui.amazon.com","amazonPage":"amazon.com","acceptLanguage":"en-US","userAgent":"","useWsMqtt":"on","autoInit":"off"},{"id":"30106f26816982fe","type":"ui_group","name":"Last Command Summary","tab":"cc8daca91241397e","order":2,"disp":true,"width":"6","collapse":false,"className":""},{"id":"cc8daca91241397e","type":"ui_tab","name":"Alexa","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

output

Saturday, July 16th 2022, 12:23:56 am   Stephen    alexa_device_name1  alexa turn off office light SUCCESS

node-red-contrib-alexa-remote2 is unmaintained you would do better changing to
node-red-contrib-alexa-remote2-applestrudel which is maintained and updated 12 hr ago. This node also output device friendly name so you would not need a lookup table.

Thanks. I probably will upgrade to applestruedel. The main reason I haven't is that I have dozens of flows and use remote2 on most of them. It could take me a couple of hours to make the change.

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