Template error on local development when checking entry variable
You are close, instead of checking entry|length, you can do {% if entry is defined %}

{% if entry is defined and entry.sectionId == 1 %}


So if I am understanding correctly, the full code will be:

{% if entry is defined and entry.sectionId == 1 %}

    //  Redirect to homepage

{% else %}

    // Do some checking into whether there's a corresponding entry, etc.

{% endif %}