Automation template for different data values?

Hi everybody,

I am currently trying to connect rhasspy to my home assistant. My test automation is to switch on my office fan. This is the code in automations/rhasspy.yaml

- id: '<number>'
  alias: "turn on fan"
  trigger:
    platform: event
    event_type: rhasspy_ChangeSwitchState
    event_data:
      name: ventilator
      state: ein
  action:
    service: switch.turn_on
    entity_id: switch.arbeitszimmer_ventilator

- id: '<number>'
  alias: "turn off fan"
  trigger:
    platform: event
    event_type: rhasspy_ChangeSwitchState
    event_data:
      name: ventilator
      state: aus
  action:
    service: switch.turn_off
    entity_id: switch.arbeitszimmer_ventilator

How can I template both name and state? Let’s say I have these names: ventilator, computer, light, desk and states ein, aus. I’d then like to

  • if state: aus => service: switch.turn_off
  • if state: ein => service: switch.turn_on
  • if name: ventilator=> entity_id: switch.ventilator
  • if name: desk => entity_id: switch.desk
  • if name: light=> entity_id: switch.light
  • if name: computer => entity_id: switch.computer

So if I say okay rhasspy, turn on computer the automation will read on and computer that has been sent by rhasspy and will know the device and state to use. Currently I would have to copy this block of code for each device and each state, but I am sure automation template can do what I want. I am just not understanding the syntax enough.

I assume I can use to_state for the state, but since I am using German voice commands, it would create switch.turn_ein or switch.turn_aus instead of on/off - and that’s only if I manage to figure out the syntax.

My fan is currently named arbeitszimmer_ventilator, which I’d have to change to ventilator only in order for this to work. But that’d be fine with me. Or perhaps I’d call the device arbeitszimmer ventilator in rhasspy, but then home assistant would somehow have to add replace the empty space in the name payload with _.

I have read through automation templates but just don’t understand how to use them. Would anybody be so kind to help me out with a minimalistic script that works in my scenario and that I can build on?

Thanks in advance for your help :slight_smile:

If you name your switches based on the name that comes through the event…

  alias: "turn on fan"
  trigger:
    platform: event
    event_type: rhasspy_ChangeSwitchState
  condition:
    condition: template
    value_template: >
      {% set entity_id = 'switch.' + trigger.event.data.name %}
      {% set entity_exists = states | selectattr('entity_id','eq', entity_id) | list | length == 1 %}
      {{ trigger.event.data.state in ['ein','aus'] and entity_exists }}
  action:
    service_template: >
      switch.turn_{{ 'on' if trigger.event.data.state == 'ein' else 'off' }}
    data_template:
      entity_id: >
        switch.{{ trigger.event.data.name }}        

This is fully expandable, meaning that if you add another switch thats like your others, this automation will work without modification. It will not fire if the switch doesn’t exist, so make sure you name all your switches based on the name that is coming through the event.

2 Likes

Thank you! This works perfectly.

Is there a way to have different trigger.event.data.names and switch names? For example, I usually name my switches () , so office fan would be (OF) fan (or actually (AR) ventilator).

rhasspy will only submit ventilator, or, if I rename it, full name of the room and then ventilator. I could just name all devices by what they are, but it will makes things difficult if there are multiple devices of the same kind in different rooms (for example, two fans and two tvs), so it’d be better to somehow include the room name (or initials) in their name…?