Mapping/creating multiple sensors and switches from REST API

I’m trying to integrate HA with an existing automation solution (Idratek) which exposes all of the objects via a REST API.

I’ve read the RESTFul API spec, but am having a few challenges with the template mapping (probably my lack of experience with the Jinja2 format doesn’t help)

Have a few questions for the community…

  1. Can you use the name returned by the API JSON as the HA object name?

So far, all examples I’ve seen hard-code the object name under

sensor:
  - name: "My sensor name"

The REST API I’m integrating with returns a list of sensors, their unique IDs, and names. Can I iteratively add these to Home Assistant in any way?

e.g. something like

resource: http://192.168.50.61/api/v1/Objects
sensor:
  - name: "{{  value_json.CortexAPI.CortexObject[0].FriendlyName }}"

And then use the loop functionality in jinja to iterate each object?

The object list JSON looks like this:

{
    "CortexAPI": {
        "CortexObject": [
           {
                "ControlObjectType": "Switch",
                "FriendlyName": "Front bedroom socket",
                "IDNumber": "497",
                "Parent": "Front bedroom",
            },
            {
                "ControlObjectType": "Floor",
                "FriendlyName": "Lower ground",
                "IDNumber": "19",
                "Parent": "House",
            },
            {
                "ControlObjectType": "Room",
                "FriendlyName": "Bedroom",
                "IDNumber": "261",
                "Parent": "Lower ground",
            },
            {
                "ControlObjectType": "Motion",
                "FriendlyName": "Flat Motion",
                "IDNumber": "252",
                "Parent": "Flat sensor",
            },
            {
                "ControlObjectType": "Temperature",
                "FriendlyName": "Flat temperature",
                "IDNumber": "253",
                "Parent": "Flat",
            }
 ]
    }
}

  1. I then need to call the API again, and iterate for each (relevant) sensor, to get the objects state or value attributes (called ports in the JSON schema):

Room presence example:

resource: http://192.168.50.61/api/v1/Ports/Lower Ground/1

Gives JSON:

{
    "CortexAPI": {
        "PortEvent": {
            "FriendlyName": "Lower ground",
            "IDNumber": "19",
            "PortNumber": "1",
            "PortDescription": "Presence state output",
            "Value": "0",
            "State": "false",
        }
    }
}

For room presence, I’m interested in mapping the “Value” returned by a room object to the appropriate “Area” objects in HA to indicate the occupied state for each room.

The “Area” object within Home Assitant doesn’t have a presence attribute, so presumably I’d just need to store this in a separate entity that’s located in the relevant Area.

Room temperature object example:

resource: http://192.168.50.61/api/v1/Ports/Kitchen temperature/0

Gives JSON output

{
    "CortexAPI": {
        "PortEvent": {
            "FriendlyName": "Kitchen Temperature",
            "IDNumber": "107",
            "PortDescription": "Temperature Output",
           "Value": "18.83984375",
        }
    }
}

Which I think I can just map to a sensor object using something like:

    sensor:
      - name: "Kitchen temperature"
        value_template: '{{ value_json.CortexAPI.PortEvent.Value }}'
		json_attributes_path: "$.CortexAPI.PortEvent"
		unit_of_measurement: "C"
		json_attributes:
          - "Value"

Any pointers appreciated.

Regards

Neil

You can with template sensors, but you’ll run into issues if your rest api changes the order of your entities. Because your entity_id won’t change but your device will. Meaning you’ll have entities that just randomly change what they control… Not good.

It’s better to hardcode the name, and ensure your templates are outputting that device.


As a sidebar, this actually seems like you should create a custom integration for this. As you’ll have all the control to handle this without being held back by the rest integration.


Your other option is to use the rest integration, stuff all the values into attributes, then use an automation to create MQTT sensors via discovery.

Thanks for the pointers.

I actually started down the custom integration route, until I realised that there was a REST integration that could talk to the API I have available.

I guess the advantages of a custom integration would be discovery of devices would be possible whereas the REST would need every device to be hard-coded in the YAML.

I’ll read the custom integration documentation and see if my python skills are sufficient to have a crack at it!