Help With Gate Notifcations

Trying to have a message sent to my phone and announcement via Alexa in the same automation. I get the push notification, but it says closed for both opening and closing the gate. Not sure what I am missing as I have something similar working fine for tracking devices in and out of zones. Any suggestions?

alias: Gate Notification
description: ''
trigger:
  - type: opened
    platform: device
    device_id: xxxxxx
    entity_id: binary_sensor.back_yard_gate
    domain: binary_sensor
  - type: not_opened
    platform: device
    device_id: xxxxx
    entity_id: binary_sensor.back_yard_gate
    domain: binary_sensor
condition: []
action:
  - device_id: xxxxx
    domain: mobile_app
    type: notify
    title: ''
    message: >-
      {% if trigger.event == "opened" %} The gate has been opened. {% else %}
      The back yard gate is now closed. {% endif %}
  - service: notify.alexa_media
    data:
      message: >-
        {% if trigger.event == "opened" %} 'The gate has been opened.' {% else
        %} 'The back yard gate is now closed.' {% endif %}
      data:
        type: tts
      target: media_player.echo_living_room
mode: queued
max: 10

You’re using device triggers. I don’t think they even create a trigger object, but if they do they certainly won’t have a .event method as there’s no event key in the trigger.

hi
maybe try 2 automations
1 for opened
1 for not_opened

trigger:
  - platform: state
    entity_id: binary_sensor.back_yard_gate
    to: opened
action:
  - service: notify.alexa_media
    data:
       message: >-
                       the gate has been opened

and i would write your template something like:

{% if is_state('device_tracker.paulus', 'home') %}
              Ha, Paulus is home!
            {% else %}
              Paulus is at {{ states('device_tracker.paulus') }}.
            {% endif %}

Doesn’t the binary sensor create an event when it changes its open/closed state?

I’ll give it a try. It’s weird though, I have a similar automation (below) working perfectly. The only difference it is triggered by zones.

alias: Mary’s Home Notification
description: ''
trigger:
  - platform: zone
    entity_id: device_tracker.mary_s_iphone_12_pro
    zone: zone.home
    event: enter
  - platform: zone
    entity_id: device_tracker.mary_s_iphone_12_pro
    zone: zone.home
    event: leave
condition:
  - type: is_not_open
    condition: device
    device_id: xxxxxxxxxxx
    entity_id: binary_sensor.front_door
    domain: binary_sensor
action:
  - device_id: xxxxxxxxx
    domain: mobile_app
    type: notify
    title: ''
    message: >-
      {% if trigger.event == "leave" %} Mary has departed home. {% else %} Mary
      has arrived home. {% endif %}
  - wait_for_trigger:
      - type: opened
        platform: device
        device_id: xxxxxxxx
        entity_id: binary_sensor.front_door
        domain: binary_sensor
  - service: notify.alexa_media
    data:
      message: >-
        ' {{ [ "Welcome home Mary, I hope you are having a great day.", "Welcome
        home Mary. How may I be of service?"] | random }} '
      data:
        type: tts
      target: media_player.echo_living_room
mode: queued
max: 10

Yes - but that’s not the event you’re triggering from, so it’s not part of the trigger object.

Put simply, device triggers/conditions/actions are for simple automations created with the ui only. If you want to do anything even slightly powerful in homeassistant (like use templates) you need to avoid them and use ‘proper’ triggers, conditions and actions.

You’ll also find that the code becomes much neater and easier to read.

alias: Gate Notification
trigger:
  platform: state
  entity_id: binary_sensor.back_yard_gate
  to:
    - 'on' 
    - 'off' 
action:
  - service: notify.mobile_app_XXXXX
    message: "The gate is {{ 'opened' if trigger.to_state.state == 'on' else 'closed' }}."
  - service: notify.alexa_media
    data:
      message: "The gate is {{ 'opened' if trigger.to_state.state == 'on' else 'closed' }}."
      target: media_player.echo_living_room 
      data:
        type: tts

It’s the ‘only’ difference, but it’s a very big difference. Zone triggers create a trigger object, which you need for the template in your action.

Oh that is way easier to read. Didn’t even realize this existed. I assume I put these in the automations.yaml?

You can do. If so, paste it like the others, so it will have a - before the word alias and then everything below needs it’s indentation preserved so it looks identical to the above, just another 2 spaces to the right each so it lines up with your other automations.

1 Like