Consume Streaming REST API within home assistant

I have a device, an Enphase Envoy, which has a streaming REST API endpoint that I’d like to consume with Home Assistant. I’m having a lot of trouble figuring out how to even search for how this is done, so am reaching out to see if someone can point me in the right direction.

I am able to use this particular endpoint from an HTML page using either the browser directly (not very useful), or an XMLHttpRequest to open the connection, and then take action on progress events in java script to consume the regularly sent data from the open connection.

The REST endpoint, https://envoy.local/stream/meter, puts out a data like shown below, every 3/4 to 1 second. How can I consume such a stream and act upon the regularly arriving data within HA?

(For non streaming endpoints, I’ve used the REST sensor within HA, but have had better experience with REST command as that provides more detail in response, such as dealing with http failures and also being able to track latency.)

(As a second question, sometimes this endpoint fails and the streaming has to be restarted, so wondering how that might be detected and accomplished within HA.)

data: {“production”:{“ph-a”:{“p”:479.615,“q”:10.347,“s”:480.464,“v”:123.524,“i”:3.889,“pf”:1.0,“f”:60.0},“ph-b”:{“p”:479.155,“q”:10.407,“s”:480.247,“v”:123.361,“i”:3.894,“pf”:1.0,“f”:60.0},“ph-c”:{“p”:0.0,“q”:0.0,“s”:0.0,“v”:0.0,“i”:0.0,“pf”:0.0,“f”:0.0}},“net-consumption”:{“ph-a”:{“p”:174.2,“q”:-1422.821,“s”:1441.839,“v”:123.501,“i”:11.675,“pf”:0.12,“f”:60.0},“ph-b”:{“p”:621.141,“q”:-1314.328,“s”:1472.328,“v”:123.267,“i”:11.944,“pf”:0.42,“f”:60.0},“ph-c”:{“p”:0.0,“q”:0.0,“s”:0.0,“v”:0.0,“i”:0.0,“pf”:0.0,“f”:0.0}},“total-consumption”:{“ph-a”:{“p”:653.815,“q”:-1412.474,“s”:1922.196,“v”:123.501,“i”:15.564,“pf”:0.34,“f”:60.0},“ph-b”:{“p”:1100.295,“q”:-1303.922,“s”:1952.286,“v”:123.267,“i”:15.838,“pf”:0.56,“f”:60.0},“ph-c”:{“p”:0.0,“q”:0.0,“s”:0.0,“v”:0.0,“i”:0.0,“pf”:0.0,“f”:0.0}}}

I just found this little explainer on how RESTful and Streaming APIs are fundamentally different things. So it would be helpful to understand more about how the data are being presented and accessed. Is the RESTful endpoint used to initiate some kind of http stream? Or is the RESTful endpoint not a stream at all, but just a http get/post whose data result changes every second? Or is it not RESTful at all and more akin to the Event Source API used by esphome?

In other words, you say the device “puts out data” but how does it do that? Does it send a packet (unprompted) somewhere? Or does it make it available to a server upong (get/post) request?

Loading that url directly into a browser window/tab, we will see the json text appear as response, and then every second or so, new such json text appears, appended to the existing text. So the browsers continues to show the old text and adds new.

Doing it from XMLHttpRequest, we can use the onprogress event to detect newly arriving data, and process it.

I would say that the device keeps the tcp connection open, and sends data regularly. Using Javascript, we use onprogress, onerror, etc… to manage the connection. The client doesn’t close the connection then data regularly appears from the server, which also doesn’t close the connection.

Or is the RESTful endpoint not a stream at all, but just a http get/post whose data result changes every second?

It doesn’t “change” the prior results, but adds to them regularly, given that the connection is still open. Connection is opened by a client and the server sends a response keeping the connection open, so it can send more response content, repeatedly.

My understanding is this is more akin to eventsource, and is not technicality RESTful since it maintains an open connection. It might be compatible with a REST sensor that disconnects after the first event is received, and reconnects again later, but that is obviously not as efficient.

Also, I see you mentioned Enphase Envoy, have you tried the official integration?

Thanks for the terminology! I will search for solutions using that keyword.

Yes, I am know the HA Envoy integration - thanks!

I managed to vibe code an integration for streaming the data from the Envoy using Claude Code. Took about 3-4 hours in total fiddling around with it. I know nothing about writing anything for home assistant, yet here we are…! :slight_smile: It really does update every 1s or more!

I’ll try and get this into HACS if/when time permits. It’s totally barebones alpha quality but the point is it works. It uses the httpx library to maintain the streaming connection. You need to use digest auth, and you need the installer password.