Simple automation based on the current weather

Hy guys.
I was trying to make a simple automation based on the current weather state. I’m using met.no that is already integrated on HA.

I can’t understand how to make a trigger based on the changing of the weather forecast. A simple: weather change to rainy → do a thing.

I tried this, but don’t work:

trigger:
  - platform: state
    entity_id: weather.casa
    to: lightning, lightning-rainy, pouring, rainy

Hello metcrl,

it‘s a little bit to much for only one to:

You can use various triggers but have to arrange them separately.

I‘m using another weather integration, so I don‘t know what attributes weather.casa has. You can see that in dev tools —> state

Are you doing this via the UI or directly in YAML? What you want to do is connect multiple conditions with OR statements to achieve what you are looking for. The trigger should be what starts thing but the condition makes the decision. So your trigger could be when any of those states change and the conditions would be OR’s:

And like @pedolsky mentioned, use the dev tools to determine what state values are since you need to be literal when testing for them.

Oh thanks for the answers.
I’m doing in YAML because i think it’s cleaner. I completely forgot the “or” statement.
What if in the trigger i set like this:

trigger:
  - platform: state
    entity_id: weather.casa
    to: sunny
  - platform: state
    entity_id: weather.casa
    to: cloudy

Will it work if any of the two state become true?

See the docs.

trigger:
  - platform: state
    entity_id: weather.casa
    to:
      - "sunny"
      - "cloudy"

Triggers are OR, so if the weather moves from any state to either sunny or cloudy, this trigger will fire.

Ok, I see. But where in the docs i can see a text formatting like yours? :thinking:

image

Oooh thanks…
So, this make sense?

  trigger:
  - platform: state
    entity_id: weather.casa
    to:
      - "rainy"
      - "snowy"
  condition:
    condition: or
    conditions:
      - condition: state
        entity_id: weather.casa
        state: sunny
      - condition: state
        entity_id: weather.casa
        state: cloudy

No, that will always fail. By the time the condition is evaluated, the trigger state has already changed. So this will trigger on “rainy”, and then check if it is “sunny” or “cloudy”, which it isn’t, because it is “rainy”.

If you want to check if it’s going from “sunny” or “cloudy” to “rainy” or “snowy”, add

from:
  - "sunny"
  - "cloudy"

into the trigger.

If that doesn’t solve it, then rather than going round in circles, can you explain what it is you’re trying to achieve?

Yeah sorry. I’m trying to prepare an automation to make the roller shutters close automatically when the weather get worse.

Looking at the possible states of met.no integration, I saw that those that predict bad weather are different. For this reason I simply wanted to make an automation that would make sure that any state that could foresee rain, would trigger the automation to lower the shutters.

At the moment, just for try, i set for action a simple notification to my smartphone.

- id: '1620898331665'
  alias: shutters down
  description: ''
  trigger:
  - platform: state
    entity_id: weather.home
    from: sunny,clear-night,cloudy,fog,partlycloudy,windy,windy-variant
  condition:
  action:
  - service: notify.mobile_app_sm_g960f
    data:
      title: shutters down
  mode: single

Does this works? With this automation, if the weather change to “rainy”, does the trigger work?

I don’t know where you’re getting the comma-separated list syntax from for the from: statement — I’ve never seen that and don’t believe it will work.

If you format the list as shown above, like this:

from:
  - "sunny"
  - "clear-night"

and so on, it will trigger when the state changes from anything in that list to anything. So a change from “windy” to “sunny” will trigger it, which is not what you want.

I think you want:

from:
  - "sunny"
  - "clear-night"
  - (all other states for which you want the shutters open)
to:
  - "rainy"
  - "snowy"
  - (all other states for which you want the shutters closed)

That syntax is an error caused by opening in UI. I’ve already fixed it.

So, if I understand:

  • put all the conditions only on the FROM, will cause that trigger every change
  • put all the conditions only on the TO, will cause that never trigger

I have to put all the conditions simultaneously in FROM e in TO.
Like that:

- id: '1620898331665'
  alias: Tapparelle Abbassa per meteo
  description: ''
  trigger:
  - platform: state
    entity_id: weather.home
    from:
      - "sunny"
      - "clear-night"
      - "cloudy"
      - "fog"
      - "partlycloudy"
      - "windy"
      - "windy-variant"
    to:
      - "hail"
      - "lightning"
      - "lightning-rainy"
      - "pouring"
      - "rainy"
      - "snowy"
      - "snowy-rainy"
  condition:
  action:
  - service: notify.mobile_app_xxx
    data:
      title: Abbassa tapparelle
  mode: single
1 Like

Speaking for myself, that seems a very complex trigger when you could check it every minute with a single line that won’t be susceptible to state term changes in the future and then you can handle the “to” states in the condition. But if it work for you then that’s all that really matters :slight_smile:

I still don’t think you’re understanding fully, from what you’ve written — although the automation trigger you’ve written probably does what you want it to :slight_smile:.

The trigger will work if the state changes from any of the states listed under from: to any of the states listed under to:. Any other situation will not trigger the automation.

So

  • windy to hail = trigger
  • snowy to partlycloudy = no trigger
  • rainy to snowy = no trigger
  • sunny to cloudy = no trigger

I am a newbie and open to any advice! If you want to explain your solution better, I’ll wait for you :wink:

Thanks!

I was trying to explain the the mistakes I was making, but maybe I didn’t succeed very much :sweat_smile:
However, now it’s working.

If there are better and cleaner solutions I am open to any advice

1 Like

There are multiple ways to skin this cat, and what’s been proposed is 100% legitimate. So long as the state names stay consistent from both the integration and the weather data then you’re just fine doing what was proposed. For me, I try not to rely on static values too much just in case (and maybe I’m just paranoid), so I might trigger it every minute so I don’t have to have so many values in multiple locations or worry about making sure I took into accounting for all the “to” and “from” and just worry about what the current state is now:

trigger:
  - platform: time_pattern
    minutes: /1
condition:
  - condition: state
    entity_id: sensor.openweathermap_condition
    state: sunny
  - condition: or
    conditions:
      - condition: state
        entity_id: sensor.openweathermap_condition
        state: cloudy

I don’t know if you can do this, and if you can then awesome, but I’ve not implemented the multiple to/from entries like what was proposed, so this may or may not work:

trigger:
  - platform: time_pattern
    minutes: /1
condition:
  - condition: state
    entity_id: sensor.openweathermap_condition
    state: 
      - sunny
      - cloudy

You can also do this:

- id: '1620898331665'
  alias: Tapparelle Abbassa per meteo
  description: ''
  trigger:
  - platform: state
    entity_id: weather.home
    to:
      - "hail"
      - "lightning"
      - "lightning-rainy"
      - "pouring"
      - "rainy"
      - "snowy"
      - "snowy-rainy"
  condition:
  action:
  - service: notify.mobile_app_xxx
    data:
      title: Abbassa tapparelle
  mode: single

It triggers when the weather changes from anything to one of the listed foul weather conditions (hail, lightning, etc).

1 Like

@123’s code is a very elegant way to do it. It minimizes the repetitive state values and has a logical trigger.

I tried like this, but it sent to me a notification every minute:

- id: '1621004030737'
  alias: Tapparelle Abbassa per meteoTEST
  description: ''
  trigger:
  - platform: time_pattern
    minutes: /1
  condition:
  - condition: state
    entity_id: weather.casa
    state: 
    - hail
    - lightning
    - lightning-rainy
    - pouring
    - rainy
    - snowy
    - snowy-rainy
  action:
  - service: notify.mobile_app_sm_g960f
    data:
      message: Previsto maltempo
      title: Abbassa tapparelleTEST
  mode: single