Trying to efficiently write automation using trigger.entity_id - can't seem to get syntax right

Disclaimer: HA newb trying to get first set of dimmers working the way I want…

I have several HS WD100+ dimmers, and I’m trying to build an automation that will work for each, without duplicating the same code throughout.
I used this thread to get it working on a single dimmer:
https://community.home-assistant.io/t/central-scene-command-class-fibaro-swipe-homeseer-ws-100d-switches/

I’m hoping to reuse the config to make life easier.
When I’m done, I’m expecting dbl tap up on any dimmer to set Brightness to 100% on that dimmer only.

This works for a single dimmer:

### - DIMMER Automation - ################
- alias: 'HSDimmer DBLTap Up'
#  hide_entity: True
  trigger:
    platform: event
    event_type: zwave.scene_activated
    event_data:
      entity_id: zwave.den_north_light
      scene_id: 1
      scene_data: 3
  action:
    service: light.turn_on
    entity_id: light.den_north_light_level
    data:
      brightness: 255

Trying to use trigger.entity_id so the action only runs for the dimmer that registered the event isn’t working. I’ve tried multiple variations of this:


### - DEN DIMMER Automations - NOT WORKING ################
- alias: 'HSDimmer DBLTap Up'
#  hide_entity: True
  trigger:
    platform: event
    event_type: zwave.scene_activated
    event_data:
      entity_id: zwave.den_north_light
      scene_id: 1
      scene_data: 3
  action:
    service: light.turn_on
    data_template:
      data:
        entity_id: >
          {% if trigger.entity_id == 'zwave.den_north_light' %}light.den_north_light_level
          {% elif trigger.entity_id == 'zwave.den_north_light_9' %}light.den_north_light_level
          {% elif trigger.entity_id == 'zwave.den_mid_light' %}light.den_mid_light_level
          {% elif trigger.entity_id == 'zwave.den_south_light' %}light.den_south_light_level
          {% elif trigger.entity_id == 'light.den_north_light_level' %}light.den_north_light_level
          {% elif trigger.entity_id == 'light.den_north_light_level_9_0' %}light.den_north_light_level
          {% elif trigger.entity_id == 'light.den_mid_light_level' %}light.den_mid_light_level
          {% elif trigger.entity_id == 'light.den_south_light_level' %}light.den_south_light_level
          {% else %}CatchAll
          {% endif %}
        brightness: 255

At this point, I’m just working on the action. I should be able to list all of my entity_id’s in the trigger… I’m only testing on one dimmer right now, but it’s not working.

Logs show:

2017-09-15 20:38:25 INFO (MainThread) [homeassistant.core] Bus:Handling <Event zwave.scene_activated[L]: entity_id=zwave.den_north_light, node_id=9, scene_data=3, scene_id=1>
2017-09-15 20:38:25 INFO (MainThread) [homeassistant.components.automation] Executing HSDimmer DBLTap Up
2017-09-15 20:38:25 INFO (MainThread) [homeassistant.core] Bus:Handling <Event logbook_entry[L]: entity_id=automation.hsdimmer_dbltap_up, name=HSDimmer DBLTap Up, domain=automation, message=has been triggered>
2017-09-15 20:38:25 INFO (MainThread) [homeassistant.helpers.script] Script HSDimmer DBLTap Up: Running script
2017-09-15 20:38:25 INFO (MainThread) [homeassistant.helpers.script] Script HSDimmer DBLTap Up: Executing step call service
2017-09-15 20:38:25 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: service_call_id=139799716724632-5, service=turn_on, domain=light, service_data=data=entity_id=CatchAll, brightness=255>
2017-09-15 20:38:25 ERROR (MainThread) [homeassistant.core] Invalid service data for light.turn_on: extra keys not allowed @ data['data']. Got {'entity_id': 'CatchAll', 'brightness': '255'}
2017-09-15 20:38:25 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=139799716724632-5>

I’d eventually like to use something like:

#        entity_id: >
#          {{'light.'+ trigger.entity_id.split('.')[1] +'_level' }}

But at this point, I’m not sure exactly what trigger.entity_id is set to… so I’m struggling to do anything with it!

Any assistant is greatly appreciated!

When I try:

..
  action:
    service: light.turn_on
    data_template:
      data:
        entity_id: '{{trigger.entity_id}}'

Logs show that the action’s “entity_id” is blank. :confounded:

Well, this is frustrating…
As per:

It seems that trigger.entity_id isn’t a valid template variable for an event!

Getting the conent of trigger.event, I see:
<Event zwave.scene_activated[L]: scene_data=3, scene_id=1, entity_id=zwave.den_north_light, node_id=9>
which contains the right entity_id… is there a way to parse that out?

This should work like you want:

- alias: 'HSDimmer DBLTap Up'
  trigger:
    platform: event
    event_type: zwave.scene_activated
    event_data:
      scene_id: 1
      scene_data: 3
  action:
    service: light.turn_on
    data_template:
      entity_id: '{{"light." + trigger.event.data.entity_id.split(".")[1] + "_level" }}'

This will trigger on all zwave.scene_activated events for any switch matching scene_id of 1 and scene_data of 3. You can then pull the entity_id for the switch that triggered it using trigger.event.data.entity_id and transform that to the light level entity needed for light.turn_on.

2 Likes

THANK YOU! That worked perfectly!