Variables or Templeting: Can I optionally append to a dictionary?

I am trying to build a new mobile_app notification script. I am trying to find a way to avoid duplicating 80% of the code, so I can just have one single notification service call.

I have determined that I can build a simple variables data dictionary and pass that to the notification service.

sequence:
  - variables:
      data:
        subtitle: 'Alert'
        url: 'homeassistant://navigate/lovelace/cameras'

  - service: notify.brian
    data:
      title: 'Title'
      message: 'Testing message.'
      data: '{{ data }}'

Now, is there a way I can append, for example, entity_id: {{ camera }}', but only if pass a camera entity to my script?

You can by using choose. If a camera entity is passed, it uses the first choice in choose (which calls the notify service employing the camera entity) otherwise it uses the default choice (which calls the notify service without the camera entity).

This is what I was trying to avoid. I am trying to only call the notify service one time. I am tinkering with different approaches, but no luck so far.

Why? Is this a coding assignment where choose is outlawed? :slight_smile:

I assume you know that you can’t use a template to include/exclude a service’s options. A template is used to supply a value to an option, not determine its inclusion/exclusion.

1 Like

:grinning_face_with_smiling_eyes:

  1. I like to challenge myself.
  2. You can’t learn if you don’t ask the question. :wink:
  3. I am switching from iOS notifications to mobile_app notifications, and would like to minimize the chances for mistakes.
  4. I have been using choose in my iOS notifications, and I have 3 options: camera, critical, and default. However, I don’t have the option to combine critical and camera.

I’ve noticed that some of the properties like url are ignored if they are empty, but some don’t like entity_id (for the camera). So we have to resort to repeating the same identical lines of code in multiple choose statements.

I suspect this isn’t possible, but I thought it was worth posing the question anyway.

Sure, but it’s a fundamental aspect of Jinja2 templating that it’s for assigning values to options and not for altering an entity’s or a service call’s options. There’s not a single example in all of the documentation that even hints such a thing is possible.

1 Like

I found a much simpler way that works without quite as many hoops to jump through. This works with any combination I have tested so far. :slight_smile:

variables:
  .
  .
  camera: '{{ camera|default("none") }}'
  critical: '{{ critical|default(false) }}'
  push:
    category: |
      {%- if camera != "none" %}
        camera
      {%- else %}
        {{ default }}
      {%- endif %}
    sound:
      name: |
        {%- if sound is string %}
          {{ sound|default }}
        {%- elif critical %}
          Update.caf
        {%- elif category == "doorbell" %}
          media-source://media_source/local/audio/jetsons-doorbell.mp3
        {% else %}
          default
        {%- endif %}
      '{{ "critical" if critical else "disabled_critical" }}': 1
      '{{ "volume" if critical else "disabled_volume" }}': 1.0
sequence:
  - service: '{{ notification_service }}'
    data:
      title: '{{ title }}'
      message: '{{ message }}'
      data:
        subtitle: '{{ subtitle }}'
        url: '{{ url }}'
        entity_id: '{{ camera }}'
        push: '{{ push }}'

Sure, but that’s an example of assigning values to an option. It’s not an example of what you originally requested which was to conditionally append an option.

Yeah, but it ends achieving my ultimate goal of not having multiple calls to the notification service. I adapted to the limitations of the programming environment. I’m happy.

Ideally, that goal should have been revealed in the first post instead of asking for how to conditionally append options. Anyway, I’m glad you figured out how to get what you really wanted.