Can someone help me out with the conditions section of an automation (I don't know the proper way to write it)

Hello all,

Can someone show me how to write the below conditions section properly. I don’t think I have done it right but don’t really know what is the proper way.

What I am attempting to do is disarm the alarm only when the “trigger.event.data.entity_id” matches one of the 3 below and only when the “trigger.event.data.actioin_text” is “Keypad unlock operation” and the last condition is only do this when the alarm is not already disarmed.

The thing I’m struggling with is are these all supposed to be nested differently? I don’t think what I have done is correct but maybe it is. Hoping someone can teach me a bit.

Thank you all for all of the help you have given me!!

alias: '#Alarm- Disarm with Door Code Entry'
description: ''
trigger:
  - platform: event
    event_type: zwave_js_notification
condition:
  - condition: or
    conditions:
      - condition: template
        value_template: '{{ trigger.event.data.entity_id == ''lock.garage_entry_door_lock'' }}'
      - condition: template
        value_template: '{{ trigger.event.data.entity_id == ''lock.front_door_lock'' }}'
      - condition: template
        value_template: '{{ trigger.event.data.entity_id == ''lock.back_door_lock'' }}'
  - condition: and
    conditions:
      - condition: template
        value_template: '{{ trigger.event.data.actioin_text == "Keypad unlock operation" }}'
  - condition: not
    conditions:
      - condition: state
        entity_id: alarm_control_panel.alarmo
        state: Disarmed
action:
  - service: alarm_control_panel.alarm_disarm
    data:
      code: 'XXXX'
    target:
      entity_id: alarm_control_panel.alarmo
  - service: notify.parents
    data:
      title: HA Alarm Status
      message: The alarm has been disarmed by {{trigger.event.data.code_slot_name}}.
mode: single

Triggers are and by default, so you can drop the and piece to make it just:

condition:
  - condition: or
    conditions:
      - condition: template
        value_template: '{{ trigger.event.data.entity_id == ''lock.garage_entry_door_lock'' }}'
      - condition: template
        value_template: '{{ trigger.event.data.entity_id == ''lock.front_door_lock'' }}'
      - condition: template
        value_template: '{{ trigger.event.data.entity_id == ''lock.back_door_lock'' }}'
  - condition: template
    value_template: '{{ trigger.event.data.action_text == "Keypad unlock operation" }}'
  - condition: not
    conditions:
      - condition: state
        entity_id: alarm_control_panel.alarmo
        state: Disarmed

(I also fixed a likely typo in actioin)

You can also simplify that big or block

      - condition: template
        value_template: '{{ trigger.event.data.entity_id == ( ''lock.garage_entry_door_lock'' or ''lock.front_door_lock'' or ''lock.back_door_lock'') }}'

If you’re interested, here’s the equivalent using Template Conditions in shorthand notation.

alias: '#Alarm- Disarm with Door Code Entry'
description: ''
trigger:
  - platform: event
    event_type: zwave_js_notification
condition:
  - "{{ trigger.event.data.entity_id in ['lock.garage_entry_door_lock', 'lock.front_door_lock', 'lock.back_door_lock'] }}"
  - "{{ trigger.event.data.action_text == 'Keypad unlock operation' }}"
  - "{{ states('alarm_control_panel.alarmo') != 'Disarmed' }}"
action:
  - service: alarm_control_panel.alarm_disarm
    data:
      code: 'XXXX'
    target:
      entity_id: alarm_control_panel.alarmo
  - service: notify.parents
    data:
      title: HA Alarm Status
      message: 'The alarm has been disarmed by {{trigger.event.data.code_slot_name}}.'
mode: single

Would it not be easier to do this:

- alias: Unlock Disarm
  id: unlock_disarm
  trigger:
    platform: state
    entity_id: 
      - lock.garage_entry_door_lock
      - lock.front_door_lock
      - lock.back_door_lock
    to: "unlocked"

  condition:
    - condition: or
      conditions:
        - condition: state
          entity_id: alarm_control_panel.alarmo
          state: "armed_home"
        - condition: state
          entity_id: alarm_control_panel.alarmo
          state: "armed_away"

  action:
    - service: alarm_control_panel.alarm_disarm
      entity_id: alarm_control_panel.alarmo
      data:
        code: !secret alarm_code
    - service: notify.adult_phones
      data:
        message: "House alarm disarmed."

Ahh, I remember reading that. It just didn’t trigger in my mind as I was attempting to write that one. Thank you for helping me with this @Tinkerer

Most definitely a typo

This is great. I like it. Thank you for showing me ways to make the scripting/ coding more efficient. Where I don’t know alot about the templating yet, I feel like I’m using a sledgehammer to build a piano a good amount of time.

@123

Thank you for showing me this. You always show me things that are a little more advanced and I appreciate it.

I take it the “!” is shorthand for “not”?

@firstof9

Thank you for your response here and your help with other things here lately.

I would say it very well could be. I think I was so involved in this new “event” concept that I just now starting using/ learning about that I didn’t even think of combining my old knowledge with that. Thank you for opening my eyes/ brain a bit.

Thank you for showing me “!secret alarm_code”, I didn’t think about doing that.

1 Like

My method works for me as I have a 2nd automation setup to send out lock notifications.

Yeah, that is essentially what I’m doing. I am setting the notification one up as we speak.

1 Like