How do I create an entity from node-red and read all updates?

I was a reply to Hellis81.

I made a test with a switch and although HA accepts that it will not change state as a switch.
It is most likely possible, but you might need some more data in the function node.
I do just not know which data is needed.

Thanks so far. I will test and if I find a solution I will post it.

Looking forward to it :slight_smile:

There is nothing limiting it from being dynamic.

[{"id":"99837e99b397cd3d","type":"function","z":"ad594fb84fe6d294","name":"","func":"msg.payload = {\n    \"attributes\":{\n        \"aa\":{\n            \"test\":1,\n            \"test2\":2\n        }\n    }\n};\nmsg.state = \"test\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":480,"y":420,"wires":[["3b021b1c81d5e096"]]},{"id":"516f37f70806a164","type":"inject","z":"ad594fb84fe6d294","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":320,"y":420,"wires":[["99837e99b397cd3d"]]},{"id":"3b021b1c81d5e096","type":"ha-entity","z":"ad594fb84fe6d294","name":"test","server":"4bbca37b.1700ec","version":2,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"test"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""},{"property":"state_class","value":""},{"property":"last_reset","value":""}],"state":"state","stateType":"msg","attributes":[],"resend":true,"outputLocation":"payload","outputLocationType":"none","inputOverride":"allow","outputOnStateChange":false,"outputPayload":"$entity().state ? \"on\": \"off\"","outputPayloadType":"jsonata","x":650,"y":380,"wires":[[]]},{"id":"4bbca37b.1700ec","type":"server","name":"Home Assistant","version":2,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30}]

msg.payload = {
    "attributes":{
        "aa":{
            "test":1,
            "test2":2
        }
    }
};
msg.state = "test"
return msg;

The only parts that can’t be dynamic (sadly) is the “Home Assistant Config” part.

1 Like

hmm, I do not know why it looked different the first time I pasted it in, but I see what you are saying now.
Have you tried setting msg.entity_id or msg.name to see if that changes anything?

In my node I set msg.entity_id = switch.test_entity

If this is possible, then it solves the issue with making switches.

It was different, I just took a sequence I had.
Now I made it as dynamic as it can be.

No you can’t have the entity id as dynamic I have spent a lot of time trying.
And it’s not documented either.

1 Like

Well, it was worth a try.
We might not have solved the entire problem, but I learned a new trick for my Node-Red fu, so I am happy.
Thank you :smiley:

My findings:

Rest API creates only read-only entities/states without unique id. Therefore it can’t be used in dashboards to enter / change data.

The entity node can only create switches but with predefined names and not number or select entities.

For me these findings are for the moment very disappointing.
Compared to ioBroker which I use for the moment this is going more then 10 years back.

KO Criteria for HA if I do not find a solution !

You can do it with NR but you’ll need to create the API calls yourself. https://community.home-assistant.io/t/help-merging-2-node-red-flows-in-1/154642/23

mqtt discovery can also work for you.

We already got the sensor up and running with NR, but it seems the poster would like a switch and that I could not figure out to make.

But I found a link to another post from you.

This pallet have been mentioned here, but it looked like the name could not be set dynamically.
In this thread there seems to be a solution to that.

a command line switch.

How do you create them dynamically?

it’s an ha template, I’d assume how you would insert a variable in an automation template. It can also execute python scripts if the template can’t be incorporated into the command.

Thanks for your input. I would like to test but I do have still 2 questions:

  1. Would your solution work for input entities (switch, number, select) as well - or just for sensors?
  2. What do I need to install for testing?

(2) It looks like I have installed everything … so just node-red companion needed.

My findings so far for websocket usage:

  • I can create sensors and switches and both are getting an id.

  • I can update values of both by node-red flow

  • Update switch from dashboard will deactivate the entity - so not working

  • Select or Number entities are not shown in ha even if they seem to get an id. So they can’t be used

I’m still not sure why you need to all this work.
Can’t these devices be integrated in Home Assistant via normal integrations?

Yes I could try to write my own integration. I am using HA now for approx. 1 week to evaluate if I should move from ioBroker to ha.

Since I do have a lot of JS written code, I could easily adapt this to node-red. The only part I am missing is to create the entities from NR/JS. I do understand the two phases: first register / then assign state value. (will be 130-150 entities for my device)

I am not an IT guy - just retired with some time to learn and test. I tried to understand how to build an integration for ha, but compared to ioBroker it seems to be quite complex or I am too stupid to understand. (and learning YAML and Python would be other tasks for me …)

Ok… But I still don’t understand all these switches and sensors you are creating. What are they linked to?
Is it physical devices? is it just virtual?

I don’t think anyone using HA has that many node red created entities. Perhaps there is something you don’t know about Home Assistant and you are using what you know from ioBroker, and that is perhaps not the best method?

I believe this is a xy problem and because you have not given us any background all we can do is help you with the generic answers to the generic questions.
Take a step (or two) back and explain what this is and how it is linked to something.

I want to integrate my heating system into ha. The existing integration does not work for me and since I have written the respective ioBroker adapter I do have the right js-coding and the polling part works already fine in ha and node-red. I tried so far to understand the ha possibilities for integrations of devices if no existing and working integration is available:

  • NR with Rest-API / Websockets do not seem to work.
  • MQTT discovery I do not really want to use to create the entities
  • I could create the respective yaml-file with a node-red flow to define the entities and include this one into configuration.yaml
  • … or I start to write my own integration

I need to decide, but if there is no other solution, I rather prefer the creation of yaml-files as a start.