Call webservice and map the result to multiple entities

Hi there,
I have a design issue I try to solve smarter if possible.
So basically I have an external display driven by openhasp and I want to display a result from an API on it. The result are a few sentences to be put on the display. As this display does not have text boxes (at least I am unaware of it) I have to split the result in “lines” and put them individually on the display. I solved this by having 8 entities with text_input which will then be filled. This is more than needed, usually only 3-4 items are used, dependend on the result every day delivers.
Anyhow,
I use node red, call the service and receive the result as a json, this is then passed back and split in a function into the individual lines.
I am unable to return a array and map the single values to the entities, so I mapped the values to global objects (global.set(“myLine1”, line1) etc.
Then in the next node, I use 8 mappings, read what is in the global object “myLine1” and pass it as payload to an action node. This happens 8 times in parallel. It works but it is not nice - is there a smarter solution?

Potentially outside of NodeRed? If possible want to stick to it as I like to stick to one environment.
Below my flow for reference.

Thanks in advance!
S.

Sounds like a Restful Integration solution outside of NR:

…and providing sensors with text in their states instead of input_text entities.

Without more specifics of the data and its processing, can’t really say any more than that.

1 Like

I can post a couple of examples of how I do things, but it will first be later (4-6 hours.)

You also have the ability to create entities in NR. Using a sensor entity node in lieu of a text entity, you can store each line in an attribute of a sensor.

Hi, I will look into this. However this is not in Node-Red

that would be highly appreciated

Here is an example that will make 3 sensors dynamically in HA.
They are defined in the function node.
The function node will send 3 messages to the API node that will create a sensor in HA.
The last of the messages will be with a value picked from the copy of the state machine store in the NR context data folder (the cog in the icon list in the upper right corner and remember to refresh the globals section to actually see the values).
A thing that is important to remember with these sensors is that they are not persistent, so after a restart of HA they do not exist until NR have run the flow. They will be unknown or unavailable until then.
This means you might get some errors in the NR and HA logs if you base stuff, like templates or automations on these sensors and Spook will complain about it. All will however work when the flow creates the sensors and Spook accepts it also and removes the warning.

[{"id":"25ef1c9c74581477","type":"ha-api","z":"be03f9025a1f137a","name":"sensor","server":"","version":1,"debugenabled":false,"protocol":"http","method":"post","path":"/api/states/{{entity_id}}","data":"","dataType":"json","responseType":"json","outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"results"}],"x":800,"y":660,"wires":[[]]},{"id":"06ac8e03a8a85876","type":"function","z":"be03f9025a1f137a","name":"make final payload","func":"let msg1 = {};\nlet msg2 = {};\nlet msg3 = {};\n\nmsg1.entity_id = `sensor.test1`;\nmsg1.payload = {\n    data: {\n        state: 'test1',\n        attributes: {\n            'name': 'name1',\n            'test1': 'test',\n            'test2': 'test',\n            friendly_name: 'Test Entity 1',\n            icon: 'mdi:pulse'\n        }\n    }\n};\nnode.send(msg1);\n\nmsg2.entity_id = `sensor.test1`;\nmsg2.payload = {\n    data: {\n        state: 'test2',\n        attributes: {\n            'name': 'name2',\n            'test1': 'test',\n            'test2': 'test',\n            friendly_name: 'Test Entity 2',\n            icon: 'mdi:pulse'\n        }\n    }\n};\nnode.send(msg2);\n\nmsg3.entity_id = `binary_sensor.is_nodered_running`;\nmsg3.payload = {\n    data: {\n        state: global.get('homeassistant').homeAssistant.states[\"binary_sensor.node_red_running\"].state,\n        attributes: {\n            'name': 'name3',\n            'test1': 'test',\n            'test2': 'test',\n            friendly_name: 'Test Entity 3',\n            icon: 'mdi:pulse'\n        }\n    }\n};\nreturn msg3;\n\n","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":660,"wires":[["25ef1c9c74581477","fee8ca78ceac8578","b84f7742bdb0e268"]]},{"id":"39963ed0628d85b4","type":"inject","z":"be03f9025a1f137a","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":450,"y":660,"wires":[["06ac8e03a8a85876"]]},{"id":"08d7fed91f66a201","type":"global-config","env":[],"modules":{"node-red-contrib-home-assistant-websocket":"0.80.3"}}]

Import the flow and set your HA server up for it.
Then run it and see the result in the HA developer tools.
The sensors that the flow will create will be removed after a HA restart, so no long term impact of the test.
But check that you do not already have sensors named what is used in the function node. :slight_smile:

1 Like