Exclude key with service_template

I’m trying to have this script turn on/off a lamp depending on a lux sensor. I wan’t it to only include brightness_pct when calling light.turn_on and exclude it when running light.turn_off, but right now I’m getting the following error:

[homeassistant.core] Invalid service data for light.turn_off: extra keys not allowed @ data['brightness_pct']. Got '40'

The config:

      - service_template: >-
          {% if states.sensor.stue_lux.state | float < 18000 %} light.turn_on {%- else -%} light.turn_off {% endif %}
        data_template:
          entity_id: light.panthella
          transition: 1800
          brightness_pct: 40 # How do I exclude this key with light.turn_off?

Did you get this to work? I’m trying to do exactly the same thing!

honestly this is one of those cases where it may just be best to break it into two scripts or automations. One to turn on the light and one to turn off the light.

Setting brightness to 0 using the turn_on service may turn it off.

What I’m trying to do is create a shutdown sequence for the lights. i.e. there are 4 lights and I want them to turn off one after another (fading out as they go). The only way to get the transition to work is to use the light.turn_on service. And when I use that, if one of the lights is already off, it will turn it on and start the fade out. I want the script to check if any of the lights are already off, and if they are then skip it from the shutdown sequence. If that makes sense…

EDIT: You cannot define different data in a service template. the data must be valid for each service defined in a service template. I would still suggest separating them out into separate automations and triggering/conditioning them accordingly.