Dual state button from scripts to open and close shades

Hello everyone, I am new to Home Assistant and I am struggling to create a button to open and close my shades… I was able to create the service and an open button and a close button… but I can’t figure out how to create the open / close on the same button… the open / close calls to a service connected to a broalink RM4-pro and works perfect…

Buttons don’t have states, they just generate events. You could try an toggle helper (input_boolean) instead - that would have on or off states.

In addition to what @Stiltjack suggested, you have a couple of other options as well:

  • Create a switch template take have more control over how you work with the device and to give you a dual state (toggle) behavior. This is the same result as suggested but gives you more control.

  • Instead of a switch template, use a cover template instead, this would be more in line with what you are needing with the added benefit of you being able to define things more granularly such as opening to a particular percentage (even if the shades don’t support it natively). This is how I do it on unsupported devices.

  • Use conditional cards in your dashboard on your two buttons so that only one is visible at a time, this provides a psuedo-dual-state behavior in your dashboard.

thank you Stiltjack

Thank you CO_4X4

I have no idea how to make the cover template I will do some research on that, I would love to open the blinds partially like you mentioned…

You could also add an if block in an automation and check the state of the shades and thus have a toggle action as well. (if shade == open then close else open)

Thanks code-in-progress but the blinds are IR (remote) and do not return the state

That’s where a cover template can come in handy, you don’t have to have feedback you just need to build logic based on assumed conditions. Some of my curtains are controlled via an Add-A-Motor device, it’s just on and off with a reversing switch when it hits either end, I built an elaborate system using Pyscript and cover templates and I know precisely where those curtains are and can do with them anything I could do with truly smart curtains. At first it was about 20% inaccurate, now it’s about 2%. That’s why HA rules over most other home automation platforms, you CAN do this if you choose to take the time.

1 Like

Got it, now I just need to figure out how to do it :slight_smile:

Thanks CO_4X4

It’s pretty simple to create the cover template. I would start there with no bells or whistles, just open and close and then refine it as you go.

Basically my Add-A-Motor cover template works like this:

  • Run a stopwatch to know how long it takes a curtain to toggle from open to close and close to open (seems redundant but in my case there was a 2 second difference)

  • Use a timer to know how long it has been running (in my case how long the Z-Wave outlet has been on) and calculate the percentage of where the cover should be

Once you know those things then you can easily figure out how long to run the open command before you say STOP to have it open to a specific percentage. I don’t know if yours allows a stop or not, if not then you may only be in open/close territory.

Thank you so I added the code below to the configuration.yaml

cover:

  • platform: template
    covers:
    shade#7:
    device_class: shade7
    friendly_name: “Shade #7
    value_template: “{{ states(‘sensor.shade#7’)|float > 0 }}”
    open_cover:
    service: script.shade_07_open
    close_cover:
    service: script.shade_07_close

I am not sure what covers: refers to and I do not have a sensor.shade#7 defined anywhere

When I go to check configuration under developer tools I get the following message
Configuration warnings

Invalid config for ‘template’ from integration ‘cover’ at configuration.yaml, line 11: invalid slug shade#7 (try shade_7) for dictionary value ‘covers’, got {‘shade#7’: {‘device_class’: ‘shade7’, ‘friendly_name’: ‘Shade #7’, ‘value_template’: “{{ states(‘sensor.shade#7’)|float > 0 }}”, ‘open_cover’: {‘service’: ‘script.shade_07_open’}, ‘close_cover’: {‘service’: ‘script.shade_07_close’}}}

Sorry, I went thru the errors and fixed a couple of things on to the next step now

cover:

  • platform: template
    covers:
    shade_7:
    device_class: blind
    friendly_name: “Shade #7
    value_template: “{{ states(‘sensor.shade_7’)|float > 0 }}”
    open_cover:
    service: script.shade_07_open
    close_cover:
    service: script.shade_07_close
1 Like

Thank you for the tip Stiltjack

First time posting here…

How do I link the template above to a button? I can’t find it as an entity or service.

Thanks again

@itmajors please look at the article link that @Stiltjack posted, he’s saying that we can’t help you until you post your code properly so we can verify that it is correct, it may not be creating an entity because of a syntax error that is impossible to determine in the way you pasted it.

cover:
  - platform: template
    covers:
      shade_7:
        device_class: blind
        friendly_name: "Shade #7"
        value_template: "{{ states('sensor.shade_7')|float > 0 }}"
        open_cover:
          service: script.shade_07_open
        close_cover:
          service: script.shade_07_close

I think my problem may be the value_template line I do not have that sensor.shade_7 created anywhere in my instance… can I remove that line completely since the blinds I have do not inform anything back to HA ?

thank you! @Stiltjack & @CO_4X4

It is an optional variable. Try it without value_template and see how it works, you can then enhance it by setting a helper when you open and close, keep building it out until it is what you want.

@CO_4X4 it worked perfect!!! Thank you so much for your help, I watched a few youtube videos looking for a solution before posting here… Time wasted…

Thank you again to everyone