Toggle light with data (brightness) in single automation - template error

Trying to make a light toggle that includes data. This means that light.toggle cannot be used because it doesn’t accept data (except entity_id). So, light.turn_on and light.turn_off then. The last one also doesn’t accept data (except entity_id) so it requires variables. First to see which service to use (on or off), then to determine if data can be send along. Which in case of on could be brightness for example.

Code:

- id: '995'
  alias: Test
  trigger:
    platform: event
    event_type: xiaomi_aqara.click
    event_data:
      entity_id: binary_sensor.switch
      click_type: double
  action:
  - service_template: >-
      {% if is_state('light.test', 'off') %}
      {{ 'light.turn_on' }}
      {% else %}
      {{ 'light.turn_off' }}
      {% endif %}
    data_template: >-
      {% if is_state('light.test', 'off') %}
      entity_id: light.test
      brightness: 255
      {% else %}
      entity_id: light.test
      {% endif %}

The service part works correctly. The data part ruins something.
In /dev-template it seems to render as in mind. However, when checking the config:
Invalid config for [automation]: expected a dictionary for dictionary value @ data['action'][0]['data_template']. Got None. (See /config/configuration.yaml, line 423). Please check the docs at https://home-assistant.io/components/automation/

I tried lots of variants, which and without quotes, brackets, etc. etc. How to get this working?

What you are trying to do is not possible with in a template. Templates can only be applied to a single field, you cannot have multiple fields inside a template.

Next, you are going to run into issues with light.turn_off not accepting brightness as a field. So, you need to work around that. There’s 2 ways to handle this. 2 automations or 1 automation and 2 scripts. Personally, I think the 2 automations is easier to manage.

- alias: Test on
  trigger:
    platform: event
    event_type: xiaomi_aqara.click
    event_data:
      entity_id: binary_sensor.switch
      click_type: double
  condition:
    - condition: state
      entity_id: light.test
      state: 'off'
  action:
  - service: light.turn_on
    data:
      entity_id: light.test
      brightness: 255
- alias: Test off
  trigger:
    platform: event
    event_type: xiaomi_aqara.click
    event_data:
      entity_id: binary_sensor.switch
      click_type: double
  condition:
    - condition: state
      entity_id: light.test
      state: 'on'
  action:
  - service: light.turn_off
    data:
      entity_id: light.test

Thanks. I am coming from 2 automations and was hoping to combine them in one.

Do you have the example in case of using 2 scripts? Wonder how the scripts look. Calling them from automations shouldn’t be a problem.

Is this field thing a Home Assistant limitation or Jinja limitation? Thus in the first case a feature request might be something to try.

Jinja limitation as jinja only returns strings, not complex objects. A series of fields is referred to as a dictionary and it is a complex object.

The scripts are simple, nothing crazy, just what’s in your action section.

- alias: Test
  trigger:
    platform: event
    event_type: xiaomi_aqara.click
    event_data:
      entity_id: binary_sensor.switch
      click_type: double
  action:
  - service_template: >-
      script.test_light_{{ 'on' if is_state('light.test','off') else 'off' }}
script:
  test_light_on:
    sequence:
    - service: light.turn_on
      data:
        entity_id: light.test
        brightness: 255
  test_light_off:
    sequence:
    - service: light.turn_off
      data:
        entity_id: light.test

Thanks! Will try it out later.

I am not sure if this is a new capability, but you can turn off the light using “light.turn_on” with “brightness: 0”.

That way you only use one automation, and no scripts. Service_template is not needed either.