Need help downloading/saving from an image to Home Assistant

Hello, everyone! I am experimenting with DALLE-2 and Home Assistant and being able to generate a prompt through Node-RED.

I have a flow currently working to the point of a debug node reading a payload of "image_url" with the link. I am extracting this from "payload.data[0].url" from the API call to OpenAI. This is my first time doing anything quite like this in Node-RED.

What I am struggling with is how I can take the URL in that payload and save the image to /local/images/dalle2-output.jpg

Here is a sample output from the last debug of my flow:

msg.image_url : string[476]
"https://oaidalleapiprodscus.blob.core.windows.net/private/org-f1Nmwrij1YbgtY3Z3xDaAB3t/user-QuN2BD7Mys7OXmpMsDvKlF9j/img-4nGUXgyEFpItc2CQkqwHoEKK.png?st=2023-08-13T13%3A50%3A22Z&se=2023-08-13T15%3A50%3A22Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-08-12T17%3A31%3A47Z&ske=2023-08-13T17%3A31%3A47Z&sks=b&skv=2021-08-06&sig=7wxC44KDiEnF1Fly1LsELPLWs6eZ5/HU9%2BhL%2BOSkyt0%3D"

How can I save the image from that URL? Thank you so much, in advance!

Cannot help with HA , best ask on their forum, or wait for another member that uses HA.

To download file use a http request node with that url. You would set the request node to output a binary buffer, then you can save the buffer to file (file write node) or send it somewhere else.

This is still helpful, thank you! This may get me one step closer. With the HTTP Request node, what would I use in the URL field for it to use the image_url payload? I added a couple of variations of what I thought may work, but the node just states "requesting" and doesn't seem to move on.

Sending a get request to the url you provided returns

<?xml version="1.0" encoding="utf-8"?><Error><Code>AuthenticationFailed</Code><Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:ba2130cd-301e-006c-4c00-ce6aa3000000
Time:2023-08-13T16:09:50.5638779Z</Message><AuthenticationErrorDetail>Signature not valid in the specified time frame: Start [Sun, 13 Aug 2023 13:50:22 GMT] - Expiry [Sun, 13 Aug 2023 15:50:22 GMT] - Current [Sun, 13 Aug 2023 16:09:50 GMT]</AuthenticationErrorDetail></Error>

So you need the authorization headers set. You will need to read the docs from the place you are downloading the image from, on how/what the headers should be set and what key/signiture to use.

Headers can be set in msg.headers or in the request node directly.

I added the header, I believe. Now, the HTTP Request node's debug is showing:

msg : string[16]

"No url specified"

So it would seem it's not getting that image_url payload. I am not sure, as I said, I have never done anything quite like this.

Thank you very much for trying. I may post this over on their forums for some further help.

I can not help as you provide no information, maybe you would provide an example flow you have tried.
Url can be set using msg.url or direct in the http request node.

I was hesitant to share a flow because I didn't want to accidentally share my API information from OpenAI, since there's a charge for that.

Changing to msg.url got me further! Now I see output like this in the HTTP Request debug:

<?xml version="1.0" encoding="utf-8"?><Error><Code>InvalidAuthenticationInfo</Code><Message>Authentication information is not given in the correct format. Check the value of Authorization header.
RequestId:1635a1dc-301e-0001-7d0c-cec08d000000
Time:2023-08-13T17:36:26.9722906Z</Message></Error>

That's a change from where I was. I think I am close, and followed what I could from the documentation. Here's a snippet of the flow:

[{"id":"5c22f2acf1af8e7c","type":"function","z":"cc3fb85baf747c6f","name":"images/generations","func":"msg.api = 'images/generations';\nmsg.params = {\n    \"prompt\": msg.dalle_prompt,\n    \"n\": 1,\n    \"size\": \"1024x1024\"\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":970,"y":4020,"wires":[["6a11733d2d2f312b"]]},{"id":"6a11733d2d2f312b","type":"openai","z":"cc3fb85baf747c6f","name":"OpenAI","api":"","creds":"4ac87e2c9f74c224","x":1140,"y":4020,"wires":[["9aafac240e6c3656","35c120ca22b9e3bf"]]},{"id":"35c120ca22b9e3bf","type":"change","z":"cc3fb85baf747c6f","name":"Set Payload","rules":[{"t":"set","p":"url","pt":"msg","to":"payload.data[0].url","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":1370,"y":4020,"wires":[["a9d38bb96d32d114","6ac2e3d8677658fa"]]},{"id":"6ac2e3d8677658fa","type":"http request","z":"cc3fb85baf747c6f","name":"","method":"GET","ret":"bin","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[{"keyType":"other","keyValue":"Authorization","valueType":"other","valueValue":"Bearer API_KEY_HERE"}],"credentials":{},"x":1670,"y":4060,"wires":[["f1586b2a2caeefe3"]]},{"id":"f1586b2a2caeefe3","type":"debug","z":"cc3fb85baf747c6f","name":"debug 247","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1890,"y":3960,"wires":[]},{"id":"4ac87e2c9f74c224","type":"openaiApiKey","name":"OpenAI"}]

I'm another step closer. I appreciate it!

The error suggests the wrong auth key or wrong format.

To be sure that you are not missing any data, what is the output of openai node, add a debug set to complete message object, there may be other info.

Here's the full output of the OpenAI node:

object
_msgid: "d91c563377c8147d"
payload: object
created: 1691955877
data: array[1]
0: object
url: "https://oaidalleapiprodscus.blob.core.windows.net/private/org-f1Nmwrij1YbgtY3Z3xDaAB3t/user-QuN2BD7Mys7OXmpMsDvKlF9j/img-FS8NpXj3U6JLrEwS48Oz5LqX.png?st=2023-08-13T18%3A44%3A37Z&se=2023-08-13T20%3A44%3A37Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-08-13T07%3A12%3A31Z&ske=2023-08-14T07%3A12%3A31Z&sks=b&skv=2021-08-06&sig=rnNrAjH7/hOFJzQ1zMvBzS6YM2Hzqt6K5RbNLif8Yro%3D"
topic: ""
dalle_prompt: "summer day in August with Mostly sunny and hot; a thunderstorm in the area late this afternoon - Pixel Art"
data: object
entity_id: "input_text.dalle_prompt"
state: "summer day in August with Mostly sunny and hot; a thunderstorm in the area late this afternoon - Pixel Art"
attributes: object
editable: true
min: 0
max: 9999
pattern: null
mode: "text"
icon: "si:openai"
friendly_name: "DALLE-2 Prompt"
context: object
id: "01H7QWSDBCPGJVX8ND76G8ZMG1"
parent_id: null
user_id: null
last_changed: "2023-08-13T16:48:37.740Z"
last_updated: "2023-08-13T16:48:37.740Z"
timeSinceChangedMs: 10551677
api: "images/generations"
params: object
prompt: "summer day in August with Mostly sunny and hot; a thunderstorm in the area late this afternoon - Pixel Art"
n: 1
size: "1024x1024"

[edit] scratch that the buffer was not an image

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