Cover template - need to read old/from state

Hi, I have a cover that represents my front gate - on the backend it is powered by Shelly Uni module. Here’s the current, working code:

    front_gate:
      device_class: gate
      friendly_name: "Front Gate"
      unique_id: front_gate
      optimistic: false
      value_template: >
        {% if is_state('binary_sensor.shelly_uni_front_gate_drive_channel_1_input', 'off') and is_state('binary_sensor.shelly_uni_front_gate_drive_channel_2_input', 'off') %}
          closed
        {% elif is_state('binary_sensor.shelly_uni_front_gate_drive_channel_1_input', 'on') and is_state('binary_sensor.shelly_uni_front_gate_drive_channel_2_input', 'on') %}
          open
        {% elif NEED_SOME_HELP_HERE %}
          opening
        {% elif NEED_SOME_HELP_HERE %}
          closing
        {% endif %}
      open_cover:
        - condition: state
          entity_id: cover.front_gate
          state: "closed"
        - service: switch.turn_on
          target:
            entity_id: switch.shelly_uni_front_gate_drive_channel_2
      close_cover:
        - condition: state
          entity_id: cover.front_gate
          state: "open"
        - service: switch.turn_on
          target:
            entity_id: switch.shelly_uni_front_gate_drive_channel_2
      stop_cover:
        - service: "switch.turn_{{ 'on' if 'ing' in states('cover.front_gate') else 'off' }}"
          target:
            entity_id: switch.shelly_uni_front_gate_drive_channel_2
      icon_template: >-
        {% if states('cover.front_gate') | float(0) > 0 %}
          mdi:gate-open
        {% else %}
          mdi:gate
        {% endif %}
      availability_template: "{{ states('cover.front_gate') not in ['unavailable', 'unknown'] }}"

This template works very nicely and my gate state is calculated based on inputs from Shelly Uni, and is working great for closed and opened states. But to be able to assess if the gate is opening/closing, I need to access previous states of these sensors:

binary_sensor.shelly_uni_front_gate_drive_channel_1_input
binary_sensor.shelly_uni_front_gate_drive_channel_2_input

As a workaround, I created some functions in NodeRed, where it is very easy to use old/from states to make decisions:

  if (msg.data.old_state.state == "on" && msg.data.new_state.state == "off") {
      if (msg.data2.state == "on") {
          newMsg.payload = "closing"    
          return newMsg;
      }
  }

based on the result I use a intermediate sensor to know if the gate is opening or closing. Is there any simple way to read from/old state in my template? I’d like to make this device HA-native without need to use intermediate sensors and NodeRed.

You can use this.state to access the previous state.

Could you give me a hint or a little bit more context on how to use it?

What I need is to have a new and previous value of:

binary_sensor.shelly_uni_front_gate_drive_channel_1_input

I guess this variable will refer to cover.front_gate device, right?

if you need the previous state of binary_sensor.shelly_uni_front_gate_drive_channel_1_input then you need to make a template sensor using a state change trigger.

template:
- trigger:
  - platform: state
    entity_id: 
    - binary_sensor.shelly_uni_front_gate_drive_channel_1_input
    - binary_sensor.shelly_uni_front_gate_drive_channel_2_input
  action:
  - variables:
      channel1: >
        {% set channel = 'binary_sensor.shelly_uni_front_gate_drive_channel_1_input' %}
        {% if trigger.entity_id == channel  %}
          {{ {'from': trigger.from_state.state | bool, 'to': trigger.to_state.state | bool} }}
        {% else %}
          {% set current = states(channel) | bool %}
          {{ {'from': current, 'to': current } }}
        {% endif %}
      channel2: >
        {% set channel = 'binary_sensor.shelly_uni_front_gate_drive_channel_2_input' %}
        {% if trigger.entity_id == channel  %}
          {{ {'from': trigger.from_state.state | bool, 'to': trigger.to_state.state | bool} }}
        {% else %}
          {% set current = states(channel) | bool %}
          {{ {'from': current, 'to': current } }}
        {% endif %}
  sensor:
  - name: blah
    state:
      {% if not channel1.to and not channel2.to %}
        closed
      {% elif channel1.to and channel2.to %}
        open
      {% add whatever logic you want here using channel1.to or channel1.from or channel2.to or channel1.from %}
      {% endif %}

Then just use the output of that sensor as your value_template for the cover. Keep in mind that .to and .from are true/false, true being on false being off. You don’t need to use == because the if true will be true and if false will be false. to check for false, do if not channel1.to and vise versa to check for true if channel1.to

Ok, thanks for your help.

Maybe not the way I expected, but you gave me a great hint on how this.state could solve my problem. I tried very hard to stick to reading sensor values to assess gate state, and for open/closed is very simple:

open:
channel_1_input = 1
channel_2_input = 1

closed:
channel_1_input = 0
channel_2_input = 0

opening/closing:
channel_1_input = 0
channel_2_input = 1

I’ve had a problem assessing the opening/closing state and used to read old states of ch1/ch2 inputs to make sure which one it is. But I can also use this.state to see the previous state of a whole gate and not only these inputs and get the same result.

Thanks a lot, I know exactly what to do now.