Template switch and keeping track of open/closed

Hi
I have a switch.garageporten that I toggle the garagedoor with. But it is “dumb” and only sends a pulse to toggle the garagedoor motor. In order to keep track of if the garagedoor is open I have a magnetic sensor (binary_sensor.garageporten).

I have made a template switch to have different icons depending on the state. But how can I also have the garegedoor status (Open/Closed) written out? Now the state is only “on” when I click the switch and then immediately goes “off”.

Been struggeling way too long with this so all help is appreciated :slight_smile:

switch:
  - platform: template
    switches:
      garageporten_template:
        friendly_name: "Garageporten"
        value_template: "{{ is_state('switch.garageporten', 'on') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.garageporten
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.garageporten
        icon_template: >-
          {% if is_state('binary_sensor.garageporten', 'on') %}
            mdi:garage-open-variant
          {% else %}
            mdi:garage-variant
          {% endif %}

It seems like your situation would be better suited to a Template Cover instead of a Temple Switch.

1 Like

Also, your value_template should reference the binary sensor, not the switch.garageporten.

Thank you, I’ll look into this one.

That is what I also discovered yesterday, but when I changed it my switch wouldn’t work. Do you see anything obviously wrong here?

switch:
  - platform: template
    switches:
      garageporten_template:
        friendly_name: "Garageporten"
        value_template: "{{ is_state('binary_sensor.garageporten', 'on') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.garageporten
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.garageporten
        icon_template: >-
          {% if is_state('binary_sensor.garageporten', 'on') %}
            mdi:garage-open-variant
          {% else %}
            mdi:garage-variant
          {% endif %}

Those two together are confusing… does switch.garageporten send a pulse on both events, i.e. when it is turned “on” and “off”? Based on the History screenshot in the first post, it appears that switch.garageporten is turning “on” then “off” immediately.

1 Like

Yes (if I understand your question). switch.garageporten turns “on” whenever the switch is triggered. The state turns on for a second and then back to off. So it is basically only on when there is a pulse sent.

What I would like to get out of it, as described in my first post, is that the state of the template switch is “Open” or “Closed” since that would add more meaning in Lovelace.

Then you want to use the binary_sensor as the main state. But the state will lag your button presses because the state only updates when the binary sensor updates. This is very apparent when it takes ~10 seconds to open/close your garage door. But if you’re patient, the state will update on the switch/cover.

And that is fine, ideally there would be a state saying “Opening” / “Closing” but that’s not needed.

But, when I change the code to

value_template: "{{ is_state('binary_sensor.garageporten', 'on') }}"

my switch template stops working. Nothing at all happens. So there is something fishy with my code but I canät figure it out.

If that always stays false or off, that means your entity_id is wrong.

I’ve never been more lost…

The only way to get my code to toggle the garagedoor is to have it like this:

value_template: "{{ is_state('switch.garageporten', 'on') }}"

But then the state is only “on” when I send the pulse to open/close. I think I will have to stick with one switch template for the icon and the toggle functionality, and then try to incoroprate the binary sensor in the code for the status of the garagedoor (open/closed).

Does binary_sensor.garageporten exist as an entity? Can you post a screenshot of it in developer tools → states page.

Yes, and it indicates on (open) / off (closed) correctly:

Then it doesn’t make sense that it doesn’t work when you change the value_template.

Have you looked in your logs for errors? Can you share a video of you using the entity in the frontend with the value_template pointing at the binary sensor?

I don’t think you understood the question, but you have provided an answer…

This is a problem:

If the switch turns “on” then immediately “off”, then the action you have assigned for turn_off will never do anything. For the template switch to effectuate a change in the actual garage door both of those actions need to be switch.turn_on.

Part of the reason you are lost is that you are using entities outside their type… you’ve got a switch that should be a button that you’re using to create another switch that should be a cover.

1 Like

good catch

Thank you for your patience :slight_smile:
With your help the switch.garageporten_template is now reporting “on” and “off” but I think that is as close as I’ll get, I’ll do some replace actions in frontend to have it show open/closed.

Go to the entity’s settings and change Show as a cover, garage door or window


or in the template

{{'open' if is_state('switch.garageporten', 'on') else 'closed'}}

Thanks, but I realized that the switch doesn’t support the device classes that I need so I think it is back to doing it right from the beginning and using cover instead of switch.

1 Like

I agree as stated earlier Template Cover is probably your best option.

Thank you all for your feedback! I went with the template cover and it seems to be working and doing everything that I wanted. Sharing the code in case someone else needs something similar in the future. Icon template isn’t necessary but prefer the wider garage door icon over the standard one.

cover:
  - platform: template
    covers:
      garage_door:
        device_class: garage
        friendly_name: "Garageporten"
        value_template: "{{ is_state('binary_sensor.garageporten', 'on') }}"
        open_cover:
          service: switch.turn_on
          entity_id: switch.garageporten
        close_cover:
          service: switch.turn_on
          entity_id: switch.garageporten
        stop_cover:
          service: switch.turn_on
          entity_id: switch.garageporten
        icon_template: >-
          {% if is_state('binary_sensor.garageporten', 'on') %}
            mdi:garage-open-variant
          {% else %}
            mdi:garage-variant
          {% endif %}