Quick help with Alexa and Node-RED

I’m trying to create an Node-RED automation where Alexa replies back to the last called device. I’m using a get entities where ‘last_called’ is true, output as an array to msg.alexa. Then in the next node, I’m calling the ‘alexa_media’ service with the data below:

{
    "message": "Please close the following doors and windows. {{payload}}.  Good night.",
    "data": {
        "type": "tts"
    },
    "target": [
        "{{alexa[0].entity_id}}"
    ]
}

When I dedug the node, it shows the target as blank. What am I missing here?

@flyize here you go, give this a try.

[{"id":"80f259f8.d48e58","type":"inject","z":"4f853fd0.1ea12","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":2600.892749786377,"y":1160.7145805358887,"wires":[["c60b9fb7.96e0a"]]},{"id":"1ec13a69.bd52a6","type":"debug","z":"4f853fd0.1ea12","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":3513.9284744262695,"y":1093.0357999801636,"wires":[]},{"id":"48c5b9ce.045e28","type":"debug","z":"4f853fd0.1ea12","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":3871.607258796692,"y":1155.8928337097168,"wires":[]},{"id":"cb1daaec.101868","type":"api-call-service","z":"4f853fd0.1ea12","name":"Alexa - Alarm deactivated (Rnd)","server":"8a3231e1.3f0e1","version":1,"service_domain":"notify","service":"alexa_media","entityId":"","data":"","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":true,"x":3605.3572540283203,"y":1155.8928327560425,"wires":[["48c5b9ce.045e28"]]},{"id":"c60b9fb7.96e0a","type":"change","z":"4f853fd0.1ea12","name":"Set Alexa Attributes","rules":[{"t":"set","p":"volume","pt":"flow","to":"0.4","tot":"str"},{"t":"set","p":"target","pt":"flow","to":"media_player.office_echo_dot","tot":"str"},{"t":"set","p":"message","pt":"flow","to":"Test of Alexa","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":2822.8572387695312,"y":1158.3928337097168,"wires":[["d63ff.9807ec018","db082c18.6fb7"]]},{"id":"d63ff.9807ec018","type":"function","z":"4f853fd0.1ea12","name":"Create Alexa Alert","func":"\nvar target = flow.get('target')\nvar message = flow.get('message')\n\nvar payload = \n{\n \"data\":\n {\n \"target\": `${target}`,\n \"message\": `${message}`\n }\n}\nmsg.payload = payload\nmsg.topic = `${target}`\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","x":3054.107192993164,"y":1158.3928184509277,"wires":[["c96d9564.ab25d8"]]},{"id":"db082c18.6fb7","type":"debug","z":"4f853fd0.1ea12","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":3018.5714797973633,"y":1234.8214235305786,"wires":[]},{"id":"c96d9564.ab25d8","type":"template","z":"4f853fd0.1ea12","name":"Alexa device - Template","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\n\"data\": \n {\n \"message\": \"{{ payload.data.message}}\",\n \"target\": \"{{ payload.data.target }}\",\n \"data\": {\n \"type\": \"announce\"\n }\n }\n} ","output":"json","x":3302.857246398926,"y":1157.1428327560425,"wires":[["cb1daaec.101868","1ec13a69.bd52a6"]]},{"id":"8a3231e1.3f0e1","type":"server","name":"Hassio","legacy":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false}]

Sorry, I should have also posted an example. This is probably a bit easier.

[{"id":"cb57ab4f.27a7b8","type":"api-call-service","z":"2de86a51.3fd3c6","name":"Notify Alexa","server":"61a3b09d.8395a","version":1,"debugenabled":true,"service_domain":"notify","service":"alexa_media","entityId":"","data":"{\"message\":\"Please close the following doors and windows. {{payload}}.  Good night.\",\"data\":{\"type\":\"tts\"},\"target\":[\"{{alexa[0].entity_id}}\"]}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":1000,"y":740,"wires":[[]]},{"id":"ef2f5b30.15b3b8","type":"ha-get-entities","z":"2de86a51.3fd3c6","server":"61a3b09d.8395a","name":"Get last Alexa","rules":[{"property":"entity_id","logic":"includes","value":"media_player.living_room, media_player.master_bedroom, media_player.office","valueType":"str"},{"property":"attributes.last_called","logic":"is","value":"true","valueType":"bool"}],"output_type":"array","output_empty_results":false,"output_location_type":"msg","output_location":"alexa","output_results_count":1,"x":860,"y":660,"wires":[["cb57ab4f.27a7b8"]]},{"id":"61a3b09d.8395a","type":"server","name":"Home Assistant","addon":true}]

I realize this doesn’t answer your question but, in your ‘Get last Alexa’ node, only the first entry for your entity_id is being recognized based on my testing (provided the device has been played of course…), i.e. even if the second entity_id in your list of entity_id’s is the last to have been played it is not being evaluated.

Are you sure that both entity_id can be evaluated in the Property entry ? In the meantime I’ll keep looking into it (its a good question!)

It appears to be working for me. Here’s the result with a debug node added.

Is ‘media_player.office’ the second entity_id entry in the Property -> entity_id list ?

No, its the third.

@flyize Ok so I am still not able to get any of my Alexa devices other than the first device in the entity list to register a response in the debug node, even after speaking into each device sequentially, so you are well ahead of the game…

This works for me with the first entity_id entry device in the list.

[{"id":"8ebcfe5d.4aa51","type":"debug","z":"25063fae.4f85f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2090.357208251953,"y":1483.2143783569336,"wires":[]},{"id":"ef2f5b30.15b3b8","type":"ha-get-entities","z":"25063fae.4f85f","server":"118d105f.a269","name":"Get last Alexa","rules":[{"property":"entity_id","logic":"includes","value":"media_player.juan_s_2nd_echo_dot, media_player.office_echo_dot","valueType":"str"},{"property":"attributes.last_called","logic":"is","value":"true","valueType":"bool"}],"output_type":"array","output_empty_results":false,"output_location_type":"msg","output_location":"alexa","output_results_count":1,"x":1884.2857666015625,"y":1557.1429443359375,"wires":[["8ebcfe5d.4aa51","d774b179.a9bed"]]},{"id":"1adc1d55.8b93c3","type":"inject","z":"25063fae.4f85f","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":1702.678611755371,"y":1558.3928451538086,"wires":[["ef2f5b30.15b3b8"]]},{"id":"d774b179.a9bed","type":"api-call-service","z":"25063fae.4f85f","name":"Notify Alexa","server":"8a3231e1.3f0e1","version":1,"service_domain":"notify","service":"alexa_media","entityId":"","data":"{\"message\":\"Please close the following doors and windows. {{alexa.0.entity_id}}.  Good night.\",\"data\":{\"type\":\"tts\"},\"target\":\"{{alexa.0.entity_id}}\"}","dataType":"json","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":2118.7500343322754,"y":1555.0000228881836,"wires":[["ffd373ad.717b6"]]},{"id":"ffd373ad.717b6","type":"debug","z":"25063fae.4f85f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":2291.2500381469727,"y":1555.0000228881836,"wires":[]},{"id":"118d105f.a269","type":"server","name":"Home Assistant","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false},{"id":"8a3231e1.3f0e1","type":"server","name":"Hassio","legacy":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":false}]

@flyize was able to get all my Echo’s to be evaluated by attribute ‘last_called’ by setting the parameters (with arrows).

Not sure if you missed it, but I was using IN and not IS in my node. If you didn’t import and use the one I posted, it would be easy to miss.

image

edit: I just looked back at yours and you were also using IN. Never mind!

This question still stands. Anyone know how to make the last Alexa to hear something speak back to me?

I wrote up some alexa node red instructions here

About a 1/3 of the way into the page, there is a section on Alexa remote 2.
With that palette, I can figure out which alexa device was talked to, and then I issue room specific commands based on device location. I get the device serial number, then route based on the serial number.
Somewhat brute force as you have to switch based on serial numbers, but it will let you send a voice message to the alexa you were talking to.