Multiple binary sensors for one trigger in an automation

I am trying to do an automation where I have all the windows grouped together in one automation. When one is opened, send a notification and tell which window was actually opened (I guess I’ll have problems if multiple sensors are opened at the same time). I can get it to somewhat work, but continues to have problems. Am I doing this correctly in calling multiple entity_id in one automation?

Ideally I want to also do a notify when a window is closed too, but if i take out “to: ‘on’” then the notifications go crazy - one a second or so. Maybe there is a way to say to only take action when the trigger has a state change.

Here is what I have tried with a little success, but still not working properly.

- alias: Window Open
  trigger:
    platform: state
    entity_id: binary_sensor.office_windows, binary_sensor.guest_windows, binary_sensor.basement_windows
    to: 'on'
  action:
  - service: notify.ios_iphone
    data:
     title: "Window Opened"
     message: |
      Date/Time: {{now().strftime("%a %b %d %H:%M:%S %p")}}
      {% if is_state('binary_sensor.office_windows', 'on') %}Office Windows Open.
      {% elif is_state('binary_sensor.guest_windows', 'on') %} Guest Windows Open.
      {% elif is_state('binary_sensor.basement_windows', 'on') %} Basement Windows Open.
      {% endif %}

I’ve literally just answered this situation in another thread :smile:

Obviously in your case it will be the sensors for the trigger and ios for the notify.

Perfect, thank you. I used the other one as a template and that solved my issue.

Now, do I need a separate automation to report on closures as well?

If I remove the to: ‘on’ section, the notifications go crazy.

For reference and others that may find this useful in the future, this is a working automation for a push notification when a window is opened:

  - alias: Window Open
    trigger:
      platform: state
      entity_id: 
        - binary_sensor.office_windows
        - binary_sensor.guest_windows
        - binary_sensor.basement_windows
      to: 'on'
    action:
    - service: notify.ios_iphone
      data_template:
       title: "Window Opened"
       message: |
        Date/Time: {{now().strftime("%a %b %d %H:%M:%S %p")}}
        {{ trigger.to_state.attributes.friendly_name }} has been opened.

Define ‘the notifications go crazy’?

If I remove the the “to: on” line, I get a notification about once a second telling me that that windows have been opened - while they are in fact closed and not tripped. Once I add “to: on” it back and reload the automation, things are back to normal. It would appear that I need a separate automation for “to: off”.

Perhaps I need to tell it an assumed initial state since it is alerting me thinking that a window is open when it is in fact open.

2018-04-23 17:21:14 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=notify, service=ios_iphone, service_data=title=Window Opened, message=Date/Time: Mon Apr 23 17:21:14 PM
Guest Windows has been opened., service_call_id=140698227492456-37>
2018-04-23 17:21:14 INFO (MainThread) [homeassistant.components.automation] Executing Window Open 2
2018-04-23 17:21:14 INFO (MainThread) [homeassistant.core] Bus:Handling <Event logbook_entry[L]: name=Window Open 2, message=has been triggered, domain=automation, entity_id=automation.window_open_2>
2018-04-23 17:21:14 INFO (MainThread) [homeassistant.helpers.script] Script Window Open 2: Executing step call service
2018-04-23 17:21:14 INFO (MainThread) [homeassistant.core] Bus:Handling <Event call_service[L]: domain=notify, service=ios_iphone, service_data=title=Window Opened, message=Date/Time: Mon Apr 23 17:21:14 PM
Office Windows has been opened., service_call_id=140698227492456-38>
2018-04-23 17:21:14 INFO (SyncWorker_0) [homeassistant.components.notify.ios] iOS push notification rate limits for iphone: 124 sent, 150 allowed, 0 errors, resets in 2:38:45
2018-04-23 17:21:14 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=140698227492456-37>
2018-04-23 17:21:14 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=automation.window_open_2, old_state=<state automation.window_open_2=on; last_triggered=2018-04-23T17:21:10.063478-04:00, friendly_name=Window Open 2 @ 2018-04-23T17:21:04.083806-04:00>, new_state=<state automation.window_open_2=on; last_triggered=2018-04-23T17:21:14.990736-04:00, friendly_name=Window Open 2 @ 2018-04-23T17:21:04.083806-04:00>>
2018-04-23 17:21:15 INFO (SyncWorker_4) [homeassistant.components.notify.ios] iOS push notification rate limits for iphone: 125 sent, 150 allowed, 0 errors, resets in 2:38:44
2018-04-23 17:21:15 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=140698227492456-36>
2018-04-23 17:21:15 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=automation.window_open_2, old_state=<state automation.window_open_2=on; last_triggered=2018-04-23T17:21:14.990736-04:00, friendly_name=Window Open 2 @ 2018-04-23T17:21:04.083806-04:00>, new_state=<state automation.window_open_2=on; last_triggered=2018-04-23T17:21:15.013556-04:00, friendly_name=Window Open 2 @ 2018-04-23T17:21:04.083806-04:00>>
2018-04-23 17:21:15 INFO (SyncWorker_2) [homeassistant.components.notify.ios] iOS push notification rate limits for iphone: 126 sent, 150 allowed, 0 errors, resets in 2:38:44
2018-04-23 17:21:15 INFO (MainThread) [homeassistant.core] Bus:Handling <Event service_executed[L]: service_call_id=140698227492456-38>
2018-04-23 17:21:15 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=automation.window_open_2, old_state=<state automation.window_open_2=on; last_triggered=2018-04-23T17:21:15.013556-04:00, friendly_name=Window Open 2 @ 2018-04-23T17:21:04.083806-04:00>, new_state=<state automation.window_open_2=on; last_triggered=2018-04-23T17:21:15.129446-04:00, friendly_name=Window Open 2 @ 2018-04-23T17:21:04.083806-04:00>>

That’s weird, surely if they’re binary sensors they can only be off or on, so any state change can only be one or the other :confused:

See how this goes…

 - alias: Window Open
    trigger:
      - platform: state
        entity_id: 
          - binary_sensor.office_windows
          - binary_sensor.guest_windows
          - binary_sensor.basement_windows
        to: 'on'
      - platform: state
        entity_id: 
          - binary_sensor.office_windows
          - binary_sensor.guest_windows
          - binary_sensor.basement_windows
        to: 'off' 
    action:
    - service: notify.ios_iphone
      data_template:
       title: "Window Opened"
       message: |
        Date/Time: {{now().strftime("%a %b %d %H:%M:%S %p")}}
        {{ trigger.to_state.attributes.friendly_name }} has been switched {{ trigger.to_state}} 

(we can make it say open/closed later)

1 Like

That does the trick. I agree that they are binary sensors so they can only be one or the other, and even without a state change it was acting up before.

Now, how do you make it say open/closed like you said we can figure out later? I see that trigger.to_state is a long string of ugly stuff for a notification. trigger.to_state.state is better but that says on/off instead of open/closed.

Yeah sorry, I missed the last .state off accidentally.

Something like…

message: >
  Date/Time: {{now().strftime("%a %b %d %H:%M:%S %p")}}
  {{ trigger.to_state.attributes.friendly_name }} has been
  {% if trigger.to_state.state == 'on' %} opened {% else %} closed {% endif %}.
1 Like

Awesome. Thanks!

1 Like