[solved] Close curtains based on temp & azimuth?

I’m trying to create an automation to close the curtains based on temperature and position (azimuth) of the sun.
I have both values but it’s not working as it should.
I’m struggling with trigger <-> condition:

trigger:
  - entity_id: binary_sensor.sun_azimuth_front
    from: "off"
    platform: state
    to: "on"
    enabled: false
  - platform: numeric_state
    entity_id: weather.home
    attribute: temperature
    above: 25
condition:
  - condition: state
    entity_id: binary_sensor.sun_azimuth_front
    state: "on"
  - above: "25"
    condition: numeric_state
    entity_id: weather.home
    value_template: "{{ states.weather.home.attributes.temperature }}"
    enabled: false
  - condition: or
    conditions:
      - condition: state
        entity_id: weather.home
        state: sunny
      - condition: state
        entity_id: weather.home
        state: partlycloudy

As you can see, I have played with both but just cannot get it right.
Any help is highly appreciated!

What “is not right” for you?

Here is a baseline to use if you want to steal it. My conditions are a little different, and mine is for my awning instead of shades so I do not extend it if the weather forecast has wind gusts being greater than 20 AND (the temp is forecast to be above 70 degrees F OR the HVAC cooling system ran the day before.

alias: Awning-Open if Temp above 70 or cooling the day before
description: ""
trigger:
  - platform: numeric_state
    entity_id: sun.sun
    attribute: azimuth
    above: 172
    below: 270
condition:
  - condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.home_wind_gust
        below: 20
      - condition: or
        conditions:
          - condition: numeric_state
            entity_id: sensor.temperature_high_forecast
            above: 70
          - condition: numeric_state
            entity_id: sensor.hvac_cooling_run_time_last_24hr
            above: 0
    enabled: true
action:
  - service: cover.stop_cover
    data: {}
    target:
      entity_id: cover.deck_awning
mode: single

1 Like

There is a very interesting project regarding covers/blinds automatically setting after the sun, that you could easily use for curtains as well.

See here:

1 Like

Hi Chris, the problem is that it doesn’t work as expected.
The obvious way - to me - is to use the azimuth as trigger and the temperature as condition but what if the temperature is below the threshold when the azimuth turns on.

I have managed to create 2 azimuth helpers for both positions (front and side of house)
Opening again is not a problem since the trigger there is when the azimuth helper switches off.

So it’s not clear to me how to get this right.

Correct me if I’m wrong please.
Like this:

  • the automation is triggered if the sensor switches from off to on, so 1 time only when this happens
  • the action will run if the trigger is fired AND the temperature is above 25°

What if the temperature is lower at the time the automation is triggered and increases while the sensor is on → the action will never be executed.

trigger:
  - entity_id: binary_sensor.sun_azimuth_front
    from: "off"
    platform: state
    to: "on"
condition:
  - above: "25"
    condition: numeric_state
    entity_id: weather.home
    value_template: "{{ states.weather.home.attributes.temperature }}"

In this example, if the temperature is higher then 25° before the sensor is on, the action will not run either, right?

trigger:
  - platform: numeric_state
    entity_id: weather.home
    attribute: temperature
    above: 25
condition:
  - condition: state
    entity_id: binary_sensor.sun_azimuth_front
    state: "on"

I would create an automation with multiple triggers one for temperature, and one for azimuth, since the triggers are “or’ed”. I would then add an “and condition” that both of these triggers are where you want them for the automation to proceed. This way if the automation is triggered by either one of these, it will not proceed if both conditions are not met.

1 Like

Both of your triggers are kind of “one-time” triggers as they only trigger in the one moment where the treshhold is passed. This will not work for what you want to achieve.

What you’d need despite is a trigger, that triggers more often, eg. the sun.azimuth. That gives you the possibility to check for your conditions. You could as well choose any other trigger, as long as it gives you the chance to proof your conditions more often. Even a time or time_pattern trigger would work, or you could use your template trigger, if it has the azimuth as an attribute (and thus updates every few minutes).

Maybe using the new trigger based template sensors could be something worth thinking about.

2 Likes

Thank you for explaining it like this, that makes it more clear!

So creating a sensor for the azimuth made it even more ‘unusable’ for my goal…
Similar to using a range for the azimuth, defining a upper value for the temperature would also make that trigger work?
I’m reading into trigger based template sensors to understand what this can bring.

What’s your idea about @pcwii his suggestion?

Guys, does this work as a range in a trigger?

platform: state
entity_id:
  - sun.sun
attribute: azimuth
from: "162"
to: "286"

No, check numeric state trigger here: https://www.home-assistant.io/docs/automation/trigger/#home-assistant-trigger

I wouldn’t use a range, as this is as well a “one-time-trigger”. It doubles the trigger rate, but in the end, it still needs a treshhold to pass.

What I think you want is a “simple” trigger, eg. the sun.azimuth. This sensor triggers on every change, meaning the state as well as any changes in attributes of that sensor. The azimuth should change every few minutes, and thus triggers every few minutes (iirc every three or four minutes should be a change in azimuth).

After that you can easily check for your conditions. If they’re met, the automation carries on and closes your curtains. The only thing I don’t know, do you want your conditions to be an “and” or an “or” condition. Meaning, what is the exact state you want the curtains to close:

  • it is over 25°C AND the azimuth is over XY
  • the azimuth is over XY OR the temp is over 25°C

If you want the first one, it could look something like this:

automation:
  - id: xxyyzz
    trigger:
      - platform: state
        entity_id: sun.azimuth
    condition:
      - condition: numeric_state
        entity_id: sun.azimuth
        above: 162
      - condition: numeric_state
        entity_id: weather.home
        above: 25
     action:
        - service: cover.close_cover # not sure about the correct service here

Note: the trigger is only the sun.azimuth without any further “conditions” in the trigger like “above”. We want it to fire a trigger, if something changes, no matter what exactly that is, either the state of the trigger, or an attribute. If you set something like “above” here, it will only trigger if that specific value changes, that’s not what we want.

After that we check for the conditions: is it warmer than 25°C AND is the azimuth above our treshhold.

If you want the second one from above (the OR condition) you just need to update the condition block like this:

    condition:
      - or:
        - condition: numeric_state
          entity_id: sun.azimuth
          above: 162
        - condition: numeric_state
          entity_id: weather.home
          above: 25

This gets you to the action part, if only one of these condtions is true.

Note the difference between trigger and conditions: if you set “above: 162” in the trigger, it only fires once, in that moment where the treshhold is passed. In the condition, it is a little different. The condition is checked every time the trigger fires, so it is evaluated “on point”. Is the value above the treshhold in the moment of execution (=> yes or no). So in the condition it is not checking for the passing of the threshhold, just for the value at that exact moment.

And to complicate things a little more (sorry), I’m using the specific sensor sun.azimuth. If you’d like to work with the sun.sun sensor, you’d need to check for the attribute, something like

automation:
  - id: xxyyzz
    trigger:
      - platform: state
        entity_id: sun.sun
        attribute: azimuth

Does this clear up tings at least a bit for you? If not, please let me know, than I’ll try to explain further. :slight_smile:

1 Like

Yesss, understood Sir! :star_struck:

You explained it very well and it needed some time/effort to comprehend (this is not my native language and even though I’m using HA for some time now and try to help others from time to time to learn and give back, it’s not that obvious to get it)

There was 1 small error though:

entity_id: sun.azimuth

has to be:

entity_id: sensor.sun_azimuth

Answering your questions whether the condition has to be AND or OR, the criteria are:

  • sun in a certain position
  • temperature above 25
  • sunny or partly sunny

This is the final code:

trigger:
  - platform: state
    entity_id:
      - sensor.sun_azimuth
condition:
  - above: "25"
    condition: numeric_state
    entity_id: weather.home
    value_template: ""
    enabled: true
    attribute: temperature
  - condition: or
    conditions:
      - condition: state
        entity_id: weather.home
        state: sunny
      - condition: state
        entity_id: weather.home
        state: partlycloudy
  - condition: numeric_state
    entity_id: sensor.sun_azimuth
    above: 103
    below: 205

Thank you VERY VERY much (isn’t that a song, yes it is) for helping Patrick! :bowing_man:

1 Like