Assistance creating a latching mailbox sensor that can clear itself

my new home has a mailbox on a hinge. the idea is I can open the front door of the mailbox and pull upward and the box will tilt to face me on the porch and let me reach in and pull out my mail. it is on ground level and the letter carrier delivers mail from the driveway height, not porch height. so if the letter carrier delivers mail, the mailbox door opens, no tilt occurs, the box closes whereas if I check the mail, the mailbox door opens, the box tilts up, the box tilts back down, the door closes.

I’m using a shelly BTHome window/door sensor. this sensor has. tilt sensor in it, but it only shows 0 when closed. when opened, that’s the reference point and any tilt past that is what it compares against to get its rotation value.

So my idea is triggering only the door sensor means mail was added, but triggering the door AND tilt meant I took mail out and should clear the you-have-mail state.

I’m struggling with this template. I cannot figure out how variables are supposed to work here.

description: ""
mode: single
triggers:
  - trigger: door.opened
    target:
      device_id: NOTSUREHOWIDENTIFIABLETHISWASSOIREDACTEDIT
    options:
      behavior: each
conditions: []
actions:
  - variables:
      wasTilted: false
  - repeat:
      until:
        - condition: door.is_closed
          target:
            entity_id: binary_sensor.outside_bthome_sensor_0903mailbox_sensor_window
          options:
            behavior: any
            for: "00:00:00"
      sequence:
        - if:
            - condition: numeric_state
              entity_id: sensor.outside_bthome_sensor_0903mailbox_sensor_rotation
              above: 5
          then:
            - variables:
                wasTilted: true
  - if:
      - condition: state
        entity_id: wasTilted
        state: true
    then:
      - action: input_boolean.turn_off
        metadata: {}
        target:
          entity_id: input_boolean.mailbox_status
        data: {}
    else:
      - action: input_boolean.turn_on
        metadata: {}
        target:
          entity_id: input_boolean.mailbox_status
        data: {}
alias: mailbox state changed

Please properly format your config.

f6be36681e0da431418ec7781fb6c62712941803


Have you considered using a Wait for Trigger with a timeout instead of whatever you are doing with that Repeat? If the wait times out without triggering for rotation, then you assume it was a delivery, if the rotation trigger fires you assume its you collecting. Or you could have no timeout and just use rotation and door closing as separate triggers for the Wait… if you think that makes more sense.

You've also got a bit of nonsense in there... but none of this is actually necessary for anything
  - if:
      - condition: state
        entity_id: wasTilted
        state: true
    then:

The variable wasTilted is not an entity ID… and what you have supplied isn’t even the value of that variable, it’s just the string “wasTilted”. You can test a variables truthiness using a Template condition. Since you are assigning either true or false as the value, all you need is the variable name inside expression delimiters:

  - if:
      - condition: template
        value_template: "{{ wasTilted }}"
    then:
alias: mailbox state changed
description: ""
mode: single
triggers:
  - trigger: door.opened
    target:
      entity_id: binary_sensor.outside_bthome_sensor_0903mailbox_sensor_window
    options:
      behavior: each
conditions: []
actions:
  - wait_for_trigger:
      - id: delivery
        trigger: door.closed
        target:
          entity_id: binary_sensor.outside_bthome_sensor_0903mailbox_sensor_window
        options:
          behavior: each
      - id: collection
        trigger: numeric_state
        entity_id: sensor.outside_bthome_sensor_0903mailbox_sensor_rotation
        above: 5
    timeout:
      minutes: 3
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ wait.trigger is none }}"
        sequence:
          - stop: Stopped due to Wait timeout
      - conditions:
          - condition: template
            value_template: "{{ wait.trigger.id == 'collection' }}"
        sequence:
          - action: input_boolean.turn_off
            metadata: {}
            target:
              entity_id: input_boolean.mailbox_status
            data: {}
    default:
      - action: input_boolean.turn_on
        metadata: {}
        target:
          entity_id: input_boolean.mailbox_status
        data: {}

got it, I hadn’t considered working the condition that way. I tweaked it a bit and ended up with this:

alias: mailbox state changed
description: “”
triggers:

  • trigger: door.opened
    target:
    entity_id: binary_sensor.outside_bthome_sensor_0903mailbox_sensor_window
    options:
    behavior: each
    conditions:
    actions:
  • wait_for_trigger:
    • id: delivery
      trigger: door.closed
      target:
      entity_id: binary_sensor.outside_bthome_sensor_0903mailbox_sensor_window
      options:
      behavior: each
    • id: collection
      trigger: numeric_state
      entity_id: sensor.outside_bthome_sensor_0903mailbox_sensor_rotation
      above: 5
      timeout:
      minutes: 3
  • if:
    • condition: numeric_state
      entity_id: sensor.outside_bthome_sensor_0903mailbox_sensor_rotation
      above: 5
      then:
    • action: input_boolean.turn_off
      metadata: {}
      target:
      entity_id: input_boolean.mailbox_status
      data: {}
      else:
    • action: input_boolean.turn_on
      metadata: {}
      target:
      entity_id: input_boolean.mailbox_status
      data: {}
      mode: single

Yet you post again without proper formatting… :thinking:

sorry, I see that. I’m not sure where the edit button if for me to fix it though.

It’s the pencil:

image