Syntax for string concatenation with IF conditions

I want to construct a notification message which consists of a series of strings, depending on the state of a list of sensors. So for each sensor that is set to “on” I want to add a piece of text to the end of the message.
What I am trying to use is this:

notification_message: Motion Detected in {{% if is_state('binary_sensor.sonoff_pir_hall', 'on') %} "Hall "} {{% if is_state('binary_sensor.sonoff_pir_lounge', 'on') %} "Lounge "} {{% if is_state('binary_sensor.sonoff_pir_study', 'on') %} "Study "} {{% if is_state('binary_sensor.sonoff_pir_kitchen', 'on') %} "Kitchen"}

…but the syntax is being rejected. Is what I am trying to do achievable?

I recommend taking a look at the templating docs here and the Jinja docs linked from there. To start, {{ }} is an “output” expression and {% %} is for logic. You have {{% and some assorted }, which is nothing :slight_smile: .

This is one way to do what you want:

notification_message: >-
  {% set sensors = ['binary_sensor.sonoff_pir_hall', 'binary_sensor.sonoff_pir_lounge', 'binary_sensor.sonoff_pir_study', 'binary_sensor.sonoff_pir_kitchen'] %}
  Motion Detected in {{ states|selectattr("entity_id", "in", sensors)|selectattr("state", "==", "on")|map(attribute="name")|list|join(", ") }}
1 Like

Another alternative is to use Automation Trigger Variable as your entity_id seems to be a motion sensor. Therefore, the likelihood of more than one binary_sensor to be triggered at the same time is quite low (in my experience).

If you are using Automation Editor UI, you can copy and paste the following YAML-

trigger:
  - platform: state
    to: 'on'
    entity_id: binary_sensor.sonoff_pir_hall
  - platform: state
    to: 'on'
    entity_id: binary_sensor.sonoff_pir_lounge
  - platform: state
    to: 'on'
    entity_id: binary_sensor.sonoff_pir_study
  - platform: state
    to: 'on'
    entity_id: binary_sensor.sonoff_pir_kitchen
condition: []
action:
  - service: notify.mobile_app_
    data:
      message: >-
        Motion is detected in {{ trigger.entity_id.attributes.friendly_name }}.

Yep. Or even:

trigger:
  - platform: state
    to: 'on'
    entity_id:
      - binary_sensor.sonoff_pir_hall
      - binary_sensor.sonoff_pir_lounge
      - binary_sensor.sonoff_pir_study
      - binary_sensor.sonoff_pir_kitchen
action:
  - service: notify.mobile_app_
    data:
      message: >-
        Motion is detected in {{ trigger.to_state.name }}.

Both of these solutions spit out the name/friendly_name of the motion sensor entity, which may not be what the OP wants. A loop could be used to index into a list of other terms based on the motion sensor entity_id, but I can’t find an elegant way to do it and I’ll leave it as an exercise to the reader :).

1 Like

For the second example that you posted, does it work if it pasted over in Automation UI YAML? Sometimes the editor can mess with YAML list (switching from Edit in YAML —> Edit in Visual Editor —> Save)

Yep, works for me. I can save it and view it in GUI mode, where it turns the list into a comma-separated list.

Good to know! Thanks!

Thanks for all the feedback. I decided to try Rob’s solution, as it seems to be exactly what I was looking for, but I get this error:

Blueprint Send actionable notifications for Android generated invalid automation with inputs OrderedDict([('notify_device', '9297be7c81e31e24c55f8dc5de467174'), ('trigger_entity', 'input_boolean.alarm_triggered'), ('notification_title', 'Home Alarm Triggered'), ('action_1_uri', '/lovelace/cameras'), ('first_action', [OrderedDict([('service', 'input_boolean.turn_off'), ('target', OrderedDict([('entity_id', 'input_boolean.alarm_triggered')]))])]), ('action_1_title', 'View Cameras'), ('notification_message', '{% set sensors = [\'binary_sensor.sonoff_pir_hall\', \'binary_sensor.sonoff_pir_lounge\', \'binary_sensor.sonoff_pir_study\', \'binary_sensor.sonoff_pir_kitchen\'] %} Motion Detected in {{ states|selectattr("entity_id", "in", sensors)|selectattr("state", "==", "on")|map(attribute="name")|list|join(", ") }} # notification_message: \'{% if is_state(\'\'binary_sensor.sonoff_pir_hall\'\', \'\'on\'\') # %} "Motion Detected in Hall" {% elif is_state(\'\'binary_sensor.sonoff_pir_lounge\'\', # \'\'on\'\') %} "Motion Detected in Lounge" {% elif is_state(\'\'binary_sensor.sonoff_pir_study\'\', # \'\'on\'\') %} "Motion Detected in Study" {% elif is_state(\'\'binary_sensor.sonoff_pir_kitchen\'\', # \'\'on\'\') %} "Motion Detected in Kitchen" {% else %} "Motion Detected" {% endif # %}\'')]): invalid template (TemplateSyntaxError: expected token ',', got 'binary_sensor') for dictionary value @ data['variables']['notification_message']. Got None

This is the full automation definition:

- id: '1628632963539'
  alias: Alarm triggered - Send actionable notifications for Android
  description: ''
  use_blueprint:
    path: vorion/actionable-notifications-for-android.yaml
    input:
      notify_device: 9297be7c81e31e24c55f8dc5de467174
      trigger_entity: input_boolean.alarm_triggered
      notification_title: Home Alarm Triggered
      action_1_uri: /lovelace/cameras
      first_action:
      - service: input_boolean.turn_off
        target:
          entity_id: input_boolean.alarm_triggered
      action_1_title: View Cameras
      notification_message: >-
        {% set sensors = ['binary_sensor.sonoff_pir_hall', 'binary_sensor.sonoff_pir_lounge', 'binary_sensor.sonoff_pir_study', 'binary_sensor.sonoff_pir_kitchen'] %} Motion Detected in {{ states|selectattr("entity_id", "in", sensors)|selectattr("state", "==", "on")|map(attribute="name")|list|join(", ") }}

Any ideas?

Not really. I tested the template and it works, so it seems like something with the blueprint.

That’s a shame. I’ve never used a blueprint before, but it was the only way that I could get actionable notifications to work :0(

Blueprints just generate YAML, and I’m sure that it’s possible to use a template for the notification. Generate the YAML for a normal notification message and then modify it manually.

How do I generate from the Blueprint? I don’t see that option anywhere - either in the Blueprint screen or in the automation that I generated from it. The automation just generates the “blueprint:” section in my automations.yaml when I save it
???