HA wth Rhasspy plugin minimal effort automation lights example no deep shit

Hi

It seems there is a missing link between installing the Rhasspy plugin and adding my lights to Home assistant. I am very great full to the community for making these two cool tools but I think the last 1% is missing. Once I enabled all the stuff in Rhasspy mic and speaker and used all the recommended stuff it took me WAY to long to connect the events generated in Rhasspy to HA. I was also not prepared to write multiple files different setting that I know would change later or would require maintaing.

This script will turn individual lights on and off or all the ground floor lights. When event come though from Rhasspy they will have a space in the name. This is stripped by the script.

In the sentences I have

[ChangeLightState]

lightname = ((ground floor | living room | hallway | back garden | dining room) {name}) | <ChangeLightColor.lightname>
light_state = (on | off) {state}
turn <lightstate> [the] <lightname> light
turn [the] <lightname> light <lightstate>
turn [the] <lightname> lights <lightstate>

Then in HA I have one automation.

alias: Change Lights
description: ''
trigger:
  - eventtype: rhasspyChangeLightState
    platform: event
condition:
  - condition: template
    value_template: '{{ ''name'' in trigger.event.data and ''state'' in trigger.event.data }}'
action:
  - servicetemplate: light.turn{{ trigger.event.data.state }}
    data_template:
      entity_id: >
        {% if trigger.event.data.name|replace(" ","") == 'groundfloor' %}
        light.diningroom,light.livingroom,light.hallway
        {% else %}
        light.{{ trigger.event.data.name|replace(" ","")}}
        {% endif %}

mode: single

You will most likely need to adjust your entities above in this example they were called

light.hallway
light.livingroom
light.diningroom
etc…

This saves writing a truck load of jinja2 for every light state. I am shocked this is not the default style of setup. As for jinja2 WTF who invited that language to the party?

good luck with your setups :slight_smile: and have fun.

It took me some time to figure those events out aswell.

I can see you are using groundfloor to turn multiple entities on/off. When adding an extra light, you have to change that as well.
It might be a good idea for you to create a group groundfloor in Home Assistant and turn that on/off instead. Saves some work.
Also you can get TTS feedback from Rhasspy too if you want.

Adding something like this to :

  - service: mqtt.publish
    data:
      topic: hermes/dialogueManager/endSession
      payload_template: '{"sessionId": "{{trigger.event.data._intent.sessionId}}",
        "text": "Ok, {{ trigger.event.data..name|replace(" ","") }} {{ trigger.event.data.state }}"}'

You could skip the text, but letting Rhasspy know what you did via endSession is good practise.
Otherwise Rhasspy goes back to ilde after a timout of 30sec. (You will probably see those timeouts in your Rhasspy log at the moment)

Thanks, I see you are using intents. I am using events, does this matter. When I tried enabling mqtt on the Rhasspy end it got caught in a boot loop and I had to reinstall the whole thing.

No, I am using events as well.
But the event.data has the _intent object which has the sessionId and siteId

I see I have made a typo too, it should be trigger.event.data.name|replace(" “,”")

Hello All,
I had quite some night spent on this topic and I don´t know if this can be useful but I want to share what I found out, this method don´t need to touch anything in Home Assistant (no automation or template to use).

In this way, it will be possible to add or remove light (or switches that so far I not tested in my case) without to touch any intent code or home assistant file.

What I did and actually works is:

  1. I did pull a list of the lights from home assistant by simply sunning in the developer page the following template:

{%- for state in states -%}
{{"\n"}}- {{state.entity_id}}
{%- endfor -%}

as result you get a long but sorted list similar to what below:

  • light.bathroom_main

  • light.bedroom

  • light.entrance

  • light.kitchen2

  • light.lamp_desk

  • light.lamp_mirror_l

  • light.lamp_mirror_r

  • light.lamp_relax

  • light.lamp_table

  • light.living_room_ceiling

  • light.mirror_lights

  • light.tv_top_light

  1. created a slot file “lights” that is later reference as “$lights” with the following format, note this is because we use:
  2. Send intents to Home Assistant (/api/intent/handle)
  3. HassTurnOn/Off pre-build intent in HA

(entrance):entrance
(relax):lamp_relax
(living room ceiling):living room ceiling
(kitchen):kitchen2
(desk):lamp_desk
(bathroom mirror):mirror lights
(table):lamp_table
(tv light):tv top light
(bathroom ceiling):bathroom main
(bedroom ceiling): bedroom

description:
(how you want to call the ligh):entity_name (example “light.tv_top_light” is the entity ID name is “tv top light” NO “light.” and in some cases without “_” because we want the entity name).

the two Rhasspy intents will be then something like:
[HassTurnOn]
(turn | switch) on [the] ($lights){name}
(turn | switch) the ($lights){name} on
($lights){name} on

[HassTurnOff]
(turn | switch) off [the] ($lights){name}
(turn | switch) the ($lights){name} off
($lights){name} off

Description:
($lights){name} we want in the Jason sent to HA a slot Name only with the Entity Name we pre set on the lights ("$lights") slot file, the intent sent to HA should look something like:

{
“entities”: [
{
“end”: 22,
“entity”: “name”,
“raw_end”: 17,
“raw_start”: 13,
“raw_value”: “desk”,
“start”: 13,
“value”: “lamp_desk”,
“value_details”: {
“kind”: “Unknown”,
“value”: “lamp_desk”
}
}

Please note “lamp_desk” is the real entity name in this case but in most of the case the entity name is edited without “_”, so if you get an error ("can´t fint entity … ") check the spelling in the slot file and this should fix the problem.

Hope this guide, can be useful for who like me did start with Rhasspy.

N.B. Home assistant will return the light status as soon it change, so we get a response like “desk is now on” each time we run the event and actually the light change status.

hope this helps