Beginners guide to use Rhasspy with HomeAssistant

I set up that automation. It does trigger in home assistant, but doesn’t speak anything.
I tried with intents and there it does speak. Anyone has an an idea to make it work?

Solved it. Have configured external mqtt. Then I was getting the
Error rendering data template: UndefinedError: ‘dict object’ has no attribute ‘event’
and
no sensor defined.
Had to change the automation to

id: Rhasspy GetTime
alias: Rhasspy GetTime
trigger:
  - platform: event
    event_data: {}
    event_type: rhasspy_GetTime
action:
  - service: mqtt.publish
    data_template:
      topic: hermes/dialogueManager/endSession
      payload: >-
        {"sessionId": "{{ trigger.event.data._intent.sessionId }}", "text": "{{
        states('sensor.time') }}"}
mode: single

sensor with states(‘sensor.time’) and just payload: >-

I will probably come back here to ask a million questions later, but want to post already that this guide is beyond perfect! I have been struggling with Rhasspy all day and after finally making it work, all my questions about it are here. It is extremely concise and has the perfect examples that the Rhasspy documentation should have. Thank you so much! Keep the amazing work!

I copied the below automation to my automations.yaml

- id: Rhasspy TurnOnLight
  alias: Rhasspy TurnOnLight
  mode: single
  trigger:
    - platform: event
      event_data: {}
      event_type: rhasspy_TurnOnLight
  action:
    - service: light.turn_on
      entity_id: "{{ trigger.event.data.entity }}"
    - service: mqtt.publish
      data:
        topic: hermes/dialogueManager/endSession
        payload_template: '{"sessionId": "{{ trigger.event.data._intent.sessionId }}", "text": "OK, the  {{ trigger.event.data.entity_raw_value }} is on"}'

and get the following error on homeassistant:
“Invalid config for [automation]: not a valid value for dictionary value @ data[‘action’][0][‘entity_id’]. Got None. (See /config/configuration.yaml, line 21).”

how do i go about fixing this error or do i need to specify the entityid on the rhasspy sentence?

There seems to a missing line.

Try:

action:
  - service: light.turn_on
    data:
      entity_id: "{{ trigger.event.data.entity }}"

Or the (now more recommended) alternative format for specifying an entity_id with a service call:

action:
  - service: light.turn_on
    target:
      entity_id: "{{ trigger.event.data.entity }}"

After adding “target” I no longer get the error under notifications. However, it will not execute (turn on the entity) and in the logs I see the following:

It sounds like you are not passing the correct entity_id from Rhasspy for your lights.

What does the automation trace tell you?

Settings > Automations & Scenes > Find your automation in the list

Click on the overflow menu (3 buttons) > Traces

The trace should show you what service-call was passed to Home Assistant, and if you look on the ‘Changed Variables’ tab, you can see the event data that came in from Rhasspy to trigger the automation. Check if there is a value for trigger.event.data.entity or something similar, and either make the adjustments in Rhasspy to send the correct event data, or in Home Assistant to make sure it’s reading the value from the event trigger correctly.

This is the changed variables tab:

this:
  entity_id: automation.rhasspy_turnonlight
  state: 'on'
  attributes:
    last_triggered: '2022-09-17T14:04:06.667034+00:00'
    mode: single
    current: 0
    id: Rhasspy TurnOnLight
    friendly_name: Rhasspy TurnOnLight
  last_changed: '2022-09-17T13:56:23.320184+00:00'
  last_updated: '2022-09-17T14:04:06.671369+00:00'
  context:
    id: 01GD5W6ZMA4FXAWZYRQM7QNMTR
    parent_id: 01GD5W6ZMAFVABPHFQJQSF5GV6
    user_id: null
trigger:
  id: '0'
  idx: '0'
  alias: null
  platform: event
  event:
    event_type: rhasspy_TurnOnLight
    data:
      name: Porch Bulb
      name_raw_value: porch bulb
      _text: turn on the Porch Bulb
      _raw_text: turn on the porch bulb
      _intent:
        input: turn on the Porch Bulb
        intent:
          intentName: TurnOnLight
          confidenceScore: 1
        siteId: satellite
        id: de0a7683-c4d6-43bd-a8c3-5325de77023c
        slots:
          - entity: hass/entities
            value:
              kind: Unknown
              value: Porch Bulb
            slotName: name
            rawValue: porch bulb
            confidence: 1
            range:
              start: 12
              end: 22
              rawStart: 12
              rawEnd: 22
        sessionId: de0a7683-c4d6-43bd-a8c3-5325de77023c
        customData: null
        asrTokens:
          - - value: turn
              confidence: 1
              rangeStart: 0
              rangeEnd: 4
              time: null
            - value: 'on'
              confidence: 1
              rangeStart: 5
              rangeEnd: 7
              time: null
            - value: the
              confidence: 1
              rangeStart: 8
              rangeEnd: 11
              time: null
            - value: Porch
              confidence: 1
              rangeStart: 12
              rangeEnd: 17
              time: null
            - value: Bulb
              confidence: 1
              rangeStart: 18
              rangeEnd: 22
              time: null
        asrConfidence: null
        rawInput: turn on the porch bulb
        wakewordId: null
        lang: null
      _site_id: satellite
    origin: REMOTE
    time_fired: '2022-09-17T14:04:06.706754+00:00'
    context:
      id: 01GD5W6ZNJ19KEQKKVDBPGHSY2
      parent_id: null
      user_id: e74f429c97724ca7892fa7eeccbac84e
  description: event 'rhasspy_TurnOnLight'```

This looks to be the problem - I think Home Assistant is trying to pass ‘Porch Bulb’ as the value of trigger.event.data.entity to the service call light.turn_on, when it will need to be something like light.porch_bulb i.e. a ‘sluggified’ entity_id that HA will recognise (spaces replaced by underscores, no capital letters, and a domain prefix e.g. light., switch.) - you’ll find what the actual entity_id is by looking at Settings > Devices & Services > and choose the Entities tab.

You need to go back to your Rhasspy sentence and adjust what it sends as the value for ‘entity’ and which arrives at HA as trigger.event.data.entity - have a look again at Patrick’s post here for how he finesses the Rhasspy sentence so that it sends the correct value.

Ok thanks for your help, ill try again. I was using intents before which worked and the “porch bulb” worked. But using events and automations seemed easier and more straight forward so i switched over.

Thanks for all your help though! Much appreciated

@paddy0174 hey what about part 5? We are all waiting for its coming! Thanks in advance, and obviously also for the guide its self, very useful :slight_smile:

1 Like

I went a different route although there might of been some updates to Rhasspy along the way. Below is my intents/home_assistant.ini file. This was based on a github script I pulled following this YouTube tutorial for setting up Rhasspy in Home Assistant. By default it only added the light and switch domains. I added the others and everything just works! The only thing I had to fix was for any of the domains you have in the ini file must not contain special characters (ex: - ( or ) ect…) for friendly name or Rhasspy will throw an error when you try to train. I had to change a few as I had some friendly names with special characters. Mostly a - symbol in the friendly name but Rhasspy won’t train and throw an error due to the special characters, even if it’s in just one friendly name for that particular domain.

This way I don’t have to go the automation route. I have already written scripts to set groups of wifi lights to say, 60 percent. You can create groups in Zigbee2MQTT and they still have the light domain but not for wifi lights that I have setup in ESPHome (at least not that I am aware of). Now I just have to write scripts for group colors and light percentage for wifi lights in groups which should be straight forward. I just thought this would help as it was much more simple, at least to me personally. Obviously this doesn’t use MQTT (even though I have Rhasspy connected to MQTT) but I didn’t care about using MQTT but automations may be the only way if you do want to use MQTT.

The github script gets put into the share directory by default as I just ran the commands in the YouTube video linked and copy/pasted everything and added thee domains and took out any special characters for friendly name but I am sure you could place them anywhere. This way I can control the following domains (light, switch, group, media_player, and script) and when I add one to HA, I don’t have to touch Rhasspy, I just have to make sure the friendly name doesn’t have any special characters in it. You could get more creative witht he intents but right now I am fine with saying “turn on script.friendlyname on” or "turn the “group.friendlyname” off. Hope this helps someone because the automation route would have been more tedious in the long run, at least for me personally.

[HassTurnOn]
lights = $hass/entities,light
switches = $hass/entities,switch
groups = $hass/entities,group
mediaplayers = $hass/entities,media_player
scripts = $hass/entities,script
entities = <lights> | <switches> | <groups> | <mediaplayers> | <scripts>
turn on [the] (<entities>){name}

[HassTurnOff]
turn off [the] (<HassTurnOn.entities>){name}

[HassToggle]
entities = <HassTurnOn.entities>
toggle [the] (<entities>){name}

[HassLightSet]
set [the] (<HassTurnOn.lights>){name} [color] to ($hass/colors){color}
set [the] (<HassTurnOn.lights>){name} brightness to (0..100){brightness}
set [the] (<HassTurnOn.lights>){name} to (0..100){brightness} percent brightness

I’m trying to create a sentence with associated automation to pull the temperature from various rooms. Each room contains a temperature sensor.

My sentence is set up thus:

[GetTemperature]
whats the temperature in the (kitchen:sensor.kitchen_temperature | office:sensor.office_temperature | garage:sensor.garage_temperature){entity}

… and the HA automation is:

- id: Rhasspy GetTemperature
  alias: Rhasspy GetTemperature
  mode: single
  trigger: 
    - platform: event
      event_data: {}
      event_type: rhasspy_GetTemperature
  action:
    - service: mqtt.publish
      data:
        topic: hermes/dialogueManager/endSession
        payload_template: '{"sessionId": "{{ trigger.event.data._intent.sessionId }}", "text": "The temperature in the {{ trigger.event.data.entity_raw_value }} is {{ trigger.event.data.entity }} degrees."}'

… but an example of the spoken result is:
“The temperature in the kitchen is sensor.kitchen underscore temperature degrees”

… so it’s pulling the entity name but not its value (state? numerical state?)

I’ve tried using all sorts of variants of:

  • states.trigger.event.data.entity
  • states.trigger.event.data.entity.state
  • etc…
    … but to no avail.

Any assistance gratefully received.

Look like you have to “build” that. See if this helps Hassio Problem with concatenate rhasspy parameter to get climate temperature - Help - Rhasspy Voice Assistant

Hi OzGav. Thank you very much. That looked like it would work but, sadly, it didn’t. I tried all sorts of combinations. I think the issue is partly that my sensor doesn’t require the additional…

, 'current_temperature'

… part on the end. (Though, obviously I’ve tried it without).

It also seems to object to the…

state_attr

… bit.

Does anyone else have any ideas?

Hi Jersey. In my case, this setting is functional:
Sentence:

[GetTemperature]
whats the temperature (in | at | on) ($temperatures){entity}

Slots named “temperatures”:

guest room:sensor.xiron_7b02_temp
patio:sensor.xiron_7701_temp
greenhouse:sensor.xiron_6101_temp
the kitchen:sensor.xiron_6401_temp
living room:sensor.obyvacka_teplota
loft:sensor.povala_1

and the automation:

alias: Rhasspy GetTemperature
description: ""
trigger:
  - platform: event
    event_type: rhasspy_GetTemperature
condition: []
action:
  - service: mqtt.publish
    data:
      topic: hermes/dialogueManager/endSession
      payload_template: >-
        {"sessionId": "{{ trigger.event.data._intent.sessionId }}", "text":
        "OK, the temperature in the {{trigger.event.data.entity_raw_value}} is
        {{states(trigger.event.data.entity)|float(0)}}"}
mode: single

I hope it helps you.

You legend! That worked perfectly. Thank you so much Nortonko!
Thank you for taking the time to post. :slight_smile:

Hi @paddy0174 ,
thanks for sharing this great thread :blush:.
Just in case it might be useful in this context, i’ve created a technical walkthrough video on connecting Rhasspy with Home Assistant as part of my Youtube video series “Rhasspy from A-M”.

Can you make an example of this?

What I would like to do:
If garage door is open for 3 min,
Rhasspy asks if it should close it and wait for response,
Based on the response it should proceed.

Hi, thanks for the guide!
I’m getting stuck at the first part: The problem I am having is that although Rhasspy recognizes the command and the log shows it being sent over the websocket, it doesn’t seem to be received by Home Assistant; the automation is not triggered. I’m not sure where to look for the logs to check if Home Assistant is receiving anything from Rhasspy, but the Rhasspy log is as follows:

[DEBUG:2023-05-02 13:10:53,239] rhasspyserver_hermes: Sent 370 char(s) to websocket
[DEBUG:2023-05-02 13:10:53,238] rhasspyserver_hermes: Sent 370 char(s) to websocket
[DEBUG:2023-05-02 13:10:53,234] rhasspyserver_hermes: Handling NluIntent (topic=hermes/intent/GetTime, id=e11b525e-5ac5-4b4c-a610-1ab8534bd712)
[DEBUG:2023-05-02 13:10:53,233] rhasspyserver_hermes: <- NluIntent(input='what time is it', intent=Intent(intent_name='GetTime', confidence_score=1.0), site_id='Rhasspy', id='095c9e11-c8ac-4d79-87a2-2eb7ad7d6322', slots=[], session_id='095c9e11-c8ac-4d79-87a2-2eb7ad7d6322', custom_data=None, asr_tokens=[[AsrToken(value='what', confidence=1.0, range_start=0, range_end=4, time=None), AsrToken(value='time', confidence=1.0, range_start=5, range_end=9, time=None), AsrToken(value='is', confidence=1.0, range_start=10, range_end=12, time=None), AsrToken(value='it', confidence=1.0, range_start=13, range_end=15, time=None)]], asr_confidence=None, raw_input='what time is it', wakeword_id=None, lang=None)
[DEBUG:2023-05-02 13:10:53,158] rhasspyserver_hermes: Publishing 271 bytes(s) to hermes/nlu/query
[DEBUG:2023-05-02 13:10:53,157] rhasspyserver_hermes: -> NluQuery(input='what time is it', site_id='Rhasspy', id='095c9e11-c8ac-4d79-87a2-2eb7ad7d6322', intent_filter=None, session_id='095c9e11-c8ac-4d79-87a2-2eb7ad7d6322', wakeword_id=None, lang=None, custom_data=None, asr_confidence=None, custom_entities=None)

Does this mean there is a mistake in my MQTT configuration? I have the add-on running and have configured it in HA (left the basic configurations), the Rhasspy MQTT config is as follows:

Host: homeassistant.local
Port: 1833
Username: USER
Password: PASS

Any help would be greatly appreciated!

EDIT: When running the GetTime automation manually, I see the following errors in my HA log:

2023-05-02 16:58:40.510 ERROR (MainThread) [homeassistant.components.automation.rhasspy_gettime] Rhasspy GetTime: Error executing script. Error for call_service at pos 1: Error rendering data template: UndefinedError: 'dict object' has no attribute 'event'
2023-05-02 16:58:40.515 ERROR (MainThread) [homeassistant.components.automation.rhasspy_gettime] Error while executing automation automation.rhasspy_gettime: Error rendering data template: UndefinedError: 'dict object' has no attribute 'event'