Simple Script/Template to switch lights

Hi, I have around 5 lights in my set-up. Currently I have an ‘automation’ for each of them individually to turn them on and off -> 10 automations. This does not really scale.
Is there an easier way to create only two automations (on/off) and keep the light-name as a parameter, received from an rhasspy voice-command input event? How and where would I add that?

One exemplary trigger event is:
rhasspy_ChangeLightState

and in the event data:

name: kitchen
state: 'off'

I am using the hass.io user interface.

Can you just post a few of the automations so we can help you better.

Sure. Voice-command/Event comes in -> light turns on.
Currently I have one of these automations for each.Would like to have one for all.

- id: '1576359663938'
  alias: Kitchen lights ON
  description: ''
  trigger:
  - event_data:
      name: kitchen
      state: 'on'
    event_type: rhasspy_ChangeLightState
    platform: event
  - event_data: {}
    event_type: rhasspy_EnteringKitchen
    platform: event
  condition: []
  action:
  - entity_id: switch.kitchen_lights
    service: homeassistant.turn_on

how about posting the yaml please

Hi Petro, thanks for helping. I added the automation.yaml to above post, can you see it? Or did you mean another yaml? Here are some more entries. They are all the same format. Light on, Light off. I created an individual automation for each of these interactions, which seems like an overkill to me. I’d love to have something like

- id: '1576359581949'
  alias: Change Lights
  description: ''
  trigger:
  - event_data:
      name: {{light_name}}
      state: {{state}}
    event_type: rhasspy_ChangeLightState
    platform: event
  condition: []
  action:
  - entity_id: switch.{{light_name}}
    service: homeassistant.turn_{{state}}

Here is an excerpt from my automations (forget about the ‘entering kitchen’ part):

- id: '1576359464488'
  alias: side lights ON
  description: ''
  trigger:
  - event_data:
      name: side
      state: 'on'
    event_type: rhasspy_ChangeLightState
    platform: event
  condition: []
  action:
  - entity_id: switch.side_lights
    service: homeassistant.turn_on
- id: '1576359501624'
  alias: side lights OFF
  description: ''
  trigger:
  - event_data:
      name: side
      state: 'off'
    event_type: rhasspy_ChangeLightState
    platform: event
  condition: []
  action:
  - entity_id: switch.side_lights
    service: homeassistant.turn_off
- id: '1576359581949'
  alias: Window lights ON
  description: ''
  trigger:
  - event_data:
      name: window
      state: 'on'
    event_type: rhasspy_ChangeLightState
    platform: event
  condition: []
  action:
  - entity_id: switch.window_lights
    service: homeassistant.turn_on
- id: '1576359618484'
  alias: Window lights OFF
  description: ''
  trigger:
  - event_data:
      name: window
      state: 'off'
    event_type: rhasspy_ChangeLightState
    platform: event
  condition: []
  action:
  - alias: ''
    data: {}
    entity_id: switch.window_lights
    service: homeassistant.turn_off
- id: '1576359663938'
  alias: Kitchen lights ON
  description: ''
  trigger:
  - event_data:
      name: kitchen
      state: 'on'
    event_type: rhasspy_ChangeLightState
    platform: event
  - event_data: {}
    event_type: rhasspy_EnteringKitchen
    platform: event
  condition: []
  action:
  - entity_id: switch.kitchen_lights
    service: homeassistant.turn_on
- id: '1576359722421'
  alias: Kitchen lights OFF
  description: ''
  trigger:
  - event_data:
      name: kitchen
      state: 'off'
    event_type: rhasspy_ChangeLightState
    platform: event

Very close! This should get you what you are looking for.

- id: '1576359581949'
  alias: Change Lights
  description: ''
  trigger:
  - event_type: rhasspy_ChangeLightState
    platform: event
  condition:
  - condition: template
    value_template: "{{ 'name' in trigger.event.data and 'state' in trigger.event.data }}"
  action:
  - service_template: switch.turn_{{ trigger.event.data.state }}
    data_template:
      entity_id: switch.{{ trigger.event.data.name }}_lights

Take a look at the trigger object and it’s data (through templating).

Pretty much what we are doing here is triggering on ANY rhasspy_ChangeLightState event. But we only fire when ‘state’ and ‘name’ are inside the event data. Then we use ‘name’ and ‘state’ to call the correct switch. We need to use template fields to use templates. So that’s why it’s using service_template and data_template. You can’t put templates anywhere, the need to be called out. Also, you can’t just add _template to a field and expect it to work. It just so happens that service_template and data_template are valid templating fields.

1 Like

Wow Petro, I’m blown away! This works like a charm on the first try - copy & pasted.
Thank you also for the detailed explanation. Writing the value_template like this sounds very odd, because it is so ‘human readable’, that I first thought it was an explanation. Works fine.

1 Like