Update helper values from nodered

Hi

I’d like to update a lot of measurements in HA from node-red using the node-red-contrib-home-assistant-websocket API node using websocket. To keep all config in one place for some items I just use node-red as a data collector, but I am looking for an easy way to get these data to HA, which is some kind of front-end for this node-red code.

Simply as a proof of concept, I created two helpers, with entity ID’s: input_number.gtst test1 and input_number.gtst test2

Now I want to set the values of these items using a function node containning something like:

msg = {
    payload: {
        domain: 'input_number',
        service: 'set_value',
        entity_id: 'input_number.gtst_test2',
        data: {
            type: 'change',
            value: 123.45,
        },
    },
};
return msg;

But this doesn’t work. Can anyone provide my a working example?

I know that I can hard code the entities, but that’s not the way I want to go. Just looking to dynamically assemble messages with entity ID’s and values, which I can fire from a function node one after the other. Of cause, 1 massive update of the HA values would be even more useful. But I can’t see how that’s possible

Thx

This is not a persistent entity in HA.
It will first be available when Node Red updates it.

[{"id":"25ef1c9c74581477","type":"ha-api","z":"be03f9025a1f137a","name":"sensor","server":"541ade28.b4a62","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":"msg.entity_id = `sensor.test_entity`;\n\nmsg.payload = {\n    data: {\n        state: 'test1',\n        attributes: {\n            'name': 'test_entity2',\n            'test1': '111',\n            'test2': '222',\n            friendly_name: 'Test Entity 2',\n            icon: 'mdi:pulse'\n        }\n    }\n};\n\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":630,"y":660,"wires":[["25ef1c9c74581477","fee8ca78ceac8578"]]},{"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":"541ade28.b4a62","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":"","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"numeric","statusMonth":"2-digit","statusDay":"2-digit","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

Setting an already made input_text helper in HA.

[{"id":"d970bc07e48e52aa","type":"function","z":"be03f9025a1f137a","name":"Make msg","func":"msg.entity_id = \"input_text.\" + msg.name + \"_test\"\nmsg.value = \"hej\";\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":2295,"wires":[["04caa331981ed487"]]},{"id":"04caa331981ed487","type":"api-call-service","z":"be03f9025a1f137a","name":"Set input_text","server":"541ade28.b4a62","version":5,"debugenabled":false,"domain":"input_text","service":"set_value","areaId":[],"deviceId":[],"entityId":[],"data":"{\"entity_id\": msg.entity_id, \"value\": msg.value}","dataType":"jsonata","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":620,"y":2295,"wires":[[]]},{"id":"1367e483e1cbbab2","type":"inject","z":"be03f9025a1f137a","name":"","props":[{"p":"name","v":"something","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":280,"y":2295,"wires":[["d970bc07e48e52aa"]]},{"id":"541ade28.b4a62","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":false,"cacheJson":false,"heartbeat":false,"heartbeatInterval":"","areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"numeric","statusMonth":"2-digit","statusDay":"2-digit","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]
1 Like

Hi Wally

Thx, your info was the key.

As an example below a test of how I developed my configuration. The first function node is an example of data being collected in a dictionary. The second function node then contains on top some config to connect the data in the dictionary to the according HA tags and then a loop fires change value messages one after the other to HA.

[{"id":"fed93ee95d2f1bbc","type":"function","z":"8684c94f.6f8c6","name":"Prep HASS","func":"//**************************************************\n// tags\nvar t = {\n    d1: {\n        value1: { d: 'input_number', s: 'set_value', id: 'gtst_test1' },\n        value2: { d: 'input_text', s: 'set_value', id: 'gtst_test3' },\n    },\n    d2: {\n        value1: { d: 'input_number', s: 'set_value', id: 'gtst_test2' },\n        value2: { d: 'input_text', s: 'set_value', id: 'gtst_test4' },\n    },\n}\n//**************************************************\n// output HASS data one item after the other\nvar pl = msg.payload.data;\nfor (let key in pl) {\n    let group = pl[key];\n    for (let gkey in group) {\n        let msgout = {\n            payload: {\n                domain  : t[key][gkey].d,\n                service : t[key][gkey].s,\n                target  : {\n                    entity_id: t[key][gkey].d + '.' + t[key][gkey].id,\n                },\n                data    : {\n                    value : group[gkey],\n                },\n            },\n        };\n        node.send(msgout);\n    };\n};\nreturn null;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1230,"y":720,"wires":[["b814a5cf602856ba"]],"icon":"font-awesome/fa-cogs"},{"id":"3e1e77f9626f8b59","type":"function","z":"8684c94f.6f8c6","name":"Collect Data","func":"//**************************************************\n// measurements 1\nvar d1 = {\n    value1: 72.3,\n    value2: 'value 23.7°C',\n}\n//**************************************************\n// measurements 2\nvar d2 = {\n    value1: 61.2,\n    value2: 'value 34.8°C',\n}\n//**************************************************\n// package message\nvar msgout = {\n    payload: {\n        data: {\n            d1: d1,\n            d2: d2,\n        },\n    },\n};\n//\nreturn msgout;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1050,"y":720,"wires":[["fed93ee95d2f1bbc"]],"icon":"node-red/status.svg"},{"id":"b814a5cf602856ba","type":"api-call-service","z":"8684c94f.6f8c6","name":"set","server":"d359d15ddc344f8c","version":5,"debugenabled":false,"domain":"homeassistant","service":"check_config","areaId":[],"deviceId":[],"entityId":[],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"all","x":1390,"y":720,"wires":[["b0b05af47e243562"]]},{"id":"b0b05af47e243562","type":"debug","z":"8684c94f.6f8c6","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1530,"y":720,"wires":[]},{"id":"ceb0ded25d97733e","type":"inject","z":"8684c94f.6f8c6","name":"","props":[],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":890,"y":720,"wires":[["3e1e77f9626f8b59"]]},{"id":"d359d15ddc344f8c","type":"server","name":"HASS_Athome","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}]

Some other question:
If I want to provide eg room data from NR to HA, then I need to create a lot of helpers in HA:

  • room1_temp
  • room1_humidity
  • room1_lightintensity
  • room2_temp
  • room2_humidity
  • room2_lightintensity
  • roomX_…

Is there a way to create a custom sensor “roomsensor”

  • temp
  • humidity
  • lightintensity
    that can be instantiated as room1sensor, room2sensor, roomXsensor …