Sun Elevation and Azimuth Binary Threshold Sensor

I would like to set up a threshold sensor for sun azimuth and elevation to trigger my covers.
Checking the config passes the test, but I get the following notification. Any ideas welcome!

binary_sensor:
# Sun Azimuth south side
  - platform: threshold
    name: "sun azimuth south"
    upper: 150
    lower: 180
    entity_id: sun.sun
    value_template: "{{ states.sun.sun.attributes.azimuth }}"

# Sun Elevation south side
  - platform: threshold
    name: "sun elevation south"
    upper: 30
    entity_id: sun.sun
    value_template: "{{ states.sun.sun.attributes.elevation }}

1 Like

Like the warning says, the threshold binary_sensor does not use the value_template parameter. All the accepted parameters are listed here.

See https://www.home-assistant.io/components/binary_sensor.threshold/

Instead of HA ignoring the value_template parameters, you are now required to fix the problem.

I see. Basically nice idea, but wrong approach. Would need to handle it in conditions instead.

Or you create template sensors for azimuth and elevation and use those in the binary trend sensors.

If you ever get something working, please post back, I think that I will need something similar in the near future.

thanks @anon34565116 - Template Sensor was doing the trick. Now I can conveniently optimize the angles and trigger my automation.

binary_sensor:
  - platform: template
    sensors:
      sun_elevation:
        friendly_name: "Sun Elevation"
        unit_of_measurement: 'degrees'
        value_template: "{{ state_attr('sun.sun', 'elevation') }}"

  - platform: template
    sensors:
      sun_azimuth:
        friendly_name: "Sun Azimuth"
        unit_of_measurement: 'degrees'
        value_template: "{{ state_attr('sun.sun', 'azimuth') }}"

sensor:
# Sun Azimuth south east front of building
  - platform: threshold
    name: "sun azimuth se"
    upper: 150
    lower: 180
    entity_id: sensor.sun_azimuth

# Sun Azimuth south west front of building
  - platform: threshold
    name: "sun azimuth sw"
    upper: 240
    lower: 270
    entity_id: sensor.sun_azimuth

# Sun Elevation
  - platform: threshold
    name: "sun elevation"
    upper: 30
    entity_id: sensor.sun_elevation
5 Likes

Hello @outlander4000, this sounds very much like a good foundation for what I’m after. I would like to roll down my blinders when the sun hits a particular side of my flat. I see that you now can define a the upper and lower values for both the azimuth and elevation, but I’m not sure how you apply the configuration above. I’m just wondering if you could now report on where you’ve got since February? Many thanks!

Hi Kitus, my blinds are working satisfactory this summer and I have a mix of conditions and triggers to meet expectations. Probably still not perfect and best option might be to have a sun intensity sensor on every side of the house.
First I have setup template sensors for azimuth and elevation. These allow more comfortable handling.

  - platform: template
    sensors:
      sun_elevation:
        friendly_name: "Sun Elevation"
        unit_of_measurement: 'degrees'
        value_template: "{{ state_attr('sun.sun', 'elevation') }}"

  - platform: template
    sensors:
      sun_azimuth:
        friendly_name: "Sun Azimuth"
        unit_of_measurement: 'degrees'
        value_template: "{{ state_attr('sun.sun', 'azimuth') }}"

Also I have created binary sensors as follows. To set the azimuth angles correct I was monitoring sun in and out in the windows for a day.

# Sun Azimuth south east front of building
  - platform: threshold
    name: "sun azimuth se"
    upper: 190
    lower: 60
    entity_id: sensor.sun_azimuth

# Sun Azimuth south west front of building
  - platform: threshold
    name: "sun azimuth sw"
    upper: 280
    lower: 135
    entity_id: sensor.sun_azimuth

# Sun Azimuth nort west front of building
  - platform: threshold
    name: "sun azimuth nw"
    upper: 300
    lower: 210
    entity_id: sensor.sun_azimuth

# Sun Elevation
  - platform: threshold
    name: "sun elevation"
    upper: 30
    entity_id: sensor.sun_elevation

In automation now I do a partial closing when sun conditions, temperature and yr are met:

- id: '1561820317276'
  alias: Jalousien southwest summer partial closing
  trigger:
  - entity_id: binary_sensor.sun_azimuth_sw
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: binary_sensor.sun_elevation
    state: 'on'
  - above: '23'
    condition: numeric_state
    entity_id: sensor.meq0832505_temperature
  - below: '9'
    condition: numeric_state
    entity_id: sensor.yr_symbol
  action:
  - data:
      entity_id:
      - cover.meq0515914
      - cover.meq0517205
      position: 30
    service: cover.set_cover_position
  - entity_id:
    - cover.office_einfahrt
    - cover.schlafen_einfahrt
    service: cover.stop_cover
  initial_state: 'true'

When it is a very hot day I close completely 40 minutes after partial closing:

- id: '1561820317278'
  alias: Jalousien southwest summer full closing
  trigger:
  - entity_id: binary_sensor.sun_azimuth_sw
    for: 00:40:00
    from: 'off'
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: binary_sensor.sun_elevation
    state: 'on'
  - condition: state
    entity_id: binary_sensor.sun_azimuth_sw
    state: 'on'
  - above: '27'
    condition: numeric_state
    entity_id: sensor.meq0832505_temperature
  - below: '9'
    condition: numeric_state
    entity_id: sensor.yr_symbol
  action:
  - data:
      entity_id:
      - cover.meq0515914
      - cover.meq0517205
      position: 5
    service: cover.set_cover_position
  - entity_id:
    - cover.office_einfahrt
    - cover.schlafen_einfahrt
    service: cover.close_cover
  initial_state: 'true'

When the sun is leaving the blinds are going up:

- id: '1561832416949'
  alias: Jalousien southwest summer opening
  trigger:
  - entity_id: binary_sensor.sun_azimuth_sw
    from: 'on'
    platform: state
    to: 'off'
  condition: []
  action:
  - data:
      entity_id:
      - cover.meq0515914
      - cover.meq0517205
      position: 100
    service: cover.set_cover_position
  - entity_id:
    - cover.office_einfahrt
    - cover.schlafen_einfahrt
    service: cover.open_cover
  initial_state: 'true'

On the northwest side I additionally use the elevation as a trigger and a light sensor.
Another automation is pulling up all blinds when it is becoming very cloudy.
I will need to check and fine tune how it works when we are getting closer to autumn.

8 Likes

Hello @outlander4000, I was hoping you could edit your post above (Mar '19) for us noobs struggling with HASS. I finally figured out that you have reversed sensor: and binary sensor: in the quoted post and also the lower: and upper: values for the range are reversed. I did think it was a strange way to specify a range but after much reading and trial and error I did figure it out It might seem trivial for more experienced users but I struggled with this for a day or more but I do have them working now!
Thanks, Daniel.

Hi @dbrunt, I just checked post 8 from August 19 and I confirm it is the code I am currently running. The only parameter I have adjusted is the yr-symbol from 9 to 4. This is still not perfect, as on very hot days, when the shutters are fully closed and clouds coming up, the yr symbol is not accurate enough for the local situation. The automations working through the year and I do not need to touch them at all. Hope you are experiencing similar success.

I was referring to post #6! I jumped on it without reading further and could not get it to work!

I have it working now after swapping sensor: and binary sensor: and swapping low/high values for range.

Then I discovered post #8

Hi @outlander4000 I realize I never took the time to thank you for your detailed explanation above. I have not put it to action though as I had a few doubts that I did not manage to address back then, and I ended up forgetting about all of these.

One thing, do you use node-red at all? I bumped into some component that may be exactly what you need.

Hi @kitus,

I was about to implement that use case on node-red, and I would be glad to hear about the component !
Very interesting thread by the way

I wanted to thank you guys for starting something for me that accelerated my automations… I just implemented sun.sun automation (great add on IMO) and I just finished my first power shade that I bought on Ali-express, that I connect via 433 and a flashed Sonoff bridge. Once I had one fully integrated, I loved it so much that I bought 9 more with new shades to now more fully automate the bedroom, bathroom, and one giant open area that gets flooded with sun.

I live in Texas and - well - it gets hot here. 105F / 40C during the summer. I am using this for a combination of making the Bedrooms blacked out, but then when we wake up (bed senses that we left) open things up in the morning until it gets too much sun.

I want to see what is better, getting the calculated sun figures to trigger things, or whether I just add some TSL2651 LUX sensors to my NodeMCU multi-sensor build and trigger them based on actual ambient light. What I like about the calculated method is that it does not involve any more devices to get it done - more failsafe IMO. I am putting the finishing touches on a new multi-sensor box that I plan on putting in several spots in the house so I may just tinker with both to see what works best - or let the ambient light be a trigger that is outside of the other calculated option you all put together here. Like @mathieu-mp I was originally planning to build the automations out in Node red. I may still do this given that I also want the shades to operate based on house occupancy as well as light.

Again - I was happy to see this was already started - love this community!

hello @outlander4000 ,

I’m very much into this topic, trying to solve it ever since I installed my first HA instance. Please tell me something: is this solution working all year round, season independent? Obviously the sun’s angle is different in spring than in summer or autumn, as the elevation is different…

Another question, if the answer to the above question is yes: can you follow Sun’s position with the cover, opening and closing it in incremental steps? What I mean:

  • if it’s open in the morning, and the Sun starts hitting it, then start to close the cover in 5% incrementals, as the sun’s elevation or azimuth is changing over time (eg: after every 5 degree change open 5% on the cover), until the predefined range is reached.

Sorry if the question sounds noob, I don’t get the basic idea behind sun based blind controls, yet, so any explanation would be greatly appreciated…

What also struck me is that this topic is not as mainstream as one would expect it, and there’s no plugin, custom component already created by community for this topic (season independent window cover adjustment based on sun’s direction and intensity)…

1 Like

I am trying to do something similar but with only one binary sensor and it seems like something is not working. I wonder if one of the templates guru could take a look at it and tell me what I am doing wrong. Also not sure if I am supposed to use INT or FLOAT. Basicly my use case is that I want a sensor to turn on when the sun angle is between 9 and 19 and the sun elevation is between 90 and 130.
Even though I can see that the current values fall within the range, it always returns “false”

- platform: template
  sensors:
    office_sun_exposure:
      friendly_name: "Office Sun Exposure"
      value_template: >
        {% if ( (states.sensor.solar_angle | int > 9) and 
        (states.sensor.solar_angle | int < 19) and
        (states.sensor.solar_azimuth | int > 90) and
        (states.sensor.solar_azimuth | int < 130)) %}true
        {% else %}false
        {% endif %}

I would like to use the azimuth SunCalc - sunrise, sunset, shadow length, solar eclipse, sun position, sun phase, sun height, sun calculator, sun movement, map, sunlight phases, elevation, Photovoltaic system, Photovoltaic value for this since i can specify the degrees here. but i can’t seem to get it working. I am constantly stuggeling with the Invalid config for [sensor]: required key not provided @ data[‘platform’]. Got None. error.

this is the code i use in my sensors.yaml

binary_sensor:
  - platform: template
    sensors:
      sun_elevation:
        friendly_name: "Sun Elevation"
        unit_of_measurement: 'degrees'
        value_template: "{{ state_attr('sun.sun', 'elevation') }}"

  - platform: template
    sensors:
      sun_azimuth:
        friendly_name: "Sun Azimuth"
        unit_of_measurement: 'degrees'
        value_template: "{{ state_attr('sun.sun', 'azimuth') }}"

sensor:
# Sun Azimuth south east front of building
  - platform: threshold
    name: "sun azimuth s"
    upper: 185
    lower: 200
    entity_id: sensor.sun_azimuth

# Sun Azimuth south west front of building
  - platform: threshold
    name: "sun azimuth nw"
    upper: 315
    lower: 300
    entity_id: sensor.sun_azimuth

# Sun Elevation
  - platform: threshold
    name: "sun elevation"
    upper: 30
    entity_id: sensor.sun_elevation

This didn’t work, and then i found the remarks of someone saying in the topic that he should switch sensor and binary_sensor so i tried that

sensor:
  - platform: template
    sensors:
      sun_elevation:
        friendly_name: "Sun Elevation"
        unit_of_measurement: 'degrees'
        value_template: "{{ state_attr('sun.sun', 'elevation') }}"

  - platform: template
    sensors:
      sun_azimuth:
        friendly_name: "Sun Azimuth"
        unit_of_measurement: 'degrees'
        value_template: "{{ state_attr('sun.sun', 'azimuth') }}"

binary_sensor:
# Sun Azimuth south east front of building
  - platform: threshold
    name: "sun azimuth s"
    upper: 185
    lower: 200
    entity_id: sensor.sun_azimuth

# Sun Azimuth south west front of building
  - platform: threshold
    name: "sun azimuth nw"
    upper: 315
    lower: 300
    entity_id: sensor.sun_azimuth

# Sun Elevation
  - platform: threshold
    name: "sun elevation"
    upper: 30
    entity_id: sensor.sun_elevation

But i still recieve the same Invalid config for [sensor]: required key not provided @ data[‘platform’]. Got None. error. Anyone got some pointers or tips to solve this error ?

Hi Evert,

it should work like this and after applying it you should see the sensors in your states-tab. (Don’t forget to reload the config.)

- platform: template
  sensors:

    sun_azimuth_s:
      friendly_name: 'Sun Azimuth south east front of building'
      value_template: '{{ (state_attr("sun.sun", "azimuth") > 185) and (state_attr("sun.sun", "azimuth") < 200) }}'

    sun_azimuth_nw:
      friendly_name: 'Sun Azimuth south west front of building'
      value_template: '{{ (state_attr("sun.sun", "azimuth") > 200) and (state_attr("sun.sun", "azimuth") < 315) }}'

    sun_elevation:
      friendly_name: 'Sun Elevation'
      value_template: '{{ (state_attr("sun.sun", "elevation") > 30) }}'

Edit: I forgot to mention that you should place it into binary_sensors.yaml

Best regards,
Stoffl