Passing a template to voice

I am trying to expose the area’s temperature and humidity to voice assistant since Home Assistant does not do that. But I think I am getting myself in a chicken/egg situation.

The idea is:

  • Ask voice assistant: “What is the temperature in the Living Room”
  • Voice Assistant find the sensor based on a template:
{{
  states.sensor |
  selectattr('entity_id','in',area_entities('<ROOM_ASKED_FOR>')) |
  selectattr('attributes.device_class','defined') |
  selectattr('attributes.device_class','eq','temperature') | map(attribute='state') |
  map('int',0)|sum
}}
  • Voice assistant response with: “The temperature in the living room is $$ degrees”

The issue is that it feels to me a 2 step process, find out the room from the query, get the temperature/humidity/etc from the sensor and then compose the message. Has anyone attempted something similar?

Thanks in advance

Not clear how you’re approaching this… Is it a custom sentence/intent? Or maybe an entity to be exposed to Assist?

1 Like

FYI, that template should be optimized.

{{
  area_entities('<ROOM_ASKED_FOR>')
    | select('match', '^sensor.')
    | select('has_value')
    | select('is_state_attr', 'device_class','temperature')
    | map(attribute='state') |map('int')|sum
}}

Yes the template should be optimized. :wink:

@TelmoX But for - getting the llm to (insert thing here) you need more than the template, you need to write a script or an intent script like jack said.

Think tools.how would yih build a tool (script) to return that data.

Your case: I need a thing I use a tool to get a thing. The tool is the intent script or script. Inside the script you’ll use your template to get the data and return it as the response

You could simply identify the sensor containing the temperature - in my case

sensor.living_room_motion_sensor_temperature

… expose it to assist and give it a couple of aliases - say, “living room temperature” and “temperature in the living room”.

Then “What’s the living room temperature?” or “What’s the temperature in the living room?” will get you an answer.

1 Like

Thanks for the pointers! is there a guide on query optimization?

I already optimized it…

It is getting an undefined when doing the last map(attribute='state') the previous part of the selector works and select the sensor ['sensor.living_room_temperature'] but trying to select the state returns an undefined

{{
  area_entities('Living Room')
  | select('match', '^sensor.')
  | select('has_value')
  | select('is_state_attr', 'device_class','temperature')
|map(attribute='state')|list
}}

is undefined, the sensor does have a state.

{{ states.sensor.living_room_temperature }}

<template TemplateState(<state sensor.living_room_temperature=66.7; state_class=measurement, unit_of_measurement=°F, device_class=temperature, friendly_name=living_room Temperature @ 2025-04-18T07:11:02.754136-04:00>)>

Do I need to fetch the first element because the return is an array?

I was also asking because I would like to learn about the optimization of the queries so in the future I don’t have to ask, and I may be able to help people in similar situation.

sorry it should be

{{
  area_entities('<ROOM_ASKED_FOR>')
    | select('match', '^sensor.')
    | select('has_value')
    | select('is_state_attr', 'device_class','temperature')
    | map('states') |map('int')|sum
}}

You probably want average too, because sum will add multiple temp sensors together instead of averaging them.

{{
  area_entities('<ROOM_ASKED_FOR>')
    | select('match', '^sensor.')
    | select('has_value')
    | select('is_state_attr', 'device_class','temperature')
    | map('states') |map('int')|average
}}