Template to output areas and devices for LLM

I’m looking to write a template that will output all areas I have set up and the devices in those areas as a JSON which I can paste into an LLM prompt. Something like this:

{ "areas": [
    {
    "area": "lounge",
    "devices": [
        {
        "device": "light.corner_lamp",
        "friendly_name": "Corner Lamp"
        "type": "light"
        },
        {
        "device": "light.main_overhead",
        "friendly_name": "Overhead Lights"
        "type": "light"
        }
    },
    {
    "area": "kitchen".... etc

I’ve had a search around but didn’t find anything. Does anyone have something like this they’d be willing to share? I’m still working on trying to create it myself (with my limited jinja knowledge!) but if someone’s already done this…

Thanks

Here’s one way to approach it…

{% set domain_wh_list = ['switch', 'light', 'climate']%}
{%- set ns = namespace(areas=[]) %}
{%- for a in areas() %}
  {%- set inner = namespace(ents=[]) %}
  {%- for e in area_entities(a)|expand|selectattr('domain', 'in', domain_wh_list) %}
    {%- set inner.ents = inner.ents + [{"entity": e.entity_id,"friendly_name": e.name,"type": e.domain }] %}
  {%- endfor %}
{% set ns.areas = ns.areas + [{"area": area_name(a), "entities": inner.ents}] %}
{% endfor %}
{{ {"areas": ns.areas} | to_json }}
1 Like
{% set ns = namespace(areas=[], entities=[]) %}
{% for a in areas() %}
  {% set ns.entities=[] %}
  {% for e in area_entities(a) %}
    {% set ns.entities = ns.entities
      + [ dict(entity_id=e,
          name=state_attr(e, 'friendly_name'),
          type=e.split('.')[0]) ] %}
  {% endfor %}
  {% set ns.areas = ns.areas + [dict(area=area_name(a), entities=ns.entities)] %}
{% endfor %}
{{ dict(areas=ns.areas) }}

EDIT 1

Ninja’d by Didgeridrew

EDIT 2

Both examples have changed the device in your example to either entity or entity_id because light.kitchen is the entity_id of an entity, it doesn’t identify a device (entity and device have unique meanings in Home Assistant).

EDIT 3

Depending on how you intend to use the template’s result, you may need to append to_json to the last line (for example, if you intend to publish the result to an MQTT topic).

2 Likes

Note just dumping the ‘stuff’ in an area doesn’t do much to help, because the LLM already has access to it through the exposed devices collection. In fact, if you’re using the default openai integration it has access to all the aliases to all those entities too! (neat tricks to be had there see later. )

So, to just give the LLM a list of stuff doesnt add much. BUT

If you ALSO simultaneously give it a list of stuff (entities by area) and each thing is carefully tagged (labels) with the kind of stuff (go nuts with metadata, like every thing, think like indexing a book) it is and where it belongs (assign areas) and what you do with it (who said an alias just has to be another name for something… The LLM clearly sees extra data here and will use it) and how many different names it has (alias liberally)…

So In your template dump ^^^ THAT… (by room by entity with all those labels… Heck, no reason not to filter everything through expand or use the expanded version of the state.) Then also dump the list of labels and tell your LLM that list is the index…in your directives tell the LLM the index can be used to associate things and stuff

THAT will make a huge monumental difference… Stuff that prompt full with metadata.

(Edit. Yes one of the first intents I wrote gives the llm a tool to pull entities by label. I call it the library. *points at index)

4 Likes