LabVIEW – Home Assistant integration via the WebSocket API

Finally the awesome and now free LabVIEW Community Edition is released so it is time to bring out the big guns to automate your home!

Home Assistant is an exceptionally awesome platform for home automation and it is my personal favorite. At the same time there are things which are fundamentally complicated or impossible – like catching an event while processing an other. But fortunately Home Assistant is open enough so you can implement these tasks in other tools.

When talking about graphical programming NodeRED was one of the go-environments but truth is that NodeRED can be annoying too. This is for example how it looks to be able to check a condition while being in a loop:

My code that handles continuous brightness change with IKEA Tradfri controllers in NodeRED

We just know that it shouldn’t really be that complicated right? Now here is the great deal.

A few days ago the LabVIEW Community Edition was released

It means for us that the de-facto industry standard graphical programming language being used from space scientists to car manufacturers is completely free for personal, non-commercial usage via the LabVIEW Community Edition.

The great thing about LabVIEW is that while it is graphical it let’s you do programming and you are not enforced to some crazy way of doing loops and weird building blocks as with many other graphical languages (a bit the case with NodeRED too..)

LabVIEW – Home Assistant Integration via the WebSocket API

Writing code in LabVIEW is real fun, at the same time to create code that is an integration API but is still easy to use and flexible requires some programming experience and may be demotivating for beginners.

My goal with this integration framework is to provide an interface to even beginners so that everyone can focus on the home automation tasks and not how WebSockets work and how to open TCP/IP connections.

How to get started?

Next to the standard LabVIEW installation you need 2 additional, free add-ons. The add-ons can be easily installed with the VI Package Manager that installs with standard LabVIEW installations.

If you have all add-ons then let’s clone the application from github: https://github.com/litechniks/hass-lv-ws-integration

You also need to generate a Long-Lived Access Token in Home Assistant (can be done by clicking on your User badge). The purpose of tokens in general is described here: IT Security

Make sure to write it down when creating as you will not have a chance later to do that.

Now open the downloaded LV project file (LV-HASS-WS.lvproj) and open the TestMainApp.vi in the Example folder. You will see this Front Panel

And this Block Diagram (Ctrl+E to open it):

Set the IP address of your own Home Assistant instance instead of the default one. Also if you are not using the 8123 port, you can set it under the IP address.

Upon the first run it will won’t authenticate but will generate a txt file next to the project file. Simply copy your Long-Lived Access Token into the txt and with the next run it will authenticate well. Or if your don’t want to use an external file to store your Access Token just simply delete the file operation VI and connect your access token as a string.

The wsHandlerEngine.vi needs to run continuously in parallel to the application as this is the one that managed the WebSocket part.

Currently Supported Features

Right now not all the Home Assistant WebService API features are implemented. This is what the integration does at the moment:

  • Connect and Authenticate
  • Register and Unregister for the Home Assistant Events
    • Empty string registers for all Events
  • Call Home Assistant Services
  • Fetch all States

Basic Theory of Operation

  • You send and receive WebSocket messages via the wsHandlerEngine.vi and can do that by using the send and receive queues.
  • Events are identified by message IDs (this is how the Home Assistant WebSocket API works) and LV User Events are generated based on Home Assistant Events that can be handled via the Event Structure.
  • The HomeAssistant WebSocket API fetches all states so if you want to work specifically with one you can select it by entity_id in the returned array of states (actually entities)
Application in operation

Next Steps

I will implement soon the remaining features of the Home Assistant WebSocket API and also will make it more comfortable to deal with events – now a large JSON string is returned.

4 thoughts on “LabVIEW – Home Assistant integration via the WebSocket API”

  1. Hi there

    thank you very much for this tools.
    I am trying to use them to make the automations in my home.
    However, the fetch vi is taking more the 50 seconds to finish.
    Did you get something like this when you use it?

    Thank you in advance

    1. Hi, thank you for your comment!
      Home Assistant provides multiple APIs and via the WebSocket communication API the only option to get states is to get them all.
      This is not performance effective so can take a while to fetch `em. At the same time in my case this is like ~2 seconds. You may have a lot of entities and thus this communication is slower.

      I will try to complete my solution with a REST API in the upcoming two weeks (depending on my workload) and update you.
      Cheers!

      1. Hi again,

        In my case, I have 97 entities, so it takes some time.
        I will wait for your sullition , then.
        My goal is tu use Home Assistant as a gateway for all the hardware, but use Labview for automation and User Interface. 😉

        Thank you

        1. Hi, just wanted to give you an update that I have added basic REST API support. You can find an updated example program.
          Interesting lesson is that it is the JSON parsing in LabVIEW which really slows things down. Nevertheless if you use the single entity status request for the REST API then it is pretty fast

Leave a Reply