Hi there,
I am struggeling to get the hang of my garage door.
I’m using two mqtt reed switches to report the positions up (garagerechtsoben_contact) or down (garagerechtsoben_contact).
This works as a charm. But I can’t figure out, how to detect the “rising edge” from a sensor from off to on, and chose the appropiate “closing” or “opening” state.
One way would be to make template sensors triggered on state changes and use that in your cover templates. For example, you can access the from and to states on the trigger state object of those binary sensors. If the down binary sensor goes from true to false, it’s opening.
Thank you for your solution!
I did not know about the possibility of template sensors at all, since I’m very new to Homeassistant (switching from fhem).
I was able to put the following sensor together:
template:
- trigger:
- platform: state
entity_id:
- binary_sensor.garagerechtsoben_contact
- binary_sensor.garagerechtsunten_contact
sensor:
- name: 'rechteGarageStatus'
unique_id: 'rechteGarage_status'
state: >
{% if (trigger.to_state.state == 'off' and trigger.entity_id == 'binary_sensor.garagerechtsoben_contact') %}
open
{% elif (trigger.to_state.state == 'off' and trigger.entity_id == 'binary_sensor.garagerechtsunten_contact') %}
closed
{% elif (trigger.to_state.state == 'on' and trigger.from_state.state == 'off' and trigger.entity_id == 'binary_sensor.garagerechtsoben_contact') %}
closing
{% elif (trigger.to_state.state == 'on' and trigger.from_state.state == 'off' and trigger.entity_id == 'binary_sensor.garagerechtsunten_contact') %}
opening
{% endif %}
In the definition of my garage door I just referenced the calculated state:
Which works like a charm. Only on the HomeAssistant startup there is no predefined state. Which I will get to by using an else construct in the sensor.
So many thanks! I marked your post as the solution.
I’m glad it works for you (and welcome to the community)!
Templating is very powerful.
Something else to avoid errors and unknown states during startup is this:
Avoid using states.sensor.temperature.state , instead use states('sensor.temperature') . It is strongly advised to use the states() , is_state() , state_attr() and is_state_attr() as much as possible, to avoid errors and error message when the entity isn’t ready yet (e.g., during Home Assistant startup).
I think you can probably change one of your if conditions to be the else (probably the closed one; I don’t think logically there are other combinations to cater for).
The template works great, however the current version of HA restores the last state at startup. Setting a default state with an else condition doesn’t change this behaviour. Ideally I want to refresh with the current actual state and not use a default or the previous state.
Example: if garage door is closed when HA is shutdown/restarted, the state will be closed when it starts, even if the garage is open.
So far I haven’t come up with a quick/easy way to automate refreshing the real state. A call to
homeassistant.update_entity with the sensor template’s entity ID doesn’t do it.
No, using a Shelly Uni device with HA’s built-in integration. This single device provides the switch entity and two sensor entities which I’m using with reed switches for opened and closed states.
After restarting, the sensor entities on the device itself show their correct status. Getting that into the template sensor is where I’m stuck.
I’ve tried putting an additional trigger into the template for when HA starts up, but it seems that might get evaluated too early as it makes the state an error condition and the sensor “unavailable” after restart. Changing state on the physical sensor connections causes it to update and reflect the correct state.
The solution was providing a trigger for HA start event in the sensor template, however, only checking for the open and closed states, because the to_state and from_state aren’t yet populated and produce an error.
That gives you something like this for the sensor template:
template:
- trigger:
- platform: state
entity_id:
- binary_sensor.garage_door_open
- binary_sensor.garage_door_closed
- platform: homeassistant
event: start
sensor:
- name: "Garage Door Status"
unique_id: "some_unique_id_garage1234"
state: >
{% if is_state('binary_sensor.garage_door_closed', 'on') %}
closed
{% elif trigger.platform != 'homeassistant' %}
{% if (trigger.to_state.state == 'off' and trigger.from_state.state == 'on' and trigger.entity_id == 'binary_sensor.garage_door_open') %}
closing
{% elif (trigger.to_state.state == 'off' and trigger.from_state.state == 'on' and trigger.entity_id == 'binary_sensor.garage_door_closed') %}
opening
{% else %}
open
{% endif %}
{% else %}
open
{% endif %}
I’ve since modified my own installation to provide real-time states by monitoring the door shaft, keeping the state change of the magnetic sensors as a backup. This can also easily be made to monitor the motor spindle instead, all using a ready-made rotary Ikea Tradfri sound controller. In total, these are the states also supported by Homekit so it all dovetails nicely there: Open, Closed, Opening, Closing, Stopped.
The rotary controller has been wrapped with multiple layers of 1mm EVA foam to add cushioning and increase friction, making sure that it always rotates in response to the garage door shaft.
Thanks for all the info. What config/code is setup for input_text.garage_rotary_state and does it track the exact door position or it outputs opening/closing?
Yes, that text input contains only the English state value of the door and it’s populated by the Blueprint I mentioned. In fact it’s the only action I specify in the blueprint for each state it can report.
It was an easy way to reference the output of the blueprint from the YAML definition.
Have any of you, but especially @kirijanker or @HVR88, tried adding this type of setup to HomeKit? I know that you can add a garage gate controller to HomeKit but I understood from other topics that people were missing the “opening”/“closing” statuses. Thanks to your tips, I managed to get the status in HA but there are several entities that handle all of this, right? One is the switch that controls the gate and the other one keeps the status. Is it possible to combine this and get all of it into HomeKit?
Edit: I checked and it actually works perfectly!
Another question though. Do you guys use tasmotized devices for your setup? I would like to use a sonoff and add two magnetic sensors but I cannot manage. I managed to add one but do not understand how to add a second one. I guess it is more of a Tasmota question but I thought I would try here.
No. If you want the serious DIY approach as has been discussed, use a Shelly Uni - it’s the best device for the job.
BUT… If you have a suitable opener, I just found out about a ready-to-go device this past week that pulls a lot more info. RATGDO (RAT Garage Door Opener) ratGDO
It works with virtually any residential Chamberlain or Liftmaster manufactured garage door opener and also offers basic support for other brands which use dry contacts to control the door.
It can control the door and report back the actual status of the door (closed, opening, open, closing). For Security + 2.0 door openers, the obstruction sensors, light, wireless lockout and even motion sensing are also integrated.
Had I known about this device earlier, I’d never have bothered with the Shelly Uni or the IKEA rotary encoder.
It made more sense to me to just make a boolean sensor called garage was open. Then I check that in the template and have two automations to change it when it opens and closes.
alias: 4 Garage Was Open Set
description: ""
trigger:
- platform: state
entity_id:
- cover.garage_door
to: open
condition: []
action:
- service: input_boolean.turn_on
data: {}
target:
entity_id: input_boolean.garage_was_open
mode: single
Thank you for your reply. I just ordered a Shelly Uni. Do you use it with ESPHome or Shellys original firmware? I asked about using tasmota but you suggested a different hardware as well so I am wondering if your advice is mainly not to use Tasmota or not to use Sonoff SV or both?
I am not sure what happened but I am getting dozens and dozens of “device unavailable” messages in HA for my Sonoff SV. The log in the console for Tasmota gives me rc -2 error. It might be weak wifi signal because I get readings of -79 to -75. At the same time, I feel like I never had those issues before. My router is maybe 2.5 m away from the Sonoff mounted in a waterproof box on the wall. There is a direct line of sigh between the router, through the window to Sonoff so it feels crazy that the signal would be still too weak but I get the signal strength I get.
I tried to implement your solution with homeassistant event: start but I still get a notification that the gate was opened and then closed every time the Sonoff becomes unavailable and then available again. Any suggestions on what I could do? It happens every 5-10 minutes. Long term I need to solve the wifi signal I guess.
I use the stock Shelly firmware and the native HA integration for Shelly. Every Shelly device I have works very well with the integration, including very fast responses and never go offline. I only don’t suggest their smart outlets - all 4 of mine died in less than a year.
I can’t help with Tasmota firmware as I don’t use that on anything. ESPHome I use on my mini-split air conditioner and it works well. I have some Sonoff switch modules, howver none are WiFi as I prefer to stick to Zigbee for those. The only WiFi stuff I use is from Shelly, apart from my Homepods, phones and computers and such.