Using 'or' in value template

From the automation to control the lights in my bedroom:`

      sequence:
      - choose:
        - conditions:
          - condition: template
            value_template: '{{ states(''sensor.dusk2dawn'') or is_state(''input_boolean.sleeping'',
              ''on'') }}'
          sequence:
          - service: light.turn_on
            data:
              brightness_pct: 25
            target:
              entity_id: light.bed1_bedside

The value template doesn’t give me the expected results.

Investigating the problem with Developer Tools:

states('sensor.dusk2dawn'): 
  {{ states('sensor.dusk2dawn') }}
is_state('input_boolean.sleeping','on'): 
  {{ is_state('input_boolean.sleeping','on') }}
states('sensor.dusk2dawn') or is_state('input_boolean.sleeping','on'): 
  {{ states('sensor.dusk2dawn') or is_state('input_boolean.sleeping','on') }}
is_state('input_boolean.sleeping','on') or states('sensor.dusk2dawn'): 
  {{ is_state('input_boolean.sleeping','on') or states('sensor.dusk2dawn') }}

(In Developer Tools states/is_state following ‘or’ is highlighted as is ‘or’).
These are the results:

states('sensor.dusk2dawn'): 
  False
is_state('input_boolean.sleeping','on'): 
  True
states('sensor.dusk2dawn') or is_state('input_boolean.sleeping','on'): 
  False
is_state('input_boolean.sleeping','on') or states('sensor.dusk2dawn'): 
  True

The two ‘or’ conditions should give the same result.

Again, in Developer Tools:

true or false: {{ true or false }}
true or true: {{ true or true }}
false or false: {{ false or false }}
false or true: {{ false or true }}

(In Developer Tools true/false following ‘or’ is highlighted as is ‘or’).
These are the results:

true or false: True
true or true: True
false or false: False
false or true: True

These are the expected results for an inclusive OR function.

What am I doing wrong?

Your dusk2dawn sensor is not logical false, it’s the string “False”, so you’re looking at the second of:

false or true: {{ false or true }}
"False" or true: {{ "False" or true }}

Use:

{{ is_state('sensor.dusk2dawn'), 'False' }}

or turn it into a binary_sensor.

You can just convert them both to booleans with a fallback of none. So if your sensor doesn’t exist, it will be unknown.

            value_template: '{{ states(''sensor.dusk2dawn'') | bool(none) or states(''input_boolean.sleeping'') | bool(none) }}'

Putting {{ is_state(‘input_boolean.sleeping’,‘on’) }} or {{ states(‘sensor.dusk2dawn’) }} individually into Developer Tools / Template tells me “Result type: boolean” for each of them and shows the values as “true” and “false” respectively.

The dusk2dawn sensor is defined as:

template:
  - sensor:
      - name: "Dusk2Dawn"
        state: >
          {{ as_datetime(states('input_datetime.last_dusk')).day == utcnow().day 
             or 
             as_datetime(states('input_datetime.last_dawn')).day != utcnow().day }}

Sorry if I’m being difficult but want to fully understand how to avoid this problem again.

The template tool sometimes tries to be too clever for its own good. It tells me that {{ states('sensor.outside_temperature') }} is a number, but throws an error if I try to add another number to it.

States are always strings. The logical operators like or can deal with the 'on' and 'off' states of a binary sensor, but won’t deal with the string “False”. As @petro suggested, you could use the bool() function which will convert "False" to false (listed here), but much better to use an appropriate type of entity, the binary sensor in this case:

template:
  - binary_sensor:
      - name: "Dusk2Dawn"
        state: >
          {{ as_datetime(states('input_datetime.last_dusk')).day == utcnow().day 
             or 
             as_datetime(states('input_datetime.last_dawn')).day != utcnow().day }}

Same template, but different outcome: 'on' if true, rather than Python True turned into a string.

Then you will have binary_sensor.dusk2dawn that will have state 'on' or 'off' and can be evaluated in logical operations.

Troon, brilliant answer. Thank you.