My config for garage door with 1 magnetic sensor, work great:
cover:
- platform: template
covers:
# секцию ниже не трогать. Рабочий прототип гаражных ворот
garage_door:
device_class: garage
friendly_name: "гараж"
unique_id: garagedoor
value_template: >
{% if is_state('binary_sensor.vorota_zakryty_contact', 'off') %}
false
{% else %}
true
{% endif %}
open_cover:
- condition: state
entity_id: binary_sensor.vorota_zakryty_contact
state: "off"
- service: switch.turn_on
target:
entity_id: switch.gate_remote
close_cover:
- condition: state
entity_id: binary_sensor.vorota_zakryty_contact
state: "on"
- service: switch.turn_on
target:
entity_id: switch.gate_remote
stop_cover:
- service: switch.turn_on
target:
entity_id: switch.gate_remote
But i have more complicated configuration. I am using 2 magnetic sensors for garage door. Sensor1 for “closed” state (off if gate is closed) and sensor2 for “open” state (off if gate is opened)
Based on real sensors i create templates:
template:
- sensor:
- name: vorota_opening
state: "on"
- name: vorota_closing
state: "on"
- name: vorota_open
state: "off"
- name: vorota_closed
state: "on"
And appdaemon script:
import appdaemon.plugins.hass.hassapi as hass
##############################################
### ПОМЕНЯТЬ ПЕРЕМЕННЫЕ ТУТ
##############################################
# имя датчика открытых ворот OpenSensor
open_sensor = "binary_sensor.0x00124b0022fef19f_contact"
# имя датчика закрытых ворот Clopen_sensoredSensor
closed_sensor = "binary_sensor.vorota_zakryty_contact"
class garage_door_status(hass.Hass):
def initialize(self):
handle = None
# слушем изменение датчиков открытых и закрытых ворот и если оно есть, запускаем функцию garage_door_status
self.listen_state(self.garage_door_status, open_sensor, attribute="all")
self.listen_state(self.garage_door_status, closed_sensor, attribute="all")
# debug only
self.listen_state(self.garage_door_status, 'cover.test_garage_door', attribute="all")
def garage_door_status(self, entity, attribute, old, new, kwargs):
# пишем в лог для траблшутинга, позже можно убрать
self.log(f'===.input entity={entity}, new state is {new["state"]}. old state is {old["state"]}')
# делаем проверку, изменился ли статус датчика
if new["state"] != old["state"]:
if entity == closed_sensor:
if old["state"] == "off" and new["state"] == "on":
self.log("info 01. set gate to OPENING state")
self.set_state('sensor.vorota_closed', state="on")
self.set_state('sensor.vorota_closing', state="on")
self.set_state('sensor.vorota_open', state="on")
self.set_state('sensor.vorota_opening', state="off")
elif old["state"] == "on" and new["state"] == "off":
self.log("info 02. set gate to CLOSED state")
self.set_state('sensor.vorota_open', state="on")
self.set_state('sensor.vorota_closing', state="on")
self.set_state('sensor.vorota_opening', state="on")
self.set_state('sensor.vorota_closed', state="off")
elif entity == open_sensor:
if old["state"] == "off" and new["state"] == "on":
self.log("info 03. set gate to CLOSING state")
self.set_state('sensor.vorota_open', state="on")
self.set_state('sensor.vorota_opening', state="on")
self.set_state('sensor.vorota_closed', state="on")
self.set_state('sensor.vorota_closing', state="off")
elif old["state"] == "on" and new["state"] == "off":
self.log("info 04. set gate to OPEN state")
self.set_state('sensor.vorota_opening', state="on")
self.set_state('sensor.vorota_closed', state="on")
self.set_state('sensor.vorota_closing', state="on")
self.set_state('sensor.vorota_open', state="off")
So My logic is:
- when sensor1 old state is “off” and new state is “on” - then i assume that gate is “opening” (sensor.vorota_opening’, ‘off’)
- when sensor2 old state is “off” and new state is “on” - then gate is “open” (sensor.vorota_open’, ‘off’)
- when sensor2 old state is “on” and new state is “off” - then gate is “closing” (sensor.vorota_closing’, ‘off’)
- when sensor1 old state is “off” and new state is “on” - then gate is “closed” (sensor.vorota_closed’, ‘off’)
On paper it should work fine, but i have strange behaivor in log:
When sensor.vorota_closed change it state from “off” (means gate is closed) to “on” means (gate is opening) - gate state is changed to “open” and in moment later “opening”
Is some one can explain to me what is my mistake?