Node-red-contrib-home-assistant-websocket

Hello all,

Node-red-contrib-home-assistant-websocket is my fork of node-red-contrib-home-assistant that I plan on maintaining. One of the major changes is that I have changed over the main communication from using the REST API and Event Stream to use WebSocket where ever is possible.

Some new key feature

  • Using WebSocket where ever possible for communication to and from Home Assistant.
  • Ability to use Long-Lived Access Tokens to authenticate with Home Assistant as well as still supporting the legacy API password.
  • Server credentials are now encrypted and stored in the node-red cred file. This also stops them from being exported in flows.
  • A new ‘Fire Event’ Node that allows you to fire events to Home Assistant.
  • Check the CHANGELOG for a list of fixes and changes that have been made so far.

Installation

Install via Node-RED Manage Palette

node-red-contrib-home-assistant-websocket

Install via npm

$ cd cd ~/.node-red
$ npm install node-red-contrib-home-assistant-websocket
# then restart node-red

If you have been using @Ayapejian version there are some breaking changes with two of the nodes. The ‘Poll State’ and ‘Current State’ both have a little different formatted output. Check your nodes and make sure they are outputting what you expect.

If you have been using @Spartan-II-117 fork this should be a drop in replacement as of this posting.

Support

If you find any issues/bugs please create a new issue on github so I can address them. Also if you have any feature requests that you think may improve this, I’d also be glad to hear from you.

Thanks.

20 Likes

Thank you,
What are the advantages of using websockets over REST API and Event Stream? Only the longlived access tokens?
As you can see I’m not well informed on this subject…

I’ve been using this for a week now without problems.
Thank you.

I am no expert but do know some of the basics. A very general high overview of a WebSocket is that it allows for two way communication over a single socket/connection. Unlike an HTTP call where it has to open a new connection for each request that contains headers and authentication that the server has to process for each request. In short, using a WebSocket should be faster and able to process more request in the same amount of time. But running node-RED and Home Assistant on the same server or LAN makes most of that irrelevant.

The main reason I switched over to using a WebSocket as the communication layer was to deal with an issue, like posted here, where node-RED would lose it’s connection to Home Assistant. This is where the event stream comes into play, node-RED would open an HTTP connection with Home Assistant asking for all the event changes. Home Assistant would keep this connection open streaming all those changes to node-RED. If there was ever a problem with this connection whether it be restarting Home Assistant or the connection just dying node-RED wouldn’t realize that it had lost the connection and would do nothing about it. So all nodes would stop receiving updates. With a WebSocket, we are easily able to see when the connection dies and attempt to reconnect and continue operations.

1 Like

Migrate to your fork, work correct after ha restart, will test it tomorrow more closely. Thanks for your work.

This node looks really good.

I have a number of flows using the nodes from @ayapejian and would like to replace his library with yours. I’m unable to uninstall as the nodes are currently used. Any advise on how to replace the existing library with yours ?

The easiest route would be if you have shell/command line access and are able to stop node-RED uninstall node-red-contrib-home-assistant and then install node-red-contrib-home-assistant-websocket

# stop node-red
$ cd ~/.node-red
$ npm uninstall node-red-contrib-home-assistant
$ npm install node-red-contrib-home-assistant-websocket
# then restart node-red

If that’s not an option you could export all the flows that use the Home Assistant nodes save them in a text file so you accidentally don’t lose them. Then delete the flows you exported which should then allow you to uninstall node-red-contrib-home-assistant via the palette and then install node-red-contrib-home-assistant-websocket. After that, you should be able to import the flows that you exported earlier. Using this method you’ll probably have to re-enter your Home Assistant credentials.

Edit:
@frenck posted on Reddit instruction if you’re a hass.io user. https://www.reddit.com/r/homeassistant/comments/9gb5vr/looking_for_some_nodered_testers/e66or5b/

Replaced the original palette with your fork this afternoon. Tested my automations and works great ! Thx for the good work. I’ll report if i have anything wrong.

Do you have to redo node by node, or swapping the package using npm (while nodered is off) works good?

@ Kermit Awesome, thank you for the instructions. I have a dedicated Node-Red install and will be able to do the command line option

Happy that there is new maintainers of the home assistant node red flows, but like buses, you wait for 1 and 2 come along. Would it be beneficial or possible if yourself and spartans fork could merge and have 2 or more people who are able to maintain one project?

At the moment i have to choose with staying with spartans fork or moving to this one,and run the risk of my wrong choice going stale and not being maintained

just a thought. either way, keep up the good work

Just upgraded and no had issues at all. Thanks for making the changes to Websocket. Hoping to get rid of the connection problem.

Just swapping the package out and replacing it should work in most cases. The only caveat being that if you’re coming from Ayapejian version you’ll have to check your ‘Poll State’ and ‘Current State’ nodes both have a little different output.

Using hass.io and suddenly I see the status below each of the nodes. :slight_smile:

Thanks for this!

Spoke too soon. I’ve generated a Long Live Account Token under my HA account, and used the generated token on Access Token. Base URL is http://hassio/homeassistant and Use Legacy API Password is checked. When I deploy I get:

[error] [server:Home Assistant] Error: [Home Assistant] Invalid access token or password.

What am I doing wrong?

If you’re using the Long Lived Access Token uncheck the Use Legacy API password checkbox

Ah yes, that worked. Also, http://hassio/homeassistant won’t work for the Base URL. It should be: http://127.0.0.1:8123 or http://IP:8123

Thanks!

1 Like

Hi, i’m getting following error trying to update

RangeError: Invalid WebSocket frame: RSV2 and RSV3 must be clear

EDIT:

Well, tried to rollback to 0.0.4, and now I seams to connect, but nothing is working.
I’m getting this error

entity could not be found in cache for entity_id: sensor.humidity1, sending empty payload

What version of nodejs are you running and are you running node-RED in docker? That looks to be same error I was getting when I was testing with an older version of node pre v8.11.2.

If you’re running at least node v8.12 can you let me know your setup so I can look into it

Node version?
Running in Docker?
hass.io?

You were exactly right!

I’m running Node-Red via docker on my RPI, using v8 image.
Node version was 8.1.3, and going to the container updating to 8.12.0 solved to problem!

Thanks a lot