Trigger tenplate binary sensor flip/flop

I can sort of see that the following is a bad idea, but I can’t see why it doesn’t work. Each element works on its own, but the two combined do not.

I have two door sensors on my garage door. One for when it’s open(ed) and the other for when it’s closed. By having two (the closed one has a replaced relay from NC to NO). I can ensure that it’s closed and not that it’s simply “not open” (IYSWIM). So, I created a pair of trigger template sensors each pointing at the same binary sensor. Each works fine in isolation, but as soon as there are two present, the sensor won’t change.

# Trigger Binary Sensors for Garage Door status
  # First is trigger for closed
  - trigger:
      - platform: state
        entity_id: binary_sensor.10006f1d21_1
        from: "off"
        to: "on"
    binary_sensor:
      - name: "Garage Door Status"
        unique_id: garage_door_status
        device_class: garage_door
        state: "off"
  # Next is trigger for open
  - trigger:
      - platform: state
        entity_id: binary_sensor.10006f1d21_4
        from: "off"
        to: "on"
    binary_sensor:
      - name: "Garage Door Status"
        unique_id: garage_door_status
        device_class: garage_door
        state: "on"

I could just change this to an automation, I suppose, but I understood that using trigger templates were preferred these days?

I suggest the following:


template:

  - trigger:
      - platform: state
        entity_id: 
        - binary_sensor.10006f1d21_1  ## = off/closed
        - binary_sensor.10006f1d21_4 ## = on/open
        from: 'off'
        to: 'on'
    binary_sensor:
      - name: 'Garage Door Status'
        unique_id: garage_door_status
        state: |
          {% set gon = trigger.entity_id == 'binary_sensor.10006f1d21_4' %}
          {{ 'on' if gon else 'off' }}
        device_class: garage_door

Thank you very much. I will try that.

May I ask why my version didn’t work? I fully understand that it’s much more long-winded, but that doesn’t explain why it simply didn’t work. Both elements were correct, as far as I could test, but they simply didn’t work in concert.

Thank you again

You attempted to configure two entities with the exact same name and unique_id. That’s not allowed.

I can only speculate. It could be that two independent sensors are conflicting with each other.

Tomatoes on my eyes. taras has sharper ones.

Ok, I had assumed it was something like that. My (incorrect) interpretation was that I was trying to modify the same one, of course.

Thank you for taking the time to reply. It is most helpful.

Is a trigger really needed? If you create a simpler template sensor it will always (and only) be calculated when one of the sensors changes it states, no?

template:
  - binary_sensor:
      - name: 'Garage Door Status'
        unique_id: garage_door_status
        state: "{{ iif(is_state('binary_sensor.10006f1d21_4', 'on'), 'on', iif(is_state('binary_sensor.10006f1d21_1', 'on'), 'off', 'unknown')) }}"
        device_class: garage_door

I’m editing directly here at the forum and couldn’t test this, but I’m pretty sure it will give the results you want (if I didn’t mixed the sensor states).

I’m very much a newbie here and had assumed that a trigger was needed as the state wss effectively being triggered.

This is now mostly working, except…

I have hit a snag in that when the sensors trigger, they stay in the “on” state for quite some time. Thus, it’s possible to open the door and then close it again and the “open” sensor still shows as “on”. This compromises the recording of closure (and presumably vice versa)

Is the duration of the trigger configurable in any way? I assume it’s a HA setting rather than the source (Sonoff) device.

Thanks,