Help with sun elevation automation

Most likely the elevation went from below 72 to above 73 in one step yesterday.

Options:

  • Use the next_noon attribute of the Sun integration.
  • Create a Trend sensor to detect when the sun starts descending (negative gradient).

If that’s the complete code, you have no action that actually closes the cover. I assume you’ve just omitted that from the snippet above?

Beautiful coding in the condition though :kissing_heart:

Thinking about this a bit more that is probably not going to work. As soon as it becomes solar noon the attribute will change to tomorrow’s noon.

You can likely get around this with the Sun2 integration.

I will test this today because it seems good for what I need. (didn’t know about trend platform at all)
Thanks

Yes, I have action also in my code but didn’t post it.
The code is given in another topic from forum member and it is very efficient.

May I ask, if I can use the sun rising attribute in a template?
I see now that is still rising = true maybe later (after 73) will give false and then I could have 2 conditions like rising=false and below 68?

next_dawn: '2020-07-14T02:42:48+00:00'
next_dusk: '2020-07-13T18:18:48+00:00'
next_midnight: '2020-07-13T22:30:36+00:00'
next_noon: '2020-07-13T10:30:25+00:00'
next_rising: '2020-07-14T03:13:26+00:00'
next_setting: '2020-07-13T17:48:05+00:00'
elevation: 69.82
azimuth: 140.26
rising: true
friendly_name: Sun

I have added the binary sensor from the trend example which right now is on (so in my case a condition to be off) but if I can do the job with the sun integration (above) I would prefer it (to keep the minimum of sensors)

binary_sensor:
  - platform: trend
    sensors:
      sun_rising:
        entity_id: sun.sun
        attribute: elevation

Yes that should work.

can you help a little with the template please? :slight_smile:
I don’t know how to choose the false…

{{ not(state_attr('sun.sun', 'rising')) }}

or

{{ (state_attr('sun.sun', 'rising') == False) }}

Note the False is not quoted, because it’s a value not a string.

1 Like

I tried both in template tools and they are correct, but file editor doesn’t accept any

condition:
    - condition: template # Only between these dates
      value_template: >
        {% set fromts = '06-01' %}  ##### apo 01 Iouniou mexri 30 Augoust
        {% set tots = '08-30' %}
        {{ fromts <= now()|as_timestamp|timestamp_custom('%m-%d', False) <= tots }}
    - condition: template
      value_template: {{ (state_attr('sun.sun','rising') == False) }}

I get this

missed comma between flow collection entries at line 812, column 56:
     ... { (state_attr('sun.sun','rising') == False) }}
                                         ^

Single-line value_templates must be enclosed in quotes (see rule 3):

value_template: "{{ (state_attr('sun.sun','rising') == False) }}"
1 Like

here is the revised code. I test it to 61 degrees but it doesn’t work

# Covers Close in SummerTime  ******************************************************************
- id: Covers Close in SummerTime
  alias: Covers Close in SummerTime
  trigger:
    - platform: numeric_state
      entity_id: sun.sun
      value_template: "{{ state.attributes.elevation }}"
      below: 61


  condition:
    condition: and
    conditions:
      - condition: template # Only between these dates
        value_template: >
          {% set fromts = '06-01' %}  ##### apo 01 Iouniou mexri 30 Augoust
          {% set tots = '08-30' %}
          {{ fromts <= now()|as_timestamp|timestamp_custom('%m-%d', False) <= tots }}
      - condition: template 
        value_template: "{{ (state_attr('sun.sun','rising') == False) }}"

  
  action:
    - service: cover.close_cover
      entity_id: cover.50758014840d8e91f036 # erker
    - service: cover.close_cover
      entity_id: cover.50758014840d8e91eaa2 # bedroom
    - service: cover.close_cover
      entity_id: cover.50758014840d8e918632 # kid

Not sure your trigger is correct. Try:

  trigger:
    - platform: numeric_state
      entity_id: sun.sun
      value_template: "{{ state_attr('sun.sun', 'elevation') }}"
      below: 61

I am trying it right now. However I have this trigger in another automation and it is working.

What it seems wrong is in the second condition the " - " sign
it is black in my configurator instead of red as all other conditions. is that meaning something?

sign

edit: didn’t work…

ha ha.
Now I realised your joke! :laughing:

I had to move to the other thread because I noticed that there is a problem (don’t know why) with the template

value_template: >
        {% set fromts = '06-01' %}  ##### apo 01 Iouniou mexri 30 Augoust
        {% set tots = '08-30' %}
        {{ fromts <= now()|as_timestamp|timestamp_custom('%m-%d', False) <= tots }}

I tried with this and it is working

value_template: >
          {% set from = '1.6.2020' %}
          {% set to = '30.8.2020' %}
          {{ as_timestamp(strptime(from,"%d.%m.%Y")) 
          <= as_timestamp(now()) <
          (as_timestamp(strptime(to,"%d.%m.%Y"))+60*60*24) }}
1 Like

Your comment (##### etc) broke it. Try without. Your new solution will only work this year.

1 Like

I just read through this topic. How about using this for a trigger:

  trigger
    - platform: template
      value_template: "{{ not state_attr('sun.sun', 'rising') }}"

This will become True, and hence trigger the automation, when the rising attribute of sun.sun changes to False (i.e., the sun begins going down.) It will not trigger again until the attribute changes to True (starts to rise) and then back to False (starts to go down.) I.e., it will trigger once a day when the sun starts to go down. I believe that is what you’re looking for.

Also, the condition in your OP works because of the way the strings are formatted. Normally doing string comparison of numbers, dates, etc. can be unpredictable. But, I have to ask – why do you use False in the timestamp_custom() function? That means you want UTC, not the local time zone. You’re probably “getting away” with this because the condition is only evaluated at midday, when the UTC date and local time zone date are more than likely the same. I’d suggest, however, you really should use True (i.e., use local time zone.)

2 Likes

You’re quite right, that should be True, and yes, it relies on correctly-formatted dates sorting as strings. I was the original author of that condition.

If you want a shorter version, code golf style:

value_template: "{{ '06-01'<=(now()|string)[5:10]<='08-30' }}"

Works until the year 10,000.

1 Like

How about this for your condition:

  condition:
    - condition: template # Only between these dates
      value_template: >
        {% set cur = now().date() %}
        {% set from = strptime(cur.year ~ "-06-01", "%Y-%m-%d").date() %}
        {% set to = strptime(cur.year ~ "-08-30", "%Y-%m-%d").date() %}
        {{ from <= cur <= to }}

Thanks a lot both!
I will try it and let you know how it performs, which I am certain will be ok.

I believe there are still two space characters that can be removed. :stuck_out_tongue_winking_eye:

1 Like