Sending a curl request and parse the the output of that request

hello!

I'm currently trying to do this in HA but it does not seem to support this (short of an integration).

Here's what I like to do. I have another machine that does AI including image generation based on prompts. It works via curl request. I originally had it set up as a rest sensor in Home Asissistant (see below), that sends the request to the LocalAI machine, and returns a value for the sensor, being where the image URL is located. I then point an image card to that sensor and it shows the picture on my HA dashboard BUT I later learned that it was not actually passing through the template sensor values. For example, below in the "prompt" field, there are few sensors that show this template {{ states('sensor.humidex_perception') }}, which is basically a string value for the sensor state. This should become part of the prompt as a string when it reaches the LocalAI machine

#LocalAI Image Generations
- platform: rest
  name: "Image URL Sensor"
  unique_id: [id]
  resource: http://192.168.1.195:8080/v1/images/generations
  method: POST
  scan_interval: 43200
  timeout: 120 
  headers:
    Content-Type: application/json
  payload: > 
    {
    "prompt": "magnificent scenery, wide landscape, sharp and crisp background, very beautiful landscape, fantasy, birdview, best quality, masterpiece, ultra high res, dark blue light, photo, photorealistic, wide view, kkw-ph1, professional shot of house, {{ state_attr('weather.forecast_home', 'forecast')[0].condition }} day, humidity perception is {{ states('sensor.humidex_perception') }}, thermal perception is {{ states('sensor.thermalperception') }}, extreme low-angle shot, realistic photography, ultra detailedmagnificent scenery, wide landscape, sharp and crisp background, very beautiful landscape, old ruins buildings, fantasy, birdview, best quality, masterpiece, ultra high res, dark blue light, cloudy, photo, photorealistic, wide viewl",
    "size": "512x512",
    "model": "landscape-photoreal"
    }
  value_template: "{{ value_json.data[0].url }}"

it was passing all those template sensors as templates.

Is this something that I can solve in NodeRed?

thank you!

You should be able to turn the curl request into a request node.

thanks, but where do I put the payload?

and to parse the responce, its takes upto 60 seconds to send it the reply, does it wait for it or is there a time out?

Cheers

ok, I think I got it by using the inject node and json format, like this

image

where the inject node has this json

{
    "prompt": "A vast cityscape stretches out before you, its towering skyscrapers piercing the clouds. Neon lights illuminate streets, creating a vibrant and dynamic atmosphere. Pedestrians hurry along the sidewalks. The city is alive with energy, the weather is {{ state_attr('weather.forecast_home', 'forecast')[0].condition }} ",
    "size": "512x512",
    "model": "landscape-photoreal"
}

is this the correct way to pass entity state in the payload? i.e.

{{ state_attr('weather.forecast_home', 'forecast')[0].condition }}

thanks

I think you have 1 too many outer brackets but otherwise yes, that's it.

I think the OP was using HA nodes, which allow Mustache syntax.
@Anto79-ops I think you will have to show where these values come from and what message property they are in, you can then use a template node to create the payload object, or a function node, or a change node using JSONata.

thanks!

these values come from the state or attribute of the sensor (entity).

I suspect I would have to use current state nodes (for each entitiy) to get the values, and then somehow combine them into the prompt field of the payload.

Yes, do that then move them to a message property so you can later add them into the payload object. added debug nodes and show us the values and what property they are in.

Then in a template node do

{
    "prompt": "A vast cityscape stretches out before you, its towering skyscrapers piercing the clouds. Neon lights illuminate streets, creating a vibrant and dynamic atmosphere. Pedestrians hurry along the sidewalks. The city is alive with energy, the weather is {{ sproerty_path }}, {{property_path}}",
    "size": "512x512",
    "model": "landscape-photoreal"
}

Read the template node sidebar help text for more info. You set the template node to output a prased json object.

Getting property paths

There’s a great page in the docs (Working with messages : Node-RED) that will explain how to use the debug panel to find the right path/value to any data item.

Pay particular attention to the part about the buttons that appear under your mouse pointer when you over hover a debug message property in the sidebar.

BX00Cy7yHi

ahh, ok I'm trying to understand here, for the your first request

and code

json
[
    {
        "id": "2f2bf929e62df7e3",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "531a8ede1ea6f42e",
        "type": "api-current-state",
        "z": "2f2bf929e62df7e3",
        "name": "Weather Forcast",
        "server": "505255aec0a0bb5e",
        "version": 3,
        "outputs": 1,
        "halt_if": "",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "entity_id": "weather.forecast_home",
        "state_type": "str",
        "blockInputOverrides": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "entity"
            }
        ],
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "override_topic": false,
        "state_location": "payload",
        "override_payload": "msg",
        "entity_location": "data",
        "override_data": "msg",
        "x": 560,
        "y": 160,
        "wires": [
            [
                "382a1b3e9ddf9586"
            ]
        ]
    },
    {
        "id": "c885b3997b7a27e5",
        "type": "api-current-state",
        "z": "2f2bf929e62df7e3",
        "name": "Humidex Perception",
        "server": "505255aec0a0bb5e",
        "version": 3,
        "outputs": 1,
        "halt_if": "",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "entity_id": "sensor.humidex_perception",
        "state_type": "str",
        "blockInputOverrides": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "entity"
            }
        ],
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "override_topic": false,
        "state_location": "payload",
        "override_payload": "msg",
        "entity_location": "data",
        "override_data": "msg",
        "x": 580,
        "y": 220,
        "wires": [
            [
                "9c15f10bba9e4988"
            ]
        ]
    },
    {
        "id": "46dd8e28bf5da6f9",
        "type": "api-current-state",
        "z": "2f2bf929e62df7e3",
        "name": "Thermal Perception",
        "server": "505255aec0a0bb5e",
        "version": 3,
        "outputs": 1,
        "halt_if": "",
        "halt_if_type": "str",
        "halt_if_compare": "is",
        "entity_id": "sensor.thermalperception",
        "state_type": "str",
        "blockInputOverrides": false,
        "outputProperties": [
            {
                "property": "payload",
                "propertyType": "msg",
                "value": "",
                "valueType": "entityState"
            },
            {
                "property": "data",
                "propertyType": "msg",
                "value": "",
                "valueType": "entity"
            }
        ],
        "for": "0",
        "forType": "num",
        "forUnits": "minutes",
        "override_topic": false,
        "state_location": "payload",
        "override_payload": "msg",
        "entity_location": "data",
        "override_data": "msg",
        "x": 570,
        "y": 280,
        "wires": [
            [
                "164f33cfbef7a0bd"
            ]
        ]
    },
    {
        "id": "382a1b3e9ddf9586",
        "type": "debug",
        "z": "2f2bf929e62df7e3",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 880,
        "y": 160,
        "wires": []
    },
    {
        "id": "9c15f10bba9e4988",
        "type": "debug",
        "z": "2f2bf929e62df7e3",
        "name": "debug 2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 880,
        "y": 220,
        "wires": []
    },
    {
        "id": "164f33cfbef7a0bd",
        "type": "debug",
        "z": "2f2bf929e62df7e3",
        "name": "debug 3",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 880,
        "y": 300,
        "wires": []
    },
    {
        "id": "95dd927b7e09eacd",
        "type": "inject",
        "z": "2f2bf929e62df7e3",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 240,
        "y": 160,
        "wires": [
            [
                "531a8ede1ea6f42e",
                "c885b3997b7a27e5",
                "46dd8e28bf5da6f9"
            ]
        ]
    },
    {
        "id": "505255aec0a0bb5e",
        "type": "server",
        "name": "Home Assistant",
        "version": 5,
        "addon": false,
        "rejectUnauthorizedCerts": true,
        "ha_boolean": "y|yes|true|on|home|open",
        "connectionDelay": true,
        "cacheJson": true,
        "heartbeat": false,
        "heartbeatInterval": "30",
        "areaSelector": "friendlyName",
        "deviceSelector": "friendlyName",
        "entitySelector": "friendlyName",
        "statusSeparator": ": ",
        "statusYear": "hidden",
        "statusMonth": "short",
        "statusDay": "numeric",
        "statusHourCycle": "default",
        "statusTimeFormat": "h:m",
        "enableGlobalContextStore": false
    }
]

as is, path is "payload" but I guess I should change that for each entity

Sorry, what do I do next?

Here is an example, I do not use HA, so have simulated output on payload.

[{"id":"e26cb5685471371d","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":360,"y":5060,"wires":[["6dd310fbec4047cf","15da924b5da7de6c","3ee3d4b6778c8658"]]},{"id":"6dd310fbec4047cf","type":"change","z":"d1395164b4eec73e","name":"simulate HA node","rules":[{"t":"set","p":"payload","pt":"msg","to":"vakue 1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":5020,"wires":[["d4ec411aac0d5bfc"]]},{"id":"15da924b5da7de6c","type":"change","z":"d1395164b4eec73e","name":"simulate HA node","rules":[{"t":"set","p":"payload","pt":"msg","to":"vakue 2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":5060,"wires":[["0db39c3196a3666e"]]},{"id":"3ee3d4b6778c8658","type":"change","z":"d1395164b4eec73e","name":"simulate HA node","rules":[{"t":"set","p":"payload","pt":"msg","to":"vakue 3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":5100,"wires":[["95db309414a6b180"]]},{"id":"d4ec411aac0d5bfc","type":"change","z":"d1395164b4eec73e","name":"set topic","rules":[{"t":"set","p":"topic","pt":"msg","to":"name1","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":5020,"wires":[["5603fed743446324"]]},{"id":"0db39c3196a3666e","type":"change","z":"d1395164b4eec73e","name":"set topic","rules":[{"t":"set","p":"topic","pt":"msg","to":"name2","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":5060,"wires":[["5603fed743446324"]]},{"id":"95db309414a6b180","type":"change","z":"d1395164b4eec73e","name":"set topic","rules":[{"t":"set","p":"topic","pt":"msg","to":"name3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":740,"y":5100,"wires":[["5603fed743446324"]]},{"id":"5603fed743446324","type":"join","z":"d1395164b4eec73e","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"10","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":870,"y":5060,"wires":[["24ba1bc44d5f8018"]]},{"id":"24ba1bc44d5f8018","type":"template","z":"d1395164b4eec73e","name":"","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"{\n    \"prompt\": \"A vast cityscape stretches out before you, its towering skyscrapers piercing the clouds. Neon lights illuminate streets, creating a vibrant and dynamic atmosphere. Pedestrians hurry along the sidewalks. The city is alive with energy, the weather is {{payload.name1}}, {{payload.name2}}, {{payload.name3}}\",\n    \"size\": \"512x512\",\n    \"model\": \"landscape-photoreal\"\n}","output":"json","x":960,"y":5100,"wires":[["10328c3bb1848212"]]},{"id":"10328c3bb1848212","type":"debug","z":"d1395164b4eec73e","name":"debug 2471","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1130,"y":5100,"wires":[]}]

How to import/export a flow

p.s. From the look of your image these values are a result of some weather api in HA, there are weather nodes that can call for the data directly into node-red.

thank you! this working!

the ouput of this is

image

the path for the image URL is

payload.data[0].url

now, if I wanted to create a sensor in HA with the state being the path of the URL, how do I specify the path in the sensor node, here?

image

Sorry I do not use HA, some here do, or ask the question in the HA forum they have a node-red category.
But at a guess enter that path where you have state msg. payload, possiible replace payload with the path to url.

thanks I just set the state to this:

msg.payload.data[0].url

Its working exactly they way I wanted it to!

I just need to figure a way to trigger this in Home Assisstant...guess its some sort of entity or service, but i'll check in the HA forums

thanks so much!

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