Trying to create an Automation for multiple Sensors

Im trying to create a notification for multiple window sensors. But I keep running into a few errors which I think is an indentation error…im not sure.

Here is the code I cobbled together with help from google:

description: ""
mode: single
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.coralynwindow1_contact
      - binary_sensor.coralynwindow2_contact
      - binary_sensor.kevinwindow1_contact
    from: "off"
    to: "on"
actions:
  - action: notify.mobile_app_waltercell
    message: {{ trigger.to_state.attributes.friendly_name }} was {% if trigger.to_state.state == 'on' %} Open {% else %} Closed {% endif %}
alias: Window Open

Couple things to note if it helps: If I put the message line in quotes " {{trigger…}}" it passes all online yaml checks I can find. But I get a malformed error in HA. If I take out the quotes, It fails both online checks and HA checks with the error falling on line 15 Spot 63 which is the was in the message

Here is the code someone tried to help me with:

alias: Window Open
description: ""
triggers:
  - trigger: state
    entity_id:
      - binary_sensor.coralynwindow1_contact
      - binary_sensor.coralynwindow2_contact
      - binary_sensor.kevinwindow1_contact
    from: "off"
    to: "on"
actions:
  - action: notify.mobile_app_waltercell
      data:
        message: {{ trigger.to_state.attributes.friendly_name + 'was Open'}}
mode: single

This fails all checks with the malformed error.

If you want to put the template on the same line as the tag, you’ll need to put qoutes around them.

And this is wrongly indented, data should be at the same level as action:

Try:

actions:
  - action: notify.mobile_app_waltercell
    data:
      message: "{{ trigger.to_state.attributes.friendly_name + 'was Open'}}"

That was it. Someone else was trying to help me on facebook with it. I was mis-reading the code

I thought they were doing the ’ was open ’ part as a way to shorten viewing the code in facebook comments.

I just tried it with this exact code and it worked.

Thank you both!

1 Like

And it can be simplified:

message: "{{ trigger.to_state.attributes.name }} was {{ 'opened.' if trigger.to_state.state == 'on' else 'closed.' }}"

Also you need to change your trigger to occur for both opening and closing. This is the easiest way:

triggers:
  - trigger: state
    entity_id:
      - binary_sensor.coralynwindow1_contact
      - binary_sensor.coralynwindow2_contact
      - binary_sensor.kevinwindow1_contact
    not_to:
      - unavailable 
      - unknown 

Modifiying the trigger is a good idea, but in order to prevent it firing when the sensors returns from unavailable as well, use:

triggers:
  - trigger: state
    entity_id:
      - binary_sensor.coralynwindow1_contact
      - binary_sensor.coralynwindow2_contact
      - binary_sensor.kevinwindow1_contact
    not_to:
      - unavailable 
      - unknown 
    not_from:
      - unavailable
      - unknown
1 Like