Can I insert ` endif ` after the ` else `

I’m trying to minimize repeat codes in this automation.yaml section. Is there a way to insert an endif after the else section? If I can, there will be only one delay: 5 seconds and one shell_command.. So in summary, if - then - else - endif!

Thanks in advance.

- id: '1660162051794'
  alias: initialize at start
  description: ''
  trigger:
  - platform: homeassistant
    event: start
  condition: []
  action:
  - if:
    - condition: state
      entity_id: binary_sensor.internet_693
      state: 'on'
      for:
        hours: 0
        minutes: 2
        seconds: 0
    then:
    - service: input_boolean.turn_on
      data: {}
      target:
        entity_id: input_boolean.693internet_status
    - delay:
        hours: 0
        minutes: 5
        seconds: 0
        milliseconds: 0
    - service: shell_command.internet_watchdog_init
      data: {}
    else:
    - service: shell_command.internet_watchdog_init
      data: {}
    - service: input_boolean.turn_off
      data: {}
      target:
        entity_id: input_boolean.693internet_status
    - delay:
        hours: 0
        minutes: 5
        seconds: 0
        milliseconds: 0
    - service: shell_command.internet_watchdog_init
      data: {}
  mode: single

Isn’t “end if” when the indentation is back to where it was when the - if was written?

No there is no concept of “endif” in the “if-then-else” action config.

Once the “if/else” is satisfied that section is terminated so there doesn’t need to be an “endif”.

It’s just implied.

I assume it’s this part you want to dry.

- service: shell_command.internet_watchdog_init
  data: {}

So I think:

- id: '1660162051794'
  alias: initialize at start
  description: ''
  trigger:
  - platform: homeassistant
    event: start
  condition: []
  action:
  - if:
    - condition: state
      entity_id: binary_sensor.internet_693
      state: 'on'
      for:
        hours: 0
        minutes: 2
        seconds: 0
    then:
    - service: input_boolean.turn_on
      data: {}
      target:
        entity_id: input_boolean.693internet_status
    - delay:
        hours: 0
        minutes: 5
        seconds: 0
        milliseconds: 0
    else:
    - service: shell_command.internet_watchdog_init
      data: {}
    - service: input_boolean.turn_off
      data: {}
      target:
        entity_id: input_boolean.693internet_status
    - delay:
        hours: 0
        minutes: 5
        seconds: 0
        milliseconds: 0
    
  - service: shell_command.internet_watchdog_init
    data: {}
  mode: single

Is probably after the end if

In my coding years, there is a way to end an if. In COBOL, a period ends the if. In LINUX (or UNIX or AIX), endif or fi, depending on the flavor. Example from my RPi 4:

echo "Test if, then,else,...."
export A1='1'
export A2="2"
echo -n 'Press Enter to continue... ';read ok
if [[ $A1 == $A2 ]];then
  echo echo "$A1 is equal to $A2"
else
  echo echo "$A1 is not EQUAL to $A2"
fi

I want to only code the delay of 5 minutes and then the shell_command once!

So, is the alignment in yaml coding signifies LOGICAL construct, instead of a syntactical coding of endif or fi to signify end of the condition(s)? I’m confused.

This is how I want it to be, logically:

at start of the home assistant, whether it be after a reboot or a manual (auto after an update) restart of the home assistant system

   - if my `binary_sensor` is on
      then 
         - turn the `input_boolean` switch to on
      else 
         - turn the `input_boolean` switch to off
   - I want to end the if condition here <---------------

So that I can then continue the 5 minute delay and shell_command. That way either on or off, the delay and the shell_command are executed.

I don’t believe python has an endif.

btw, this is better imo and is what the UI will generate. Notice the spacing…

- id: '1660162051794'
  alias: initialize at start
  description: ''
  trigger:
    - platform: homeassistant
      event: start
  condition: []
  action:
    - if:
        - condition: state
          entity_id: binary_sensor.internet_693
          state: 'on'
          for:
            hours: 0
            minutes: 2
            seconds: 0
      then:
        - service: input_boolean.turn_on
          data: {}
          target:
            entity_id: input_boolean.693internet_status
        - delay:
            hours: 0
            minutes: 5
            seconds: 0
            milliseconds: 0
      else:
        - service: shell_command.internet_watchdog_init
          data: {}
        - service: input_boolean.turn_off
          data: {}
          target:
            entity_id: input_boolean.693internet_status
        - delay:
            hours: 0
            minutes: 5
            seconds: 0
            milliseconds: 0
      
    - service: shell_command.internet_watchdog_init
      data: {}
  mode: single

This is neither of those. This is YAML.

in this case there is no “endif”. It is implied from the indentation/spacing.

I’m not even sure if the yaml specification has the if/then/else construct. It might be just created for HA by the HA developers. I do know that was just added recently as an option.

But as an alternate example the Jinja templating has an “if/elif/endif” construct tho.

Yeah, I guess indentation/spacing is the key to the logical construct of yaml coding. I am not familiar with python coding at all! So is python based on indentation or spacing, not free form based on logical construct?

I’ll try this indentation/spacing approach on what I intend to in logic I want to follow. And I’ll trigger an alert at the then, an alert at the else, then an alert at the end. We’ll see.

Thanks.

Now, I see it in the UI, too. At the end of the then, there is “Add Action”. If there is no more action under the then, you proceed to do the else. At the end of the else, there is “Add Action”. If there is no more action under the else, you proceed to the next “Add Action”, which aligns with the if. Perfect! Now understand how to end the if-then-else.

Thanks all for clarifying.