Node Red, Alexa, Device Aware - Variables?

Hi, in an attempt to make alexa respond from the device that was spoken to I used a function node to pass only the messages for which msg.payload.alexaResponse==“OK” and then used this to set a variable msg.whichalexa to msg.payload.deviceSerialNumber

You can then set the “device” under the Alexa Routine which is used to be the varaible msg.whichalexa.

This is fine until your messages get overridden etc with more complex automations.

So, I need a way to store the variable “whichalexa” a bit more persistently than in the message. I used flow.whichalexa and this works fine, except there seems to be some kind of bug with the Alexa Routine Node in that the contents of the Devices box seem to get cleared after you press “Done” and when node red restarts etc the automation stops working because the Device box is empty.

It seems that anything flow. or global. seems to get emptied as soon as you press “Done”

So, I am wondering if there is somewhere else I can store this data? If “env variable” is chosen it doesnt seem get cleared, but I can’t figure out how to set a env variable using a function?

Does anyone know how to set a env variable using a function?
Or another way to do what I am doing?

Afaik the Homeassistant Alexa Media Player integration has a sensor called last Alexa that does what you want already.

Otherwise you can also save a global variable as a file (even resists a restart)

var newmsg = {}
global.set("alarm", 1, "storeInFile");
newmsg.payload = "True";

return newmsg;

Thank for your reply, however the last_used sensor is not reliable, sometimes it updates sometimes it doesn’t.

The problem with setting a global variable is that the Alexa routine node doesn’t retain the contents of the device field if you put a global or flow variable, it only seems to persist if you put a msg. Or “env variable” but I can’t figure out how to set a “env variable” with a function or change?

env variables have to be hard coded in settings.js. Flow and Global variables can be set through nodes. See for how to use and set context in nodered

https://nodered.org/docs/user-guide/context

Ive now realised there is a service available which forces the update of last_alexa so i will just introduce this into the sub flow.

Hi @stuengland - It’s an old thread, but I’d like to realize the same function. Would you mind to elaborate a bit more how you achieved to get the device id and used it further in the flow? Thx! TG