Automation with variable triggers based on the weather, is possible?

Hi, I would like to create an automation to close my windows at sunset, but based on the weather.
If there is rain it must close 30 minutes before sunset, if cloudy at the exact sunset, if clear 15 minutes after sunset.
I have already done this with 3 automations, but I wanted to put it in only one, also because I would like to send a voice alert 2 minutes before closing.
I created this code but it doesn’t work

automation:
- id: '112233445566'
  alias: chiusura_tapparelle
  trigger:
    platform: sun
    event: sunset
    offset: 
      value_template: > 
        {% if states("weather.casa") == 'sunny' %} 
            "00.15.00" 
        {%  elif states("weather.casa") == 'partlycloudy' %}
            "-00.00.00" 
        {%  elif states("weather.casa") == 'rainey' %}
            "-00.30.00" 
        {%  elif states("weather.casa") == 'snowy' %}
            "-00.40.00" 
        {% endif %}
  action:
    - service: script.chiudi_tapparelle   

I’d use a template trigger and the sun elevation. Then you can close at (say) 1 degree when it’s sunny, 1.5 degrees when party cloudy, etc.

That’s also much more consistent in terms of light level. Half an hour before sunset is a very different level of light outside on the longest day than the shortest day.

OK, It’s work. I create an sensor template that calculates the elevation of the sun based on the weather.

  - platform: template
    sensors:
     chiusura_tapparelle_meteo:
       friendly_name: "chiusura tapparelle con meteo"
       unit_of_measurement: '°'
       value_template: > 
        {% if states("weather.casa") == 'sunny' %}  
            {{ state_attr('sun.sun', 'elevation') + 2 }}
        {%  elif states("weather.casa") == 'partlycloudy' %}
            {{ state_attr('sun.sun', 'elevation') + 0 }}
        {%  elif states("weather.casa") == 'rainy' %}
            {{ state_attr('sun.sun', 'elevation') - 5 }}
        {%  elif states("weather.casa") == 'cloudy' %}
            {{ state_attr('sun.sun', 'elevation') - 3 }}
        {%  elif states("weather.casa") == 'fog' %}
            {{ state_attr('sun.sun', 'elevation') - 5 }}
        {% else %} 
            {{ state_attr('sun.sun', 'elevation') }}
        {% endif %}        
        

and used this sensor to trigger the automation.
Now I’d like to put an entity that shows me the time left to close.
I know that the altitude is not linear, but I want to create a sensor that when there are 3 or 4 hours to sunset, I calculate the time left.
The multiplying factor for my zone is 0.166 every minute, which means the elevation decreases by about 1.66 every ten minutes.
So if the sunset today is at 20:00 and at 17:00 the elevation calculated by my sensor is 30.36 multiplying 30.36 x 0.166 I get 182.8 minutes which is 3:02 hours at closing.
What code can I use to create such a sensor?

2 Likes

I did, I created a sensor that gives me the sunset time until a few hours before sunset, then gives me the missing time, until the shutters close and then returns to give me the sunset.

sensor:
  - platform: template
    sensors:
      tempo_alla_chiusura:
        friendly_name: "Chiusura"
        value_template: >
          {% set tc = states('sensor.chiusura_tapparelle_meteo') | float  / 0.166   %}
          {% set tc = tc | int %}
          {% if now().hour > 16 and tc > 0 %}
          {{ tc // 60 }}:{{ '{:0>2d}'.format(tc%60) }}
          {% else %}
          {{ states('sensor.nextsunset') }}
          {% endif %}        
        

chiusura

1 Like

Thanks @byxil for sharing the code of your sensor.

Based on what you’ve done, I just rewrote it in a way that let me define all the angles for all the different weather types. Sharing in case :slight_smile:

{% set sun_elevation = state_attr('sun.sun', 'elevation') %}
{% set current_weather = states("weather.home") %}
{% set weather_to_angle_map = {
  "sunny": +5,
  "clear-night": +5,
  "partlycloudy": +2,
  "cloudy": -3,
  "fog": -5,
  "hail": -5,
  "lightning": -5,
  "lightning-rainy": -5,
  "pouring": -5,
  "rainy": -5,
  "snowy": -5,
  "snowy-rainy": -5,
  "windy": -5,
  "windy-variant": -5,
  "exceptional": -5
} %}

{% set angle_delta_based_on_weather = weather_to_angle_map[current_weather] or 0 %}

{{ sun_elevation + angle_delta_based_on_weather }}

Note: all the weather values were taken from here.

1 Like