Push variables to script based on event_data

I’m completely stuck (on probably something very silly). I’m trying to push variables to a script. The values should be based on the fact if specific event_data has been sent into the automation:

If the service_data contains ‘brightness_pct’, then it should send value a, if not it should send value b.
I tried several approaches, but none of them seemed to work for me.

- id: 'lichtcorrectie'
  alias: 'Lichtcorrectie'
  initial_state: 'on'
  mode: 'parallel'
  trigger:
    platform: event
    event_type: call_service
    event_data:
      domain: light
      service: turn_on
  action:
    - choose:
      - conditions:
          - condition: template
            value_template: "{{ trigger.event.data.service_data.entity_id == 'light.mini_lampen' }}"
          - condition: template
            value_template: "{{ trigger.event.data.service_data.brightness_pct is not none }}"
        sequence:
          - service: script.turn_on
            data_template:
              entity_id: script.mini_verlichting_nieuwe_stand
              variables: >
                vertraging:{% if trigger.event.data.service_data.brightness_pct is defined %}3600{% else %}0{% endif %}
                verloopsnelheid:{% if trigger.event.data.service_data.brightness_pct is defined %}300{% else %}20{% endif %}

Ideally the figures (3600, 0, 300 and 20) are set at the top so I can just point use them as a template, but I also couldn’t get that working. Any help is much appreciated! :slight_smile:

The error I’m getting thrown is:

2021-01-31 14:37:26 ERROR (MainThread) [homeassistant.components.automation.lichtcorrectie] Lichtcorrectie: Choose at step 1: choice 5: Error executing script. Invalid data for call_service at pos 1: expected a dictionary for dictionary value @ data[‘variables’]
2021-01-31 14:37:26 ERROR (MainThread) [homeassistant.components.automation.lichtcorrectie] Lichtcorrectie: Error executing script. Invalid data for choose at pos 1: expected a dictionary for dictionary value @ data[‘variables’]
2021-01-31 14:37:26 ERROR (MainThread) [homeassistant.components.automation.lichtcorrectie] While executing automation automation.lichtcorrectie
Traceback (most recent call last):

Try this:

        sequence:
          - service: script.turn_on
            data:
              entity_id: script.mini_verlichting_nieuwe_stand
              variables: >
                vertraging: '{{ 3600 if trigger.event.data.service_data.brightness_pct is defined else 0 }}'
                verloopsnelheid: '{{ 300 if trigger.event.data.service_data.brightness_pct is defined else 20 }}'

Thanks.
Works fine! Not sure why I didn’t think of that approach. :slight_smile:

1 Like