Template Sensor For garage door

need some help getting a cover template working.
I have an athom tech garage door opener with the reed switch configured at the open end of the door. I have a reed switch for when the door is closed connected through my Konnected alarm panel.
I want to combine these two sensors to have a Garage door state Closed, Open, Opening/Closing. I have the code I am using in my binary sensor template configured through the front end below. The state remains as “Closed” even when the sensor states dictate an “open” state. Any thoughts?

{% if is_state('binary_sensor.athom_garage_door_01063c_contact', 'off') and is_state('binary_sensor.konnected_b8334c_zone_3', 'off') %}
  Closed
{% elif is_state('binary_sensor.athom_garage_door_01063c_contact', 'on') and is_state('binary_sensor.konnected_b8334c_zone_3', 'on') %}
  Open
{% else %}
  Opening / Closing
{% endif %}

None of the outputs from your template are valid.

https://discord.com/channels/606225571898327139/1159226712584880158

{% if is_state('binary_sensor.athom_garage_door_01063c_contact', 'off') and is_state('binary_sensor.konnected_b8334c_zone_3', 'off') %}
  closed
{% elif is_state('binary_sensor.athom_garage_door_01063c_contact', 'on') and is_state('binary_sensor.konnected_b8334c_zone_3', 'on') %}
  open
{% else %}
  opening 
{% endif %}

Unfortunately you will have to choose between “opening” or “closing” for that last state. You can’t have both - unless you can also detect the direction the door is moving (possibly by checking which binary sensor changed last).

I don’t think the last changed would work, as if you stop it half way and hit the relay again it reverses direction.

Thanks for the help but it still isn’t working. I tried creating a sensor rather than a binary sensor (maybe because theres more than one option?) and It seemed to work. As I haven’t got the alarm reed switch set up so its currently open.

Wait, what?

Isn’t this a template cover?

Binary sensors only have two states, on or off.

I put a template cover in the config yaml and nothing happened. So I used the GUI. You can set the binary sensor to “Garage Door”.

Maybe the yaml code didn’t work because of my capitalised states?

Use this:

ok so I’ve got this in my yaml. I can get the stop button working but state is unknown and the up/down are not working

  - trigger:
    platform: state
    entity_id:
      - binary_sensor.athom_garage_door_01063c_contact
      - binary_sensor.konnected_b8334c_zone_3
    sensor:
      - name: "Garage Door Status"
        unique_id: "garage_door_calc_status"
        state: >
          {% if is_state('binary_sensor.athom_garage_door_01063c_contact', 'off') and is_state('binary_sensor.konnected_b8334c_zone_3', 'off') %}
            closed
          {% elif is_state('binary_sensor.athom_garage_door_01063c_contact', 'on') and is_state('binary_sensor.konnected_b8334c_zone_3', 'on') %}
            open
          {% elif (trigger.to_state.state == 'on' and trigger.from_state.state == 'off' and trigger.entity_id == 'binary_sensor.athom_garage_door_01063c_contact') %}
            closing
          {% else (trigger.to_state.state == 'on' and trigger.from_state.state == 'off' and trigger.entity_id == 'binary_sensor.konnected_b8334c_zone_3') %}
            opening
          {% endif %}
cover:
  - platform: template
    covers:
      garage_door:
        device_class: garage
        friendly_name: "Camlet Garage"
        unique_id: "smart_garage"
        position_template: '{{ states("sensor.garage_door_calc_status") }}'
        open_cover:
          - condition: state
            entity_id: sensor.garage_door_calc_status
            state: "open"
          - action: switch.turn_on
            target:
              entity_id: switch.athom_garage_door_01063c_relay
        close_cover:
          - condition: state
            entity_id: sensor.garage_door_calc_status
            state: "on"
          - action: switch.turn_off
            target:
              entity_id: switch.athom_garage_door_01063c_relay
        stop_cover:
          action: switch.turn_on
          target:
            entity_id: switch.athom_garage_door_01063c_relay
        icon_template: >-
          {% if is_states('sensor.garage_door_calc_status','closed') %}
            mdi:garage
          {% else %}
            mdi:garage-open
          {% endif %}

ok documenting this for others. here is the code I have working. although the icon change doesn’t appear to be working:

template:
  - sensor:
      - name: Garage State
        unique_id: "garage_state"
        state: >
          {% set open_on = is_state('binary_sensor.athom_garage_door_01063c_contact','on') %}
          {% set close_on = is_state('binary_sensor.konnected_b8334c_zone_3','off') %}
          {% if open_on and not close_on %}
            open
          {% elif not open_on and close_on %}
            closed
          {% elif open_on and close_on %}
            sensor error
          {% else %}
            {% if is_state('sensor.garage_state', 'open') or is_state('sensor.garage_state', 'closing') %}
              closing
            {% else %}
              opening
            {% endif %}
          {% endif %}

cover:
  - platform: template
    covers:
      garage_door:
        device_class: garage
        friendly_name: "Camlet Garage"
        unique_id: "smart_garage"
        value_template: '{{ states("sensor.garage_state") }}'
        open_cover:
          action: switch.turn_on
          target:
            entity_id: switch.athom_garage_door_01063c_relay
        close_cover:
          action: switch.turn_on
          target:
            entity_id: switch.athom_garage_door_01063c_relay
        stop_cover:
          action: switch.turn_on
          target:
            entity_id: switch.athom_garage_door_01063c_relay
        icon_template: >
          {% if is_state('sensor.garage_state','closed') %}
            mdi:garage-lock
          {% elif is_state('sensor.garage_state','sensor error') %}
            mdi:garage-alert
          {% elif is_state('sensor.garage_state','opening') %}
            mdi:garage-open
          {% elif is_state('sensor.garage_state','closing') %}
            mdi:garage-open
          {% else %}
            mdi:garage-alert
          {% endif %}

What’s wrong with the icon?

Note that with a trigger-based template, the various properties of the entity (icon, attributes, etc.) are evaluated only when a trigger fires. And, counter-intuitively, at that split second when the icon is being computed, is_state might not get the state to which your entity is about to change to; you might be accessing a stale state, namely the one you’re about to change away from. Often this manifests itself with a “lag” where the icon after the state update is what it should have been before the state update.

In my template entities, I deal with this by avoiding is_state, states, and other current state references in the sensor definition code and instead reference the same state code I use for the entity’s state definition. If that makes sense…

Ok so I fixed it. while screwing around while nothing worked I had set the icon in the GUI on the cover. So that was “overriding” the yaml icon

1 Like