Trigger template with And/Or logic

Hi all,

this is my first post, so hopefully I do everything correct.
I’m trying to automate my physical mailbox and have added two doorsensors there.
The “mailbox_open” detects if the lid was opened to put something inside.
The “mailbox_emptied” detects if the mailbox-door was opened to clear it.
Important to know: To open the mailbox-door, you have to open the lid fist.

I tried to create a binary sensor “parcel delivered” which should become True when the lid is opened and stay true till the door was opened to empty the postbox.

I tried the following but the sensor is always false:

template:
  - trigger:
      - platform: state
        entity_id: 
        - binary_sensor.lumi_lumi_sensor_magnet_aq2_opening #Briefkasten geoeffnet
        - binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_2 #Briefkasten geleert
        to: 'on'
    sensor:
      - name: ParcelDelivered
        state: "{{ (is_state('this', 'True') or is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening', 'off')) and is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_opening_2', 'off') }}"

Has anyone an idea what I’m doing wrong?

It is not clear which contact sensor is which from your post so I will use stand in sensors that you can change:

template:
  - trigger:
      - id: 'true'
        platform: state
        entity_id: binary_sensor.mailbox_lid # change this to your mailbox lid sensor
        from: 'off'
        to: 'on'
      - id: 'false'
        platform: state
        entity_id: binary_sensor.mailbox_door # change this to your mailbox door sensor
        from: 'off'
        to: 'on'
    binary_sensor:
      - name: Parcel Delivered
        icon: "mdi:mailbox" # or whatever you want
        state: "{{ trigger.id }}"

Do not omit the from: 'off' part of the triggers. Otherwise a sensor that becomes unavailable then connects again could mess with the state.

Thanks a lot for the quick response. Works exactly as it should!

1 Like