Script to Toggle Cover

Hello everyone,

New to homeassitant, and got a question for you guys and gals…

I want to create a script that could toggle the cover when called. I understand that I can create one script to open, and one to close, using the sintax:

alias: Open Cover
service: cover.open_cover
data:
  entity_id: cover.mqtt_cover

But I want to link this script to a IFTTT button on IOS, so ideally I just want one button, and if the garage is open I want the script to close the cover, if it is closed I want to to open it.

So the script needs to read the current status, and request the opposite… is it possible?

I think you can do this with templating based on the state but it would be a lot easier if cover had a toggle service like switch does.

There’s an example with a very similar topic here

Perfect!

Thanks! for future reference, if anybody needs to do the same the following worked fine for me:

script:
  toggle_cover:
    sequence:
      - service_template: >
          {% if is_state('cover.mqtt_cover', 'open') %}
            cover.close_cover
          {% else %}
            cover.open_cover
          {% endif %}
        entity_id: cover.mqtt_cover
2 Likes

I knew it was done with a template; I just couldn’t remember the service template connection. Sorry!

Glad you got this solved.

Awesome stuff. Is there a way to integrate this into a MQTT Publish sequence? I have the following to create a MQTT Toggle button in MQTT Dash and it works extremely well but your template may well tidy that up even further.

### Closes garage door (if open) when publishing to display/kitchen/touch/garage ###
### with the payload as 'Closed' which matches the subscribe state in MQTT Dashboard ###
  - alias: MQTT Garage Close
    hide_entity: true

    condition:
      - condition: state
        entity_id: cover.garage
        state: 'open'

    trigger:
      - platform: mqtt
        topic: "display/kitchen/touch/garage"
        payload: "Closed"

    action:
      - service: cover.close_cover
        entity_id: cover.garage

### Opens garage door (if closed) when publishing to display/kitchen/touch/garage ###
### with the payload as 'Open' which matches the subscribe state in MQTT Dashboard ###
  - alias: MQTT Garage Open
    hide_entity: true

    condition:
      - condition: state
        entity_id: cover.garage
        state: 'closed'

    trigger:
      - platform: mqtt
        topic: "display/kitchen/touch/garage"
        payload: "Open"

    action:
      - service: cover.open_cover
        entity_id: cover.garage

In case anyone is still wondering, you don’t need a script anymore

There is now a service cover.toggle

1 Like