Close / open cover with sun position

Hello everybody

I am trying to make a blueprint in order to open / close cover with sun azimuth, elevation, outside temperature

Trigger will be time_pattern

blueprint:
  name: Close and open cover with sun position
  description: Close and open cover with sun elevation and azimuth
  domain: automation
  input:
    
    cover_target:
      name: Cover
      description: cover or groups you would like to control
      selector:
        target:
          entity:
            domain: cover
            device_class: shutter
    weather_service:
      name: Weather service
      selector:
        entity:
          domain: weather
    over_elevation:
      name: Over sun elevation
      description: Solar elevation. when sun is over this value, shutter could be triggered
      default: 32
      selector:
        number:
          min: -90.0
          max: 360.0
          unit_of_measurement: degrees
          step: 1.0
          mode: slider
    below_azimuth:
      name: Below sun azimuth
      description: Solar azimuth. when sun is below this value, shutter could be triggered
      default: 200
      selector:
        number:
          min: -360.0
          max: 360
          unit_of_measurement: degrees
          step: 1.0
          mode: slider
    over_azimuth:
      name: Over sun azimuth
      description: Solar azimuth. when sun is over this value, shutter could be triggered
      default: 89
      selector:
        number:
          min: -360.0
          max: 360
          unit_of_measurement: degrees
          step: 1.0
          mode: slider
    temp_ext_max:
      name: Maximal outdoor temperature
      description: Maximal outdoor temperature when Otdoor temp is over this value, shutter could be triggered
      default: 16 
      selector:
        number:
          min: 0
          max: 30.0
          unit_of_measurement: °C
          step: 1.0
          mode: slider

            


trigger:
  - platform: time_pattern
    minutes: '*'

variables:
  over_elevation: !input 'over_elevation'
  over_azimuth: !input 'over_azimuth'
  below_azimuth: !input 'below_azimuth'
  weather_service: !input 'weather_service'
  outdoor_temp:  '{{ state_attr(weather_service,''temperature'') }}'
  temp_ext_max: !input 'temp_ext_max'
  cover_target: !input 'cover_target'
  sun_elevation:  '{{ state_attr(''sun.sun'',''elevation'') }}'
  sun_azimuth:  '{{ state_attr(''sun.sun'',''azimuth'') }}'
  cover: '{{  states(''cover.cover_bureau'') }}'
  cover_check: '{{  states(''input_boolean.cover_bureau_is_auto_closed'') }}'

# Add a condition to ensure that this only triggers near sunset, in the evening
condition:
  # Check that it is after sunrise
  condition: and
  conditions:
    - condition: sun
      after: sunrise
      after_offset: '01:00:00'
    - condition: sun
      before: sunset
      after_offset: '00:30:00'



action:
  - variables:
      corresponding_cover: >
          {{ cover_target }}

          
  - service: system_log.write
    data:
      message: 'COVER - Close cover (Blueprint): {{ corresponding_cover }}'
      level: info      
  
  - service: >
      {% if( cover == 'open' and (outdoor_temp | float) > (temp_ext_max | float) and (sun_elevation | float) > (over_elevation | float) and (sun_azimuth | float) > (over_azimuth | float) and (sun_azimuth | float) < (below_azimuth | float) ) %}
        cover.close_cover
      {% elif ( cover_check == 'on' and cover == 'closed' and ((sun_azimuth | float) < (over_azimuth | float) or (sun_azimuth | float) > (below_azimuth | float) )  )%}
        cover.open_cover
      {% else %}
        cover.set_cover_position
      {% endif %}
    target: !input cover_target
  
  - service: >
      {% if( cover == 'open' and (outdoor_temp | float) > (temp_ext_max | float) and (sun_elevation | float) > (over_elevation | float) and (sun_azimuth | float) > (over_azimuth | float) and (sun_azimuth | float) < (below_azimuth | float) ) %}
        input_boolean.toggle
      {% elif ( cover_check == 'on' and cover == 'closed' and ((sun_azimuth | float) < (over_azimuth | float) or (sun_azimuth | float) > (below_azimuth | float) )  )%}
        input_boolean.toggle
      {% else %}
        input_boolean.reload
      {% endif %}
    target:
      entity_id: input_boolean.cover_bureau_is_auto_closed

  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: >
      {% if( (sun_elevation | float) > (over_elevation | float) and (sun_azimuth | float) > (over_azimuth | float) and (sun_azimuth | float) < (below_azimuth | float) ) %}
        cover.stop_cover
      {% else %}
        cover.set_cover_position
      {% endif %}
    target: !input cover_target
 

I have a problem with getting the states of my cover_target

Do you have any Idea, I would like to remove the fix variable

cover: '{{ states(''cover.cover_bureau'') }}'

and replace with a selectable one

cover_target: !input 'cover_target'

Thanks a lot

I think the issue is that target selector enables multiple entities, so you are unable to get the state directly from the target.
I didn’t understand what you want to archive with this check, although you can make a for loop to identify the state of each target.

Maybe set two variables, something like:

allopen_state: > 
  {%- set open = true -%}
  {%- for(target in cover_target) -%}
    {%- set open = open and is_state(target, 'open') -%}
  {%- endfor -%}
  {{ open }}
allclosed_state: > 
  {%- set closed = true -%}
  {%- for(target in cover_target) -%}
    {%- set closed = closed and is_state(target, 'closed') -%}
  {%- endfor -%}
  {{ closed }}

Hey,

Did you succeed in building this blueprint?

I am interested in trying it. Can you provide your final version please?

Thanks

Hello I have created the blueprint

https://community.home-assistant.io/t/manage-cover-with-sun-position-elevation-and-azimuth/405948

Best regards

Hey,

Great! I will have a look.

Thanks

Hi!

Thank you for your work with this blueprint, it is really nice and exactly what I’m looking for.
However I have some problems when testing it.
When the closing time is not set exactly right the cover will not close fully and the var_e will not be set. This makes the automation not open the cover when the time / weather requires it.

I beleive the cover.close_cover and cover.open_cover services trigger a full movement in most implementations. I wonder if the delay is really needed? Maybe a condition in both open and close parts can be introduced:

  • Close → if cover.current_position < 100 → trigger cover.close_cover, otherwise no action
  • Open → if cover.current_position > 0 → trigger clover.open_cover, otherwise no action

This way the extra variable and the logic around could be omitted. What do you think?