Nested AND/OR expressions in template triggers

I couldn’t find much about nested boolean expressions in template triggers, so just documenting what I found to work. I wanted a trigger based on a template with a boolean expression, which includes a nested OR statement. The below is what I came up with, seems to work so far, but interested in any feedback. The logic goes:

  • trigger
    – sun elevation below 50 degrees AND (sun azimuth of 270 - 360 OR 0 - 40 degrees)

  • conditions (all conditions need to be met by default)
    – blind position is above 50%
    – solar panel ouput power is above 2000W (ie. the sun is shining to some extent)
    – sun elevation above 5 degrees (ie. the sun hasn’t set)

  • action
    – close blind to 50%

- alias: blind solar control
  trigger:
  - platform: template
  # nested OR statement in parentheses
    value_template: "{{ state_attr('sun.sun', 'elevation') < 50 and (state_attr('sun.sun','azimuth') < 40 or state_attr('sun.sun', 'azimuth') > 270) }}"
  condition:
  # Blind needs to be open > 50%
  - above: 50
    condition: device
    domain: cover
    entity_id: cover.dining_2
    type: is_position
  # solar panel output higher than 2000W
  - above: '2000'
    condition: numeric_state
    entity_id: sensor.power_ac_fronius_inverter_1
  # sun hasn't set
  - condition: template
    value_template: '{{ state_attr('sun.sun', 'elevation') > 5 }}'
  action:
  # close the blind half-way
  - domain: cover
    entity_id: cover.dining_2
    position: 50
    type: set_position
  mode: single