Rest API - All 'domain' states in a single call

Hi everyone. I am using the REST API to get vehicle data (Toyota) into a custom dashboard…

Currently, I can get the above info in one of two ways…

Screenshot 2025-01-09 120801
This call is faster but lists every single entity state in the entire system! Also, it numbers each entity individually and those numbers change so it will mess with the JavaScript code.

Screenshot 2025-01-09 120825
This call works well but I need to make a separate API call for each entity so there is considerable lagtime.

So all that to ask the question: Is there a way to list all services/states within one “domain” (toyota_na) in a single API call?

Thank you very much to this amazing community!

You can use the api/template endpoint to post with a body like this to give you name and state. Add to the template for other attributes needed.

{"template" : 
    "{% set data = namespace(entities=[]) %}{% for entity in integration_entities('sun') %}{% set e = {'name': entity, 'state': states(entity)} %}{% set data.entities = data.entities + [e] %}{% endfor %}{{data.entities}}"
}

EDIT: Obviously change the integration name in the template

1 Like

This is great, thank you so much for your reply! I have managed to get all of this in one API call using your suggestion…

[{'name': 'binary_sensor.front_passenger_door_2023_rav4', 'state': 'off'}, {'name': 'binary_sensor.rear_driver_door_2023_rav4', 'state': 'off'}, {'name': 'binary_sensor.rear_passenger_door_2023_rav4', 'state': 'off'}, {'name': 'binary_sensor.hood_2023_rav4', 'state': 'off'}, {'name': 'binary_sensor.trunk_2023_rav4', 'state': 'off'}, {'name': 'binary_sensor.moonroof_2023_rav4', 'state': 'unavailable'}, {'name': 'binary_sensor.front_driver_window_2023_rav4', 'state': 'off'}, {'name': 'binary_sensor.front_passenger_window_2023_rav4', 'state': 'off'}, {'name': 'binary_sensor.rear_driver_window_2023_rav4', 'state': 'unavailable'}, {'name': 'binary_sensor.rear_passenger_window_2023_rav4', 'state': 'unavailable'}, {'name': 'binary_sensor.front_driver_door_lock_2023_rav4', 'state': 'off'}, {'name': 'binary_sensor.front_passenger_door_lock_2023_rav4', 'state': 'off'}, {'name': 'binary_sensor.rear_driver_door_lock_2023_rav4', 'state': 'off'}, {'name': 'binary_sensor.rear_passenger_door_lock_2023_rav4', 'state': 'off'}, {'name': 'binary_sensor.remote_start_2023_rav4', 'state': 'off'}, {'name': 'device_tracker.last_parked_location_2023_rav4', 'state': 'not_home'}, {'name': 'device_tracker.current_location_2023_rav4', 'state': 'not_home'}, {'name': 'lock.2023_rav4', 'state': 'locked'}, {'name': 'sensor.distance_to_empty_2023_rav4', 'state': '343'}, {'name': 'sensor.fuel_level_2023_rav4', 'state': '78'}, {'name': 'sensor.odometer_2023_rav4', 'state': '25543'}, {'name': 'sensor.trip_details_a_2023_rav4', 'state': '8654'}, {'name': 'sensor.trip_details_b_2023_rav4', 'state': '5543'}, {'name': 'sensor.next_service_2023_rav4', 'state': '6457'}, {'name': 'sensor.last_update_timestamp_2023_rav4', 'state': '1736440635.0'}, {'name': 'sensor.speed_2023_rav4', 'state': '0.0'}, {'name': 'binary_sensor.front_driver_door_2023_rav4', 'state': 'off'}]

I am having a hard time parsing the data as it comes in one large text string. I have tried JSON.stringify() & JSON.parse(). I’d rather not just search for the text I need. I’d love to access like this: data.name[1].state (for example). Any suggestions? Thank you again for your time.

I got it! I just had to add “|tojson” to the data.entities at the end…

{"template" : 
    "{% set data = namespace(entities=[]) %}{% for entity in integration_entities('toyota_na') %}{% set e = {'name': entity, 'state': states(entity)} %}{% set data.entities = data.entities + [e] %}{% endfor %}{{data.entities|tojson}}"
}

Thank you so much for your time!

For the format you are looking for you could use a template like this

{% set data = namespace(entities={}) %}
{% for entity in integration_entities('wiser') %}
{% set e = {entity:{"state":states(entity)}} %}
{% set data.entities = dict(data.entities, **e) %}
{% endfor %}
{{ {"data":data.entities} | to_json}}

Needs to be all on 1 line for api call

1 Like