Conditionally omitting fields in template

I want to synchronize two dimmers, so that manually changing one will also change the other.

So, conditionally call light.turn_on or light.turn_off depending on whether another light is on or off. If that light is on, set brightness to the same. Trying to pass that brightness value is where things fall apart. light.turn_off doesn’t accept a brightness field and the call will fail outright if it’s in data.

So, brightness needs to be there when turning the light on, but not when turning the light off. I can’t figure out how to do this.

This is my best guess:

  data_template:
    {% if states.light.dragon_tech_in_wall_dimmer_level_26_0.state != "off" %}
        brightness: {{ states.light.dragon_tech_in_wall_dimmer_level_26_0.attributes.brightness | round(0) }}
    {% endif %}

But it fails to parse:

ERROR:homeassistant.util.yaml:while scanning for the next token
found character '%' that cannot start any token
  in "/home/roothorick/hass/automations.yaml", line 77, column 10

What am I missing?

I ran into this too. I don’t think you can conditionally pass data.

I exploited the fact that sending a brightness of zero effectively acts as a light.turn_off. This could break later, but it’s working for me now.