Sensor with opening and closing state

Hello guys. I want to create a sensor that shows if a gate is closing or opening. For this I have 2 reed switches, one with the state when it’s open (binary_sensor.gate_open), another for when it’s closed (binary_sensor.gate_closed), and a binary sensor that shows when the gate is moving (binary_sensor.gate_moving).
My idea to create a template is based on the following, but I have no knowledge to do so:

  • binary_sensor.gate_moving is on and binary_sensor.gate_open goes from on to off = gate is closing
  • binary_sensor.gate_moving is on and binary_sensor.gate_closed goes from on to off = gate is opening

Is this possible? Is there another way to achieve this?

Thanks

A cover entity has opening and closing states. You can define a template cover using YAML configs.

Here’s an example that might help you, but it relies on using the existing state of itself to determine whether the movement is in the open or close direction. I haven’t checked if it lets you actually do this, but there are other options and they may depend on what entity(ies) you are actually controlling.

covers:
  three_sensor_gate:
    device_class: gate
    friendly_name: "Three-sensor Gate"
    value_template: >-
      {% if (is_state('binary_sensor.gate_open', 'on')) %} open
      {% elif (is_state('binary_sensor.gate_closed', 'on')) %} closed
      {% elif (is_state('binary_sensor.gate_moving', 'on') and is_state('cover.three_sensor_gate','open')) %} closing
      {% elif (is_state('binary_sensor.gate_moving', 'on') and is_state('cover.three_sensor_gate','closed')) %} opening
      {% else %} none
      {% endif %}
    open_cover:
      - service: switch.turn_on
        target:
          entity_id: switch.gate_open
    close_cover:
      - service: switch.turn_on
        target:
          entity_id: switch.gate_close
    stop_cover:
      - service: switch.turn_off
        target:
          entity_id: 
            - switch.gate_open
            - switch.gate_close
    icon_template: >-
      {% if states('cover.three_sensor_gate') == 'closed' %} mdi:gate-closed
      {% else %} mdi:gate
      {% endif %}