Script: if without else condition

Hi All,

I have some problems with the Script syntax, where i need your help. I’ve made a script with a lot of if conditions, but i do not need the else execution. At the moment i have some warnings found in the log. How to do it better?
What i want to do is just open the covers which are enablead by input booleans, so in general:
if boolean 1 = on, then open cover 1
if boolean 2 = on, then open cover 2
if boolean 3 = on, then open cover 3

at the moment i’ve “homeassistant.update_entity” in the else condition but of course it’s not a entity_id.

scene_morning:
  alias: open covers based on boolean
  sequence:
  - service: cover.open_cover
    data_template:
      entity_id: >
        {%- if is_state('input_boolean.auf_cover_1', 'on') -%}
          cover.cover_temp_1
        {%- else -%}
          homeassistant.update_entity
        {%- endif -%}
  - service: cover.open_cover
    data_template:
      entity_id: >
        {%- if is_state('input_boolean.auf_cover_2', 'on') -%}
          cover.cover_temp_2
        {%- else -%}
          homeassistant.update_entity
        {%- endif -%}

I’ve also tried use the condition state option like in automations but i do not want to stop further execution if one condition is not true. Like in the example above, even if boolean 1 is disabled, i want to open 2 and 3 for example.

From a template perspective you can just leave out the else. Trouble is, the service is called no matter what. Aka, if it’s not true, the service is called with homeassistant.update_entity or if you leave it out, nothing.

In my opinion it’s just easy to fix if you just drop the templates all together and switch to an automation or script :slight_smile: There you can just use the chooser.

Please try this template

{% if is_state('input_boolean.auf_cover_1', 'on') %}
  cover.cover_temp_1
{% endif %}

Script example:

alias: Morning scene script
sequence:
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.auf_cover_1
            state: 'on'
        sequence:
          - service: cover.open_cover
            target:
              entity_id: cover.cover_temp_1
    default: []
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.auf_cover_2
            state: 'on'
        sequence:
          - service: cover.open_cover
            target:
              entity_id: cover.cover_temp_2
    default: []
mode: single

@sheminasalam Like I said, that will still fail because now the service is called without entity.

1 Like

thank you. it seems to be working great. I will adapt this to all 18 conditions and hope this is also working for calling scripts.

I’ve found also another solution which seems to be working with the service templating.

sample_script:
  sequence:
  - service_template: >
        {%- if is_state('input_boolean.auf_cover_1', 'on') -%}
           cover.open_cover
        {% else %}
           homeassistant.update_entity
        {% endif %}
    entity_id: cover.cover_temp_1
  mode: single

Ah, yeah, you could template the call indeed. But I think that’s more of a hack. The chooser is introduced for this kind of tasks.

1 Like