uncategorized

Intent Handling

After a voice command is recognized and its intent is identified, for example, by Rhasspy, the intent needs to be handled, for example, by Home Assistant. Below we illustrate how to do this by using Rhasspy and Home Assistant.

Event Driven

When we configure Rhasspy to use Home Assistant for intent handling, there two options: One is to send an event to Home Assistant, the other is to send the intent directly to Home Assistant. Here we will use the first option. In this case, the event (along other information such as identified slots) sent to Home Assistant will be named rhasspy_INTENT_NAME. We take rhasspy_turn_on and rhasspy_turn_off for turning on/off some devices as an example in the sequel.

First, we want to handle the events rhasspy_turn_on and rhasspy_turn_off in the same places. Second, we want certain flexibility to handle the events. For these, we choose to use a python script to handle the events, by first creating a file rhasspy_turn_on_off.py in the folder /config/python_scripts (by using the File Editor add-on); create the folder python_scripts if it does not exist. An example of script is found here. In the example script, we assume the entity is named in the format DOMAIN.NAME_ROOM_FLOOR, e.g., light.ceiling_light_master_room_second_floor. Also modify the dictionary variable site_id_to_area accordingly. The example script identifies the device name and area from the event data sent from Rhasspy, and use them to locate the associated Home Assistant entity. It also automatically infers the area of the device if the area is not provided by the users.

Then, add python_script: to /config/configuration.yaml (without leading indent). Restart Home Assistant. Finally, we create an automation in Home Assistant with the following configuration:

  • Name: the name for the automation, for example, Rhasspy Turn On.
  • Description: a brief description of the automation, for example, turn on a device when receiving a rhasspy_turn_on event.
  • Trigger type: Event
  • Event type: this must be the event name, and in our example here, it is rhasspy_turn_on.
  • Action type: Call service
  • Service: python_script.rhasspy_turn_on_off

Then, click the three-dot icon and choose Edit in YAML. Then under the line service: python_script.rhasspy_turn_on_off, provide the following lines of code:

data:
  event_type: '{{ trigger.event.event_type }}'
  message: '{{ trigger.event.data }}'

This code ensures that the event type and the associated event data (sent from Rhasspy) will be passed to the python script rhasspy_turn_on_off.py.