Rhasspy event templates

So I spent quite a bit of time setting up rhasspy recently. I pulled from alot of different templates and examples and wanted to share here. Im using events and not intents for now. This was not as straight forward as I expected, and would love to know if anyone has done this in another/better way. I initially tried to use areas, but the implementation seems incomplete, so just went with groups.

my siteId’s are livingroom and bedroom

automations to catch rhasspy events:

- alias: Rhasspy Lights
  description: Voice Control on/off states for lights
  trigger:
  - event_data: {}
    event_type: rhasspy_Lights
    platform: event
  condition: []
  action:
  - alias: ''
    data_template:
      light_name: "{{ trigger.event.data.light_name }}"
      light_state: "{{ trigger.event.data.light_state }}"
      siteId: "{{ trigger.event.data._intent.siteId }}"
      light_group: "{{ trigger.event.data.light_group }}"
    service: script.rhasspy_lights

script to handle events:

rhasspy_lights:
  alias: rhasspy_lights
  fields:
    light_name:
      description: "light name"
      example: light lamp etc
    light_state:
      description: "State to change the switch to"
      example: on
    siteId:
      description: "rhasspy siteId"
      example: livingroom
    light_group:
      description: "light group"
      example: light.all_livingroom_light
  sequence:
    - service_template: >
        {% set light_state = light_state | string %}
        {% if light_state == 'on' %}
          light.turn_on
        {% elif light_state == 'off' %}
          light.turn_off
        {%elif light_state == 'toggle' %}
          light.toggle
        {% endif %}

      data_template:
        entity_id: >-
          {% set group_entity = "light."+light_group|string %}
          {% if group_entity in states|map(attribute='entity_id') %}
            {% set light_entity = group_entity %}
          {% elif light_group == 'all' %}
            {% set light_entity = ("light.all_"+siteId+"_light") | string %}
          {% else %}
            {% set light_entity = ("light."+siteId+"_"+light_name) | string %}
          {% endif %}
          {{light_entity}}

lights.yaml:

- platform: template
  lights:
    livingroom_lamp:
      friendly_name: livingroom_lamp
      unique_id: livingroom_lamp
      turn_on:
        service: switch.turn_on
        data:
          entity_id: switch.livingroom_lamp
      turn_off:
        service: switch.turn_off
        data:
          entity_id: switch.livingroom_lamp
    livingroom_light:
      friendly_name: livingroom_light
      unique_id: livingroom_light
      turn_on:
        - condition: state
          entity_id: light.livingroom_light
          state: 'off'
        - service: script.light_fan_livingroom
      turn_off:
        - condition: state
          entity_id: light.livingroom_light
          state: 'on'
        - service: script.light_fan_livingroom
    bedroom_light:
      friendly_name: bedroom_light
      unique_id: bedroom_light
      turn_on:
        - condition: state
          entity_id: light.bedroom_light
          state: 'off'
        - service: script.light_fan_bedroom
      turn_off:
        - condition: state
          entity_id: light.bedroom_light
          state: 'on'
        - service: script.light_fan_bedroom
    driveway_light:
      friendly_name: driveway_light
      unique_id: driveway_light
      turn_on:
        service: switch.turn_on
        data:
          entity_id: switch.driveway_light
      turn_off:
        service: switch.turn_off
        data:
          entity_id: switch.driveway_light
    porch_light:
      friendly_name: porch_light
      unique_id: porch_light
      turn_on:
        service: switch.turn_on
        data:
          entity_id: switch.porch_light
      turn_off:
        service: switch.turn_off
        data:
          entity_id: switch.porch_light

- platform: group
  name: all_livingroom_light
  entities:
    - light.livingroom_light
    - light.livingroom_lamp
- platform: group
  name: all_bedroom_light
  entities:
    - light.bedroom_light
- platform: group
  name: outside_light
  entities:
    - light.porch_light
    - light.driveway_light
- platform: group
  name: every_light
  entities:
    - light.porch_light
    - light.driveway_light
    - light.livingroom_light
    - light.livingroom_lamp
    - light.bedroom_light

relavant rhasspy sentance config:

[Lights]
state=(on|off|:toggle){light_state}
state2 = (activate:on | deactivate:off) {light_state}
#single lights
turn [the] ($lights) {lights_name} <state>
turn <state> [the] ($lights) {light_name} 
($lights) {light_name} <state>
<state2> ($lights) {light_name}
#single lights with location area
#turn [the] ($areas){area} ($lights) {lights_name} <state>
#turn <state> [the] ($areas){area} ($lights) {light_name} 
#($areas){area} ($lights) {light_name} <state>
#<state2> ($areas){area} ($lights) {light_name}
#light groups
turn ($light_groups){light_group} <state>
turn <state> [the] ($light_groups){light_group}
($light_groups){light_group} <state>
<state2> ($light_groups){light_group}

slot “$lights”:

(light):light
(lamp):lamp
(lights):light

slot “$light_groups”

(every light):every_light
(all lights|these lights):all
(all living room lights|living room lights):all_livingroom_light
(all outside lights|outside lights):outside_light
(all bedroom lights|bedroom lights):all_bedroom_light
1 Like

holy crap, this is sweet, thanks for posting this! I’m just getting rhasspy setup and this takes quite a bit of the legwork out of it for me! Not sure why this has zero replies, but it’s sure going to help me out! Thanks!

1 Like

Glad it helped someone. While I code fairly often for work, I never use jinja, and programing home assistant templates is often frustrating. I hope this removes some of the pain for you.