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…
- 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",
}
]
}
}
- 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