Help with data_template

I’m trying to have a script turn off/on a lamp based on a lux sensor, but the script below is returning extra keys to light.turn_off while I only want it to return the keys to light.turn_on.

The error: Invalid service data for light.turn_off: extra keys not allowed @ data['brightness_pct']. Got ''

How do I make the script only return “brightness_pct: 80” when calling the turn_on service?

  alias: Mere lys naar der bliver moerkt
  - service_template: >-
      {% if states.sensor.stue_lys.state | float < 16000 %}
      {% else %}
      {% endif %}
      entity_id: light.ph5
      transition: 10
      brightness_pct: >-
        {% if states.sensor.stue_lys.state | float < 16000 %}
        {%- endif %}

This might actually just be because when your sensor is below that value, you’re passing a blank brightness. Try getting rid of that second if statement as it looks like you’re not adjusting the brightness here, or add an else to it with some other brightness.

Also, I think you should replace your data: with data_template and then get rid of the then redundant data_template line.

Switch data and data_template