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.