How can I optimize this automation about weather state

Hi everyone!

I am starting with HA, and I made this automation reading here and there… It works, but not the correct way, and I think it has room for improvement.

This is the automation:

alias: Subir estores cuando hace sol
description: ''
trigger:
  - platform: state
    entity_id: weather.home
condition:
  - condition: or
    conditions:
      - condition: sun
        before: sunset
      - condition: sun
        after: sunrise
  - condition: numeric_state
    entity_id: sun.sun
    attribute: elevation
    above: '12'
action:
  - choose:
      - conditions:
          - condition: template
            value_template: >-
              {{ is_state('weather.home', 'sunny') or is_state('weather.home',
              'partlycloudy') }}
        sequence:
          - service: cover.open_cover
            target:
              entity_id: cover.estores
          - service: notify.telegramamibumping
            data:
              message: Subiendo estores
              title: Hace sol en Burgos
      - conditions:
          - condition: template
            value_template: >-
              {{ not is_state('weather.home', 'sunny') and not
              is_state('weather.home', 'partlycloudy') }}
        sequence:
          - service: cover.close_cover
            target:
              entity_id:
                - cover.estor_2
                - cover.estor_3
                - cover.estor_4
          - service: notify.telegramamibumping
            data:
              message: Cerrando estores
              title: No hace sol en Burgos
    default: []
mode: restart

What it does is:

  1. It checks weather state.

  2. If the sun is between sunrise and sunset, and the sun elevation is greater than 13º

Then:

  1. If it is sunny or partly cloudy
    1.1) The covers go up
    1.2) Send Notification

  2. If it is not sunny or partly cloudy
    2.1) The covers go down
    2.2) Send Notification

Restart mode, for evaluate the automation every weather.state change

The things I am experimenting.

Every time the weather state is evaluated I received obvious the notification, even if the state of the covers is already open, but I don’t know how to do it…

And the other thing is that maybe the weather gets updated at for example 10:00, and at that time the condition for the elevation is not true, no actions occurred, that’s correct, but if then if the elevation is true, the automation doesn’t run until the next weather state update even if one of the weather conditions (sunny or partly cloudy) went true before, I hope I explain myself clear…

Thank you!

You could at least, you could combine the sunset/sunrise conditions to

  condition: state
  entity_id: sun.sun
  state: "above_horizon"

And: The action conditions are opposite to each other. You could keep the first one, but move the second one (without conditions) to the default section.

1 Like

Thank you!

I will apply those suggestions right now.

Still lookink something for these:

Every time the weather state is evaluated I received obvious the notification, even if the state of the covers is already open, but I don’t know how to do it…

And the other thing is that maybe the weather gets updated at for example 10:00, and at that time the condition for the elevation is not true, no actions occurred, that’s correct, but if then if the elevation is true, the automation doesn’t run until the next weather state update even if one of the weather conditions (sunny or partly cloudy) went true before, I hope I explain myself clear…


              {{ is_state('weather.home', 'sunny') or is_state('weather.home',
              'partlycloudy') and is_state('input_boolean.interruptor_estores',
              'off') }}

              {{ not is_state('weather.home', 'sunny') and not
              is_state('weather.home', 'partlycloudy') and
              is_state('input_boolean.interruptor_estores', 'on') }}


Yesterday I changed the condition template adding an input to now the state of the covers, and with that not repeating the notifications every time weather change it’s state.

I thought it was working but today, the notifications was send it again…

And I think is the and/or condition. I made it now with the visual editor and I think it is working, but I would like to know how to do it with a sentence.

{{ is_state('weather.home', 'sunny') or is_state('weather.home',
              'partlycloudy') and is_state('input_boolean.interruptor_estores',
              'off') }}

With that condition, what I want to be is that either is sunny or partly cloudy but input is on, to be false. but I test that and it gives me true, when the weather is sunny, if I change the order of the weather state, first partly cloudy, it gives me false

This is the way and/or is evaluated in Python. The first “or" condition is true (it’s sunny), the statement evaluates to true and the rest of the conditions are ignored. Try adding parentheses to group the weather conditions together.

{{ (is_state('weather.home', 'sunny') or is_state('weather.home',
              'partlycloudy')) and is_state('input_boolean.interruptor_estores',
              'off') }}
1 Like

Oh thank you, that worked @brg468 !

I think to reduce code a little bit you can do this:

{{ states('weather.home') not in ['sunny','partlycloudy','unavailable','unknown'] and is_state('input_boolean.interruptor_estores','off') }}

That way you can compare to multiple items at once, without having to keep writing is_state statements.

1 Like