How to read the area where the voice command was activated?

Hi everyone,

finally got my voice PE and playing around with the assist feature (i love this).
I’ve discovered soon, that assist always uses the thermostat of an area to determine the temperature. However, i want it to read the normal temperature sensors (e.g. we don’t have a thermostat where we sleep).

I was able to create an intent, which reads the area from the voice command, searches for the first temperature sensors and outputs the temperature – almost perfect. The result would be:
German: “Es ist 19.5 Grad im Wohnzimmer” (English: It is 19.5 Degree in the living room)

How can i read the area where the voice was activated?
e.g. if i talk to my voice assistant PE in the kitchen, i want to give out the kitchen’s temperature.
Any other tips you can give me in my automation?

Cheers and a happy new year of tinkering :slight_smile:

alias: Voice - Temperatur im Zimmer
description: ""
triggers:
  - trigger: conversation
    command:
      - Wie warm ist es im {area}
  - trigger: conversation
    command:
      - Wie warm ist es in der {area}
conditions: []
actions:
  - set_conversation_response: >-
      {% set result = states.sensor
      | selectattr('entity_id', 'in', area_entities(trigger.slots.area))
      | selectattr('object_id', 'search',
      '^(?!.*(schalter|steckdose|strom)).*_temperature$')
      | selectattr('state', '!=', 'unavailable')
      | map(attribute='state') | list %}
      {{ 'Es ist ' + result[0] + ' Grad im ' + trigger.slots.area if result else 'Keine Temperaturangabe gefunden' }}
mode: single

Notes

This searches for all sensors
set result = states.sensor

This searches for all entities in the area specified in the voice command
selectattr('entity_id', 'in', area_entities(trigger.slots.area))

This searches for entities with the name temperature, but removes schalter/steckdose/strom (i don’t want to have the internal temperature of my shelly switches)
| selectattr('object_id', 'search', '^(?!.*(schalter|steckdose|strom)).*_temperature$')

This removes the unavailable entities
| selectattr('state', '!=', 'unavailable')

This outputs the first temperature result if it exists, else “Keine Temperaturangabe gefunden” is there is not temp sensor
{{ 'Es ist ' + result[0] + ' Grad im ' + trigger.slots.area if result else 'Keine Temperaturangabe gefunden' }}

Ok, found the solution myself. If anyone is interested.

The area of the triggering device (e.g. the HA voice PE) can be read via
area_id(trigger.device_id)

To keep one single automation, i’ve added intents with {area} and some without.
This expression then sets the correct area: if the intent (voice command) has an area set, then use it, else use the area of the device.
{%- set area = trigger.slots.area if trigger.slots.area else area_id(trigger.device_id) -%}

This is the entire solution if you are interested

alias: Voice - Temperatur im Zimmer

description: >-
  Reads the area from the voice prompt (or the calling device), searches for the first temperature
  sensors and outputs the temperature. The result would be in German - Es ist
  19.5 Grad im Wohnzimmer

triggers:
  - trigger: conversation
    command:
      - Wie (warm|kalt|heiß|kühl) ist es
  - trigger: conversation
    command:
      - Wie (warm|kalt|heiß|kühl) ist es (im|in der) {area}

conditions: []

actions:
  - set_conversation_response: >-
      {%- set area = trigger.slots.area if trigger.slots.area else area_id(trigger.device_id) -%}
      {% set result = states.sensor

      | selectattr('entity_id', 'in', area_entities(area))

      | selectattr('object_id', 'search',
      '^(?!.*(schalter|steckdose|strom)).*_temperature$')

      | rejectattr('state', 'in', ['unavailable','unknown'])

      | map(attribute='state') | list %}

      {{ 'Es ist ' + result[0] + ' Grad im Raum ' + area if result
      else 'Keine Temperaturangabe im ' + area + ' gefunden' }}


mode: single
6 Likes

This is brilliant, Thank you @Hell255.
Two issues with me though, I was careful changing the text to English BTW :slight_smile:.

The response for the area is announced using the long area ID! (removing ‘+ area’ does kind of solve this).
Also, asking for the other area/room temperatures are responded as ‘Done’ only.

Hope you can help me on this, it would be a cleaner way of asking for temperatures around the home.