Need help with if/else

Hi all, I’m pretty new to Home Assistant and need some help with a configuration issue I’m having. I have a Shelly switch that I’m using to control my garage door. The Shelly switch is recognized as two separate components, one is the actual switch and the other is the sensor of the switch (which I’m using to read as open for on and closed for off). I combined the two into a Cover so that Home Assistant recognizes the switch as a garage door and properly reads the status of the door. Here is my cover which works fine for the most part when using Home Assistant:

cover: 
  - platform: template
    covers:
      garage_door:
        device_class: garage
        friendly_name: "Garage Door 2"
        value_template: "{{ is_state('binary_sensor.shelly_shsw_1_b8f683_switch', 'on') }}"
        open_cover:
          service: switch.turn_on
          data:
            entity_id: switch.shelly_shsw_1_b8f683
        close_cover:
          service: switch.turn_on
          data:
            entity_id: switch.shelly_shsw_1_b8f683
        icon_template: >-
          {% if is_state('binary_sensor.shelly_shsw_1_b8f683_switch', 'on') %}
          mdi:garage-open
          {% else %}
          mdi:garage
          {% endif %}

The problem is that I use HomeKit to publish my devices into my Apple Home. It works fine there too with one major exception, Apple Home automations and Siri. As an example I have an automation that triggers when I plug my phone into CarPlay, it checks to make sure that all the doors are shut, lights off and alarm armed. Now, when it does this even though the state of garage door may already be “Closed” it’s firing off the command close_cover in Home Assistant. This results in my garage door opening when I want it to stay closed!

What i’d like to do is have the service close_cover run an if/else, but this is where I’m stuck. If the door is open (sensor ON) then fire the command to turn on the switch. If the door is closed (sensor OFF) then i want it to do nothing. I experimented with the following but it results in configuration errors:

close_cover:
          service: >
			{% if is_state(‘binary_sensor.shelly_shsw_1_b8f683_switch’, ‘on’) %}
			switch.turn_on
			{% endif %}
		  entity_id: switch.shelly_shsw_1_b8f683 

Can anyone guide me on what I might be missing here?

If you could, please post the full cover config?

I’m on my phone but I think what you need to do is to define value_template with what determines your cover’s state. Based on that, the appropriate action (like close_cover) will be invoked. In the service section for action, remove the template stuff and just use the service call and entity ID.

The full cover config is posted in my original post but I’ll post it again:

cover: 
  - platform: template
    covers:
      garage_door:
        device_class: garage
        friendly_name: "Garage Door 2"
        value_template: "{{ is_state('binary_sensor.shelly_shsw_1_b8f683_switch', 'on') }}"
        open_cover:
          service: switch.turn_on
          data:
            entity_id: switch.shelly_shsw_1_b8f683
        close_cover:
          service: switch.turn_on
          data:
            entity_id: switch.shelly_shsw_1_b8f683
        icon_template: >-
          {% if is_state('binary_sensor.shelly_shsw_1_b8f683_switch', 'on') %}
          mdi:garage-open
          {% else %}
          mdi:garage
          {% endif %}

This works fine, it’s just when HomeKit evokes the close_cover command it basically just toggles the switch ignoring the fact that the door is already closed.

Don’t know much about covers but can you explain why the close action is exactly the same as the open action ?

Because there is no actual “off” with this switch, it just acts like a momentary button which automatically turns off after 1 second. Otherwise my garage door would be operating non-stop.

The cover should be doing this already. It’s odd that it’s not suppressing the turn off. That leads be to believe the value_template is incorrect. But it does look correct syntaxtually. Either way, you can make a script with a condition in your close cover

script:
  garage_door_2_close_cover:
    sequence:
    - condition: state
      entity_id: binary_sensor.shelly_shsw_1_b8f683_switch
      state: 'on'
    - service:  switch.turn_on
      data:
        entity_id: switch.shelly_shsw_1_b8f683

then in your cover…

        close_cover:
          service: script.garage_door_2_close_cover
1 Like

I believe you just made that word up. :wink: :laughing:

I think the word you are looking for is “syntactically”.

2 Likes

I’m glad that wasn’t just me !

1 Like

tomato toMAto

This did the trick. Thank you so much!

1 Like