Toggle light based on weather condition with openweathermap

Hello there.
I integrated openweathermap to my home assistant, so far no problems. Afterwards I set up the automation:

  • between 2h after sunrise and 2h before sunset (condition, because light normaly turns off 2h after sunrise)
  • if sunny or windy, turn light on (or action 1, same scene is used with other automations)
  • else turn the light on (or action 2, same scene is used with other automations)

Here is the code I got, what am I doing wrong? Looking outside the window, its cloudy, thats what openweathermap shows me as well (Sensors: condition=cloudy)

description: ""
trigger:
  - platform: state
    entity_id:
      - sensor.openweathermap_condition
condition:
  - condition: and
    conditions:
      - condition: time
        after: input_datetime.sunrise_plus_2h
        before: input_datetime.sunrise_minus_2h
action:
  - if:
      - condition: or
        conditions:
          - condition: state
            entity_id: sensor.openweathermap_condition
            state: windy-variant
          - condition: state
            entity_id: sensor.openweathermap_condition
            state: sunny
    then:
      - service: scene.turn_on
        target:
          entity_id: scene.turn_off_all_lights
        metadata: {}
    else:
      - service: scene.turn_on
        target:
          entity_id: scene.all_lights
        metadata: {}

Or is it because it has to CHANGE the state, rather then just beeing e.g. cloudy?

https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

Update: I tried to move from the weather state to cloud_coverage, since its probably an easier indicator. Code looks like this now:

alias: 07 - Toggle Lights Weather
description: ""
trigger:
  - platform: state
    entity_id: weather.openweathermap
    attribute: cloud_coverage
condition:
  - condition: and
    conditions:
      - condition: time
        after: input_datetime.sunrise_plus_2h
        before: input_datetime.sunset_minus_2h
action:
  - if:
      - condition: numeric_state
        entity_id: weather.openweathermap
        attribute: cloud_coverage
        above: 75
    then:
      - service: scene.turn_on
        target:
          entity_id: scene.lights_on_sitting_bedroom_corridor
        metadata: {}
    else:
      - service: scene.turn_on
        target:
          entity_id: scene.turn_off_all_lights
        metadata: {}

In my understanding, betweend 2h after sunrise and 2h before sunset, as soon as cloud coverage goes above 75%, the light turns on. otherwise it switches all lights off…
Yet, it doesnt seem to work and after playing with the ifelse, conditionsm I still cant figure why… What did I overlook?

Nope. The way they have it is (sort of) correct, see: https://www.home-assistant.io/docs/scripts/conditions/#time-condition

The AND condition is not needed.

1 Like

ugh - sorry - I’ll remove my post to avoid confusion.,

After a few more tries, I got it. No more and function as suggested and works:

alias: 07 - Toggle Lights Weather
description: ""
trigger:
  - platform: state
    entity_id: weather.openweathermap
    attribute: cloud_coverage
condition:
  - condition: sun
    after: sunrise
    before: sunset
    before_offset: "02:00:00"
    after_offset: "02:00:00"
action:
  - if:
      - condition: numeric_state
        entity_id: weather.openweathermap
        attribute: cloud_coverage
        above: 69
    then:
      - service: scene.turn_on
        target:
          entity_id: scene.lights_on_sitting_bedroom_corridor
        metadata: {}
    else:
      - service: scene.turn_on
        target:
          entity_id: scene.turn_off_all_lights
        metadata: {}