Trigger On SUN.SUN

Hi all,

I have had an automation working for the last year without problem. I have changed JUST the trigger and not the conditions or action, and it is now not working.

The new trigger is;

trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation + ((state.attributes.azimuth - 285)/ 3) }}'
    above: 0

The figures that have just passed for me are;

sun.sun
elevation: 60.05
azimuth: 108.93

According to my maths HA should calculate 1.376 and trigger the action but it doesn’t.

Can anyone please help me with what is probably a basic mistake?

Thanks in advance
John.

Well my first question is: Why is azimuth used in your calculation at all? Azimuth does not determine the elevation in relation to the horizon…

Anyways, I don’t see anything wrong with your trigger. Your math checks out. The format looks good. The syntax is correct. Can you please post the remainder of your automation?

Have you tried your template in developer tools?

Thanks for the reply.

This automation is all about closing a blind when the sun shines through a particular window in my house.

In the old version, I used elevation, as the sun comes over the house, but this is not consistent through the year. I also tried azimuth, as the sun comes around the house, but again this is not consistent.

I then discovered the world of sun shade calculations on-line, and it turns out that you need to take into account azimuth AND elevation, and the little calculation I have included is the right solution for working out when the blind should close each day. The problem is I cannot get it to work.

Old version (it works)

- id: OldO
  alias: Close Kitchen Blind with PM sun and 25 degrees
  initial_state: True
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.azimuth }}'
    above: 227
  condition:
    condition: and
    conditions:
      - condition: time
        after: '14:00:00'
        before: '18:00:00'
      - condition: numeric_state
        entity_id: sensor.yr_temperature
        above: 24.0
      - condition: numeric_state
        entity_id: sensor.yr_cloudiness
        below: 35        
  action:
    - service: cover.set_cover_position
      data_template:
        entity_id:
          - cover.kitchen_patio_blind_level
        position: 30

New Version (doesn’t work). In my testing today you can see I have deleted the time condition as I was doing this in the morning, and I also changed the cloud cover condition to 99 as it was sat at about 40%. The temp was 37 degrees so that condition was ok.

- id: OldO
  alias: Close Kitchen Blind with PM sun and 25 degrees
  initial_state: False
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation + ((state.attributes.azimuth - 285)/ 3)}}'
    above: 0
  condition:
    condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.yr_temperature
        above: 24.0
      - condition: numeric_state
        entity_id: sensor.yr_cloudiness
        below: 99        
  action:
    - service: cover.set_cover_position
      data_template:
        entity_id:
          - cover.kitchen_patio_blind_level
        position: 30

Ah ok. Well, i’m not sure why it doesn’t update. Maybe try a simple old value_template instead of a numeric_state template:

- id: OldO
  alias: Close Kitchen Blind with PM sun and 25 degrees
  initial_state: False
  trigger:
    platform: template
    value_template: '{{ ( states.sun.sun.attributes.elevation + (states.sun.sun.attributes.azimuth - 285) / 3 ) > 0 }}'

Perhaps the / causes this. Try rounding it, or making an int:

value_template: '{{ state.attributes.elevation + (((state.attributes.azimuth - 285)/ 3)|int) }}'

And again, try the template editor:

http://your-ip:8123/dev-template

I’m an idiot, a fool and a twit…

It works.

I have had a long standing habit of changing initial state from True to False when messing about with automations. As i have not changed much in the house in the last 6 months I have become out of practice and less disciplined in my testing. This means I set the state to Falso, made changes, made some more changes all with the automation OFF.

I feel like an idiot, but just as a courtesy to @petro and @nickrout I had to be honest and own up.

Thank you both for your help!

3 Likes

Hey
Well done :slight_smile:
I have a request, can you explain more how you set the pattern for automation. My window on the wall is at 240 degrees SW and I do not know how to use this information for calculations.

Hi.

I never quite got this to work, and stopped playing with it last summer, although I will start again this year. I started looking at solar websites and also sites that talk about the path of the sun throughout the year, but I never managed to sort out the maths to apply this to my house. I am sure somewhere there is an equation to work out when the sun will shine through a particular window, but I have not found it yet.

Sorry.