Template expert needed for automation puzzler!

I have plenty of Insteon scenes where multiple dimmer switches control a single load (virtual 3-way switches). One switch actually controls the electrical load, and the rest act as synchronized remotes.

To keep the indicators on all the dimmers in sync when turning on/off or dimming the load, I hide the non-load-bearing switches (and the scene itself) in my HA instance, and use automations to sync up the non-load-bearing switches when the state of the load-bearing switch changes.

Recently, I realized that this was causing excessive traffic, because not only do the automations run when I change the state of the device using HA, but also when I change it using a switch on the wall.

To address this, I want to change my automation triggers to use the “event” platform, instead of the “state” platform, so it will only trigger the automations when the HA instance changes the load, reducing unnecessary and redundant insteon traffic. The problem is, I can’t figure out how to pass brightness data to the action based on the ‘event’ trigger.

My old automation looked like this:

automation
  - alias: 'Dimmer Fix Kitchen Chandelier On'
    trigger:
      platform: state
      entity_id: light.kitchen_chand_1_by_thermosta
      to: 'on'
    action: 
      service: light.turn_on
      entity_id:
        - light.kitchen_chand_2_by_intercom
      data_template:
        brightness: '{{ trigger.to_state.attributes.brightness }}'

This worked like a charm - the data_template in the action picked up the brightness from the changed state, and passed it to the secondary dimmer switch to sync its indicators.

My new automation looks like this:

automation
  - alias: 'Dimmer Fix Kitchen Chandelier On'
    trigger:
      platform: event
      event_type: call_service
      event_data:
        service_data: 
          entity_id: light.kitchen_chand_1_by_thermosta
        domain: homeassistant
        service: turn_on
    action: 
      service: light.turn_on
      entity_id:
        - light.kitchen_chand_2_by_intercom
      data_template:
        brightness: '{{ trigger.event.service_data.brightness }}'

This doesn’t work so well. The triggers seem fine - it fires if I comment out the data_template lines, but I can’t figure out how to get the brightness passed to the action.

According to https://home-assistant.io/docs/automation/templating/, the available trigger data for the event platform includes trigger.event, and according to https://home-assistant.io/docs/configuration/events/, the call_service event should include service_data.brightness.

I’ve also tried: trigger.event.service_data.attributes.brightness
and changing the domain for the trigger to ‘light’ instead of ‘homeassistant’

Can anyone help me figure this out?

Thanks!

Some progress. It appears that the data_template string should be '{{ trigger.event.data.service_data.brightness }}' but for some reason, there’s something not working right in the automation trigger.

If I click the switch in the web interface to turn the light on, I see this in the logs (from a different light, but same script):

2017-06-29 22:24:23 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=light, service=turn_on, service_data=entity_id=['light.garage_stairs_1_top'], service_call_id=140527647609464-10>

This doesn’t include brightness data. It does, however, trigger the automation looking for event_type: call_service with this entity_id, but the automation fails because there is no brightness in the service_data (so it doesn’t even turn the other switch on at all).

If I click the switch name to bring up the slider for adjusting brightness, I see this in the log:

2017-06-29 22:24:29 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=light, service=turn_on, service_data=entity_id=light.garage_stairs_1_top, brightness=140, service_call_id=140527647609464-14>

This, however, fails to trigger the automation. The formatting on the service_data looks a bit weird to me, but it does look like there is a brightness value. Nevertheless, it doesn’t fire the trigger.

I suppose this means I should do an if/then kind of action, evaluating the service_data for a brightness value, which it passes if there is one, and doesn’t pass if there isn’t. But since the presence of a brightness value somehow stops the trigger from firing, that would only solve half the problem.

Still hopeful someone can chime in?

For any interested lurkers following along, I’ve noted some different behavior in the way v.0.47.1 handles input from the HA front end. I turned on the log at ‘info’ level for homeassistant.core and ran tail -f home-assistant.log | grep call_service

This is what I saw:

If click the light switch from on to off, both the homeassistant and light domain experience a service call:

2017-06-30 06:28:21 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=homeassistant, service=turn_off, service_data=entity_id=light.game_room_hall_1_by_door, service_call_id=140286323438200-69> 2017-06-30 06:28:21 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=light, service=turn_off, service_data=entity_id=['light.game_room_hall_1_by_door'], service_call_id=140286323438200-70>

If click the light switch from off to “full” on, I see the same behavior (both domains):

2017-06-30 06:27:42 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=homeassistant, service=turn_on, service_data=entity_id=light.game_room_hall_1_by_door, service_call_id=140286323438200-66> 2017-06-30 06:27:42 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=light, service=turn_on, service_data=entity_id=['light.game_room_hall_1_by_door'], service_call_id=140286323438200-67>

But, if I open the dimmer panel in the HA web interface and click the input slider when the light is off to turn it on to an intermediate brightness, only the light domain experiences a service call:

2017-06-30 06:27:07 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=light, service=turn_on, service_data=entity_id=light.game_room_hall_1_by_door, brightness=121, service_call_id=140286323438200-61>

The behavior is the same if I If click slider to adjust from one intermediate brightness to another brightness:

2017-06-30 06:28:40 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=light, service=turn_on, service_data=entity_id=light.game_room_hall_1_by_door, brightness=164, service_call_id=140286323438200-74>

I wonder if this is by design? In any event, because of the different formatting of the latter two service calls in the light domain (with service_data=entity_id=light.game_room_hall_1_by_door instead of service_data=entity_id=['light.game_room_hall_1_by_door']), my automations won’t trigger on those behaviors.