Custom Component for node-red-contrib-home-assistant-websocket

UPDATE: This has been pushed out to full release.


Looking for some testers for new custom integration to be a companion to node-red-contrib-home-assistant-websocket. It will add two new nodes and enable each event node to be exposed to Home Assistant as switches.

Sensor Node:

It will create a sensor or binary sensor inside Home Assistant that can be updated directly from Node-RED.

Webhook Node:

Creates a webhook in Home Assistant that will be handled by Node-RED.

https://:/api/webhook/LvNwB2ckjGWyYGVvArJ0uCUbQFrqAWcg

Event Nodes (events: all, events: state, trigger: state, and poll state):

Will have the option to be exposed to Home Assistant, and when enabled, it will show up in Home Assistant as a switch. Turning on and off these switches will disable/enable the nodes in Node-RED. This should help people who find themselves having to make input_booleans in HA to enable/disable flows.

Installation

Installation of custom integration can be done using HACS as a custom repository, https://github.com/zachowj/hass-node-red, or manually, instruction can be found in the README.

To get the new nodes in NR you’ll need to install a new branch of node-red-contrib-home-assistant-websocket.

Shell

$ npm install https://github.com/zachowj/node-red-contrib-home-assistant-websocket#integration

Hass.io Users need to add this to their config

"npm_packages": [
  "git+https://github.com/zachowj/node-red-contrib-home-assistant-websocket#integration"
],

Tip:

Filling in the name field in the HA config of these new nodes before you first deploy will attempt to make it with that entity_id otherwise it will default to sensor.nodered_79fba2c4_04342c. Where the random string is the node id in Node-RED. The entity ids change be changed in HA after creating them in NR.

9 Likes

I would love to try this, since I lost a REST based node HASS-post, that is no longer working due to api passwords being dropped by HA,and i used to send attributes to a fake sensor, now i think with your implementation I can make a sensor and update its attributes, but sadly i am still a noob, I use Hassio and I don’t know where exactly to add the npm package, as you described. Thank You.

@rraven Go to the Hassio Node-RED addon, and put it into the config there.

@Kermit, This looks great! I’ll have to think of some ideas to use the new nodes.

1 Like

Got it, thank you sir, i totally forgot, since i already added a package that way.
I will test these nodes now :slight_smile:

This is really interesting!

I don’t use Node-RED, but I’ve always wished for a way to add sensor, binary_sensor, switch, etc entities to Home Assistant from an external source. This can be done with the REST API, of course, but it doesn’t use config flow, doesn’t register like an integration, and the entities aren’t first-class entities in Home Assistant (i.e. you can’t rename them, for example).

Your integration seems to cover all of that.

Do you have any documentation for using the integration available? I can scour the Node Red Node code to find it, but, if you’ve got it documented somewhere, that’ll make it a lot easier.

This is beautiful ! Just yesterday i asked for help to replace a custom node which no longer works (using an the old API password authentication) , “node-red-contrib-hass” , that sent statuses, attributes via REST api to my “fake” template sensor in HA, in order to populate it with info from Node Red, and after just a bit, @Kermit came with this solution. Is like it was made for the old noob me! Thank you!
I already tried the sensor node, and it works beautifully for me.


And the result (on the ‘statusac’ attribute, in a markdown card in lovelace ; don’t mind the language please):
Screenshot_18

This part didn’t worked, i got the sensor.nodered_randomshere , but i changed it in the integrations, no problem.

Don’t really have any documentation as it is all handled by Node-RED and home-assistant-js-websocket so the end-user doesn’t really need to know how it works.

Pretty much everything you really need to know to use it without node-red is located in
https://github.com/zachowj/hass-node-red/blob/63475d1536d2881ec2cb1ccb62f2ff65a5ff2dff/custom_components/nodered/websocket.py. The integration creates two websocket commands, nodered/discovery and nodered/entity. Discovery is what creates the entity in HA and entity is what updates it.

nodered/discovery
component sensor/binary_sensor/switch
server_id/node_id Are just unique identifiers so they can be any string
config Is any normal HA config that would go with the selected component
remove If this is set to true the entity will be removed from HA

nodered/entity
server_id/node_id Are just unique identifiers so they can be any string. Need to be the as what was used in the discovery call
state The value to set entity state
attributes key/value pair of attributes to set for the entity

home-assistant-js-websocket handles all the websocket communication between NR and HA so for a switch component the integration will send back a message on the same command id that it received the discovery of the switch on. That’s how it updates node-red of the state change from HA. For sensors, it’s just one-way communication from NR to HA.

In practice this would look something like:

  1. make a websocket connection to HA
  2. send all the nodered/discovery messages to HA
  3. listen for the command id message back from HA for any switch changes
  4. send nodered/entity updates as needed

Hopefully, that might help you a little. This is all subject to change as it is my first iteration also my first time writing any substantial python.

1 Like

Nice to hear it’s working for you.

I’m guessing the preset name didn’t work because it used the name for the node at the top and not below in the HA config box. If that’s the case I should have been more clear.

1 Like

Oh. I’m sorry, my bad then. I didn’t understood properly (inglish iz haard) . Next time I will do it right. Thank you again, I’m so happy it works!
Side question: Is it possible to use the same sensor node in other flow , without creating another sensor in HA? Let’s say i just use the same sensor to update another attribute from another flow.

Take a look at the link in and link out nodes

1 Like

Is there anyway to test this with NR running inside docker. I tried adding the shell command, and restarting NR but no additional nodes showed up

You can install Node RED components using the command pallet (I think that’s what it’s called) in the Node RED GUI. Not sure if it allows arbitrary github URLs or not.

This is enough to get me started. Thank you for this!

i already have the websockets node installed, updating to use the integration branch using the command above said it had updated, yet there is no new nodes available other than the ones for the current websockets node

Ah. That’s goes a bit deeper than my cursory understanding of Node RED. Hopefully, someone else can help.

If you have npm installed on the docker host you just need to run the npm install command from the where you have your config files of node-red.

If you don’t have npm installed on the host system you can log into the shell and do it from there.

$ docker exec -it <containername> /bin/bash
$ cd /data
$ npm install https://github.com/zachowj/node-red-contrib-home-assistant-websocket
$ exit

restart node-red

1 Like

Working great (HA and NR both on docker)! I can certainly find a lot of use cases for this extension!

Is the “resend state and attributes” toggle supposed to resend the values after a HA restart?

Yes, when NR registers the entity with HA and it doesn’t exist yet it will send the last values if they exist.

This didn’t work the first restart of HA for me, but since then it have worked flawless :slight_smile:

Yes, I did find a bug where the sensor wouldn’t resend the old payload until the node had been deployed again.

1 Like