Adding entities in bulk based on REST API

Hi,
I’m new to Home Assistant and I’ve been trying to accomplish something in the past few days that i don’t know if its possible. I found an API that returns a list of all the traffic cameras of my country (it basically gives you the coordinates the road name and a link to the camera feed).

My question is witch would be the best approach on adding all of the cameras in bulk. I’ve tried using Node-RED but I’m only able to add one camera from the array.
Bellow I show an example of the returned response of the API:

{
...
    "spatialReference": <object>,
    "fields": <array>,
    "features": [
        {
            {
            "attributes": {
                "objectid": <number>
                "description": <string>
                "url1": <url>,
                "url2": <url>
            },
            "geometry": {
                "x": <number>
                "y": <number>
            }
        {
            "attributes": {
                "objectid": <number>
                "description": <string>
                "url1": <url>
                "url2": <url>
            },
            "geometry": {
                "x": <number>
                "y": <number>
            }
        }
]}

PS: the part that matters is the features, the other stuff is just the containing fields and map projection information.

Thanks in advance for the help.

There is no REST API to add entities to HA. There’s a POST /api/states/<entity id> API but you can’t use that for this. That won’t actually make camera entities (or any particular type of entity) it just shoves data in the state machine like it’s a giant hashmap. Even if you call the entity camera.something it won’t be a camera and won’t work like one. It also will disappear after a restart.

The only way to make cameras from outside HA without touching anything in HA is via Mqtt discovery if you have HA set up to talk to an Mqtt broker. Other then that you have to find an integration for your camera and follow it’s configuration instructions.

Hi, thanks for the reply. I don’t need it to be a camera entity i only need it to “hold” the url’s is that achievable than?

I suppose. If you have the HA nodes set up in NR and the corresponding custom integration installed then NR can stuff that data in the attributes of a sensor that will survive restarts.

But then you would still need to make a generic camera entity in HA for each one that uses a template to set its URL from a particular item in the list. I’d only want to do this if the URLs are changing a lot, if they are static I’d probably just copy and paste them into the config of the generic camera. Since you have to configure a generic camera per entity anyway this extra entity with the URLs doesn’t have much value if the URLs are static.

If the number of cameras in the list is frequently changing then this isn’t going to work. There is no way to automatically make each item in a list in the attribute of a sensor into a camera. You would have to define config per camera you wanted. If the list can change in length then MQTT discovery is probably best bet so you can cause HA to discover and delete cameras on the fly.

EDIT: Actually not totally sure MQTT discovery would work. I don’t think MQTT cameras can use a URL for the image source, pretty sure they only want binary or base64 encoded data in the mqtt payload. I think if the list varies in length you might have to make your own custom integration to handle it or find an existing one for your particular data source in HACS.

1 Like

I might just do a generic sensor in general in which one of the attributes is a url. Than ill just use the html card to display the contents of the url. Thanks.

1 Like

True! I didn’t think about that, if all you want to do is show in the UI you don’t really need a camera at all. HTML/Markdown card works just fine.

1 Like