Passing Object From Node-Red to HA, and Using It To Create & Manage Sensors

I’m having lots of issues passing data from Node-Red to Home Assistant. I’ve tried various Nodes and have not been able to get some of them working at all, and others have major issues. I was creating a text field in NR and pulling it into HA via the companion app, and finding the experience very buggy (there is even a warning about it being in beta). Plus I can only pass in a short text string and not an object (passing in bad data causes corruption that even a restart can’t fix).

So after packing the object into a text string for HA, I use about 55 template sensors hard coded in my configuration.yaml file in a hacky attempt to build a sort of “object” in HA.

In order to make this less hacky, I tried:

  • Creating a loop in Jinja2 to iterate through everything, but I can’t find a way to create the virtual template sensors dynamically in the loop. Can’t put Jinja2 in the configuration.yaml unless I hardcode each one and use a “state” key.
  • Researching online, people say a better way to pass the data into HA is with MQTT, but I can’t find much on how to do the handling on the HA side to accomplish what I want.
  • I know HA supports “scripts”, but not sure how advanced they can get in terms of looping, Python, Jinya2, and creating and handling sensors.

Any other ideas or options?

I’m trying to create an advanced thermostat “app”, that handles multiple rooms and room states.

I don’t know what to address first. Your post doesn’t really make it clear what you are trying to achieve…

To get data from Node-Red to HA you just need to use the sensor node. I assume that you actually have a payload of some kind in Node-Red that contains the data?

Why? You can create a sensor with attributes in Node-Red, why put the info into a string to just unpack and repack in HA?

I’m confused as to why you want to use jinga templates in node red. You can use the blue home assistant template node for that btw.

Nodered is a mix between JSON, Jsonata, and Javascript/node.js Trying to do things in another language is always going to be difficult.

Thanks.
That’s because if I added details, my post would have been 5 pages and no one would read it.
I need to allow Home Assistant to modify values and have Node Red see it. Sensors in HA can’t be directly modified as far as I know.
That’s why I did it the hacky way.

Based on what you are describing, it sounds like you need to use a helper?

This is an xy problem I’m just going to start with the basics. What triggers the temp change?

A sensor? event state a simple if >/< = etc

Screenshot 2024-02-05 103506

If you want to add conditions. This persons home, outside weather is x Trigger node

Screenshot 2024-02-05 103902

Time? Use a time node and create a time only helper in ha. This value can be changed by an automation in ha or nr. You can also place the time entity on the dash to change manually. The node will fire once at the set time

Screenshot 2024-02-05 104605

Like the datetime helper, you can also create number entities that can be changed. You need to think horizontally now, the flow.

With each of these triggers comes a json message with various information, this is the heart of nodered. as it runs down the line we make changes based on tests. Your conditionals, current state of an entity.

Screenshot 2024-02-05 105314

If this entity is in this state go out the top output otherwise go out the bottom and do something else.

There are time nodes, do this after 10, do that before

Screenshot 2024-02-05 110413

Switch node. The message from the trigger, say it contains a temp value.

Screenshot 2024-02-05 110003

You can add additional tests and each one will create a new output to connect to. The template node could be used here as well with a jinga template.

Overall the message that gets passed down the line is where you make all your changes. Instead of writing an if statement that logic is now visibly represented as a node in the path of a flow.

Debug is your friend, set it to complete message object. This is where you check to see if the output of the node is what you expect.

Screenshot 2024-02-05 110930