Using a button in automation to cancel cover closing

Hello everybody,

I have a question for a simple automation. I used google to solve the problem on my own but I failed to create the automation correctly.
I want to close my window cover when the sun goes down. Thats clear and working. Now I want to add a button which cancels the process so that the covers stay open. For this scenario I created a button (on or off). The automation should do the following:
If the sun goes down and the cancel button is on, set the cancel button to off and do nothing else. If the cancel button is off close the cover. I want to use this for my garden door. I want to prevent to close the cover when I’m outside. I hope you understand, what I mean. The problem is that I don’t understand the yaml coding. Is there a good manual (except the manual from Home assistant) to read so I can learn this? This is my code. Sorry in advance for the completely wrong code but I don’t know better:

alias: Arbeitszimmer rollo schließen

trigger:

  event: sunset

  platform: sun

action:

#  

    data_template:

      entity_id: >

        {% if is_state(input_boolean.rollo_az_aussetzen","on") %}

              entity_id: input_boolean.rollo_az_aussetzen

              service: input_boolean.turn_off

        {% else %}

          - service: cover.close_cover

            cover.shelly_shsw_25_e62887 # Rollo Arbeitszimmer

        {% endif %}

Maybe something like this (no guarantee regarding indentation)

alias: Arbeitszimmer rollo schließen
trigger:
    event: sunset
    platform: sun
action:
    service_template: >
      {% if is_state(input_boolean.rollo_az_aussetzen,"on") %}
      input_boolean.turn_off
      {% else %}
      cover.close_cover
      {% endif %}
    data_template:
      entity_id: >
        {% if is_state(input_boolean.rollo_az_aussetzen,"on") %}
        input_boolean.rollo_az_aussetzen
        {% else %}
        cover.shelly_shsw_25_e62887 # Rollo Arbeitszimmer
        {% endif %}```

Hey Koying,

thank you for your answer. When I try your code I receive the following error

Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token ‘,’, got ‘string’) for dictionary value @ data[‘action’][0][‘data_template’][‘entity_id’]. Got None
invalid template (TemplateSyntaxError: expected token ‘,’, got ‘string’) for dictionary value @ data[‘action’][0][‘service_template’]. Got None. (See ?, line ?).

Do you know what this error means?

Some bogus double-quote in your original code.
I’ve edited my code

Hey Koying,

thank you for your help. I corrected the code as you described and the configuration check is ok now. I will check the automation today. Only one wrong character produces this big error message.
I want to understand the error messages and indeed the yaml scripting. Do you have any tips where I can find good material?

I received the following error in log and the automation didn’t work:

homeassistant.exceptions.HomeAssistantError: Error rendering service name template: UndefinedError: ‘input_boolean’ is undefined

I googled and got the information, that the input_boolean.rollo_az_aussetzen has to be set like this ‘input_boolean.rollo_az_aussetzen’. I corrected that and now it’s working fine. Thank you Koying for your time and your help. Here is the code which is working fine if someone want to do something similar

- alias: Arbeitszimmer rollo schließen erweitert
  trigger:
    event: sunset
    platform: sun
  action:
    service_template: >
      {% if is_state('input_boolean.rollo_az_aussetzen',"on") %}
      input_boolean.turn_off
      {% else %}
      cover.close_cover
      {% endif %}

    data_template:
      entity_id: >
        {% if is_state('input_boolean.rollo_az_aussetzen',"on") %}
        input_boolean.rollo_az_aussetzen
        {% else %}
        cover.shelly_shsw_25_e62887
        {% endif %}

The Template tab in the Developer tools is your friend :slight_smile:

Thank you that is really helpful. I didn’t recognized this before. This will help, thanks :slight_smile:

Hello Koying, may I ask you again for help? I have more covers to control and I want to use buttons to prevent the closing process for each cover. I tried the code below but now nothing happens and I don’t have an error in my log file. I found many if then else codes via google but in every threat they use only one template. But how can I solve it using 2 or more templates? I tried and changed code for hours but I don’t have any idea how to solve it. Can you please give me an advice? Thanks in advance!!

- alias: Rollos schließen bei Sonnenuntergang
  trigger:
    event: sunset
    platform: sun
#-----Steuerung Arbeitszimmer-----
  action:
    service_template: >
      {% if is_state('input_boolean.rollo_az_aussetzen',"on") %}
      input_boolean.turn_off
      {% else %}
      cover.close_cover
      {% endif %}

    data_template:
      entity_id: >
        {% if is_state('input_boolean.rollo_az_aussetzen',"on") %}
        input_boolean.rollo_az_aussetzen
        {% else %}
        cover.shelly_shsw_25_e62887
        {% endif %}

#-----Steuerung Badezimmer-----
  action:
    service_template: >
      {% if is_state('input_boolean.rollo_bz_aussetzen',"on") %}
      input_boolean.turn_off
      {% else %}
      cover.close_cover
      {% endif %}

    data_template:
      entity_id: >
        {% if is_state('input_boolean.rollo_bz_aussetzen',"on") %}
        input_boolean.rollo_bz_aussetzen
        {% else %}
        cover.shelly_shsw_25_e4c38e
        {% endif %}

You don’t repeat “action:”.
Something like this should do:

- alias: Rollos schließen bei Sonnenuntergang
  trigger:
    event: sunset
    platform: sun
#-----Steuerung Arbeitszimmer-----
  action:
    - service_template: >
      {% if is_state('input_boolean.rollo_az_aussetzen',"on") %}
      input_boolean.turn_off
      {% else %}
      cover.close_cover
      {% endif %}

      data_template:
        entity_id: >
          {% if is_state('input_boolean.rollo_az_aussetzen',"on") %}
          input_boolean.rollo_az_aussetzen
          {% else %}
          cover.shelly_shsw_25_e62887
          {% endif %}

#-----Steuerung Badezimmer-----
    - service_template: >
      {% if is_state('input_boolean.rollo_bz_aussetzen',"on") %}
      input_boolean.turn_off
      {% else %}
      cover.close_cover
      {% endif %}

      data_template:
        entity_id: >
         {% if is_state('input_boolean.rollo_bz_aussetzen',"on") %}
         input_boolean.rollo_bz_aussetzen
         {% else %}
         cover.shelly_shsw_25_e4c38e
         {% endif %}

Thank you for your answer. I tried your suggestion (I created a file named TEST.yaml) but I receive the following error

Error loading /config/configuration.yaml: while scanning for the next token
found character ‘%’ that cannot start any token
in “/config/custom_automation_dir/TEST.yaml”, line 8, column 8