Error: Invalid service data

I am seeing this error now:

ERROR (MainThread) [homeassistant.core] Invalid service data for light.turn_on: expected int for dictionary value @ data[‘brightness’]. Got ‘’

I am using an ISY994 and I see it as a result of this (and other) automations

- alias: Fix Livingroom 3-way
  hide_entity: True
  trigger:
    platform: state
    entity_id: light.living_room_lights
  action:
    service: homeassistant.turn_on
    entity_id:
        - light.living_room_lights_2
    data_template:
        brightness: '{{ trigger.to_state.attributes.brightness }}'

When I look at the web interface in HA, when the light is off, I do not see a brightness attribute on ‘light.living_room_lights’. When I turn it on, I see one pop up at 255. I think, what is happening is that sometimes the automation runs before the attribute actually updates. I believe the reverse sometimes actually happens.

I guess I could try implementing a small delay to see if that is really the problem. This worked before. I believe I was on .36 then.

Ok, I fixed my issue. I was only half right. The problem was when turning things off. I was actually backwards. The brightness attribute was being removed when the light is turned off and never is 0, so by the time the automation triggers, there is no brightness attribute. It looks like it was due to a recent change to the ISY component.

I updated my template:

brightness: '{{ trigger.to_state.attributes.brightness|default(0) }}'

This way, if brightness doesn’t exists, it will default to 0, which turns it off.