Sun azimuth trigger does not work

I try to trigger an automation based on the sun’s azimuth - but it does not trigger.

The goal is to close the blinds when the sun shines into the window, it is hot, nobody at home and the solar cells are very hot. Then the heat shall be kept out :wink:

# keep the heat out by closing the covers
# calculate azimuth here https://www.sonnenverlauf.de/
- alias: 'Close covers if nobody at home to keep the heat out'
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state.attributes.azimuth }}"
    above: 120.0
    below: 250.0
  condition:
    - condition: numeric_state
      entity_id: sensor.outdoor_temp
      above: 27.0
    - condition: state
      entity_id: 'group.family'
      state: 'not_home'
    - condition: state
      entity_id: 'group.all_covers'
      state: 'open'
    - condition: numeric_state
      entity_id: sensor.solar_temp
      above: 140.0
  action:
    - service: cover.close_cover
      data: 
        entity_id: group.all_covers
    - service: notify.ios_fabian_iphone
      data:
        title: "Rolladen wird wegen Hitze geschlossen"
        message: "Außentemperatur: {{ states.sensor.outdoor_temp.state }} °C"

Any help appreciated :slight_smile:

Thanks

Hi @nodomain, seems everything is ok with your trigger.
Tested it and it doesn`t work. :unamused:
Found this topic about the same problem.
Maybe a bug?

what is the output of “”{{ state.attributes.azimuth }}"" in when you paste it in the template developer panel?

Also, are all your conditions being met? When you know the trigger conditions are being met check the status of your conditions to ensure they can be met.

Finally, try dropping the decimal point (120.0 to 120). Not sure if it would affect anything but it isn’t needed so I would drop it.

Won’t that only trigger when the sun goes above or below those values?

So it would only trigger at the point the sun is going above 120.0 and again only trigger when going below 250.0 (but it would only trigger if all those conditions match at those times)

Do you want it to trigger as long as the sun is between those two values?

Nothing, this is weird - but I think this is because as part of the trigger it is referenced in a different way, right?

If I use

{{ states.sun.sun.attributes.azimuth }}

a value is returned. Am I now right or wrong?

I mean between - am I holding it wrong?
My understanding is that the sun component fires an event whenever the azimuth/elevation is changed. This triggers this automation which in turn is executed when the conditions are met:


So I would expect that the covers are closed during that time of the day when we have full sun on our big window.

Or am I on the completely wrong track?

This looks correct to me. I didn’t think your original value template looked correct but without the tool I am horrible at developing templates that work. I am very dependent on trial and error when developing templates.

I haven’t personnally used above and below together but the docs don’t seem to point that it isn’t allowed. I would see how it works with the updated template in the trigger and go from there. Keeping in mind that all your conditions have to be met at the same time.

1 Like

My inspiration came from here: https://home-assistant.io/cookbook/automation_sun/

What do you mean with the “updated” template? How shall I update it?

Change your trigger from

  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state.attributes.azimuth }}"
    above: 120.0
    below: 250.0

to

  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ states.sun.sun.attributes.azimuth }}"
    above: 120.0
    below: 250.0

Ok, let’s see what happens tomorrow.

Dont mean to thread crash but what tool do you speak of?

I believe that this will only trigger when the sun either rises above your value or falls below it, but at all other times in between those values there is nothing to trigger the automation. (Think of a trigger as a specific moment in time when a state changes from X to Y).

You could try a time trigger for every 60 seconds with a condition that the sun is between those two values and your other conditions are also met

Other more experienced users might prove me wrong, or have better ideas of how to produce a trigger though!

Got it. I’ll give it a try.

This is exactly what I want to do. Trigger when the sun is between 110-250 degrees and conditions illuminace is higher than 4000 lux and temp outside above 25 degrees Celsius.

Have you gotten your automation to work and or developed it further?

Just go for it, the OP had a problem 4 years ago …
You might consider using the temperature as a trigger and lux and azimuth as condition.
And use another control (like an input_boolean switch) so it won’t be triggered when the temperature is falling below and back above your value while in the azimuth range.

I’ve no problems trying, but sometimes the little things you haven’t thought about evolves your automations to perfection. So I’m sorry if I wasn’t clear, I want suggestions…
Like what if the trigger (temp) goes above the value and the condition (azimuth or illumination) aren’t met… what is the best way to trigger again, do I use multiple triggers or?

Okay, I see. I believe it depends what you want to do respectively why. I have been a bit hasty yesterday when telling you should use the temperature as trigger.

Aside from prevent heating up the living room and kitchen in summer, I want to protect my plants in my living room (south) and kitchen (west) window all year round, because some of them might burn when getting to much sun even in winter. So temperature or illumination are no conditions for me, but you might have other reasons.

I trigger my covers when the cloud coverage is below a certain amount and the condition is the sun within an azimuth range.

Just make sure you don’t have your screen go up and down like crazy, cause that could happen easily with every change of a sensor… I have a lot of screen-automations, probably too many, but it works for me. But to start of easy, and to make sure your screen won’t go up and down every minute, I would suggest something like this:

# Screen down

- alias: Down
  trigger:
# Or if you want you can do this ofcourse 3 or more times an hour to check...
  - platform: time_pattern
    minutes: 0
  - platform: time_pattern
    minutes: 30
  condition:
  - condition: numeric_state
    entity_id: sensor.illuminace
    above: 4000
  - condition: numeric_state
    entity_id: sensor.outdoor_temperature
    above: 25
  - condition: numeric_state
    entity_id: sensor.azimuth
    above: 110
    below: 250
  action:
  - service: cover.set_cover_position
    data:
      entity_id: cover.covername
      position: 25

# Screen up

- alias: Up
  trigger:
  - platform: time_pattern
    minutes: 0
  - platform: time_pattern
    minutes: 30
  condition:
# Or play around with AND/OR conditions if needed...
  - condition: numeric_state
    entity_id: sensor.illuminace
    below: 4000
  - condition: numeric_state
    entity_id: sensor.outdoor_temperature
    below: 25
  - condition: numeric_state
    entity_id: sensor.azimuth
    above: 110
    below: 250
  action:
  - service: cover.set_cover_position
    data:
      entity_id: cover.covername
      position: 100

# Screen up when above 250

- alias: Up2
  trigger:
  - platform: time_pattern
    minutes: 0
  - platform: time_pattern
    minutes: 30
  condition:
  - condition: numeric_state
    entity_id: sensor.azimuth
    above: 250
  action:
  - service: cover.set_cover_position
    data:
      entity_id: cover.covername
      position: 100

And a sensor (not really needed if you use the attribute itself, but I’s easy to just make one and if you make (like me) a lot of other automations with azimuth):

  - platform: template
    sensors:
      azimuth:
        unit_of_measurement: '°'
        value_template: "{{ state_attr('sun.sun', 'azimuth') | round(0) }}"
1 Like

Thanks for your great example!

This one triggers every 30 minutes, the conditions are:

  • Azimuth (bearing of the sun) (used https://www.suncalc.org to get the values). Between 110-250.
  • Illumination in Lux, outside. Over 4000 Lux
  • Temperature outside. Over 25 degrees Celcius.
    When they are all met the covers drop to 19% open (I use this value as a condition in the “up” automation.
#Covers down to a uniqe position, so it can be used as a condition.
#That way it will leave the covers down if I've manually lowered them
alias: Rullgardiner Soligt ner
description: ''
trigger:
  - platform: time_pattern
    minutes: '0'
  - platform: time_pattern
    minutes: '30'
condition:
  - condition: numeric_state
    entity_id: sun.sun
    value_template: '{{ states.sun.sun.attributes.azimuth }}'
    above: '110'
    below: '250'
  - condition: numeric_state
    entity_id: sensor.mi_balkongen_illuminance
    above: '4000'
  - condition: numeric_state
    entity_id: sensor.mi_balkong_temperature
    above: '25'
  - condition: numeric_state
    entity_id: cover.rullgardin_hoger
    attribute: current_position
    above: '80'
action:
  - device_id: de2b21f21bd6d98a6d2d2c48e3ca9b8a
    domain: cover
    entity_id: cover.rullgardin_vanster
    type: set_position
    position: 19
  - device_id: e935029ae6ea28f72dc12dbf0db655b3
    domain: cover
    entity_id: cover.rullgardin_hoger
    type: set_position
    position: 19
mode: single

The “up” automation also triggers every 30 minutes.
Checking that the position is 19% (means they have been pulled down by the automation above)
And either of the following:

  • Illumination below 4000 Lux
  • Temperature below 24 degrees celcius.
  • Azimuth higer than 250 (250-360) and under 110 (0-110)
#Covers up
alias: Rullgardiner Molnigt upp
description: ''
trigger:
  - platform: time_pattern
    minutes: '0'
  - platform: time_pattern
    minutes: '30'
condition:
  - condition: numeric_state
    entity_id: cover.rullgardin_hoger
    above: '18'
    value_template: '{{ states.cover.rullgardin_hoger.attributes.current_position }}'
    below: '20'
  - condition: or
    conditions:
      - condition: numeric_state
        entity_id: sensor.mi_balkongen_illuminance
        below: '4000'
      - condition: numeric_state
        entity_id: sensor.mi_balkong_temperature
        below: '24'
      - condition: numeric_state
        entity_id: sun.sun
        attribute: azimuth
        above: '250'
        below: '110'
action:
  - device_id: de2b21f21bd6d98a6d2d2c48e3ca9b8a
    domain: cover
    entity_id: cover.rullgardin_vanster
    type: set_position
    position: 99
  - device_id: e935029ae6ea28f72dc12dbf0db655b3
    domain: cover
    entity_id: cover.rullgardin_hoger
    type: set_position
    position: 99
mode: single

The only thing is the between value in the “up” automation for the current cover position… can’t get it to work with absolute value.
Otherwise it looks like it’s working.

I could offcourse use an input_boolean instead of the fixed value of 19, but that would mean that I need to remeber to reset that each time I manually use the blinds.

good thread here. Getting lots of ideas even though its been some time ago.
I just have curtains motorized and looking for the auto close on sun . I have good data on solar panels and or illuminacnce from weather station into sensors and i’m figuring out what azimuth and time the window is exposed.
Currently the polling of curtain position is bit bad, that needs attention first.