Light automation with sun and weather

Hi,
I want to update my automation that triggers X minutes before sunset, adding a second condition that makes the automation trigger even earlier if the weather is cloudy or rainy.

Here’s the result of the automation I made with the frontend:

Working automation before the update

trigger:
  - event: sunset
    offset: -00:15:00
    platform: sun
  condition: []
  action:
  - data:
      brightness: 128
    entity_id: light.lampada_sala
    service: light.turn_on

(it turns on a light at 50% brightness 15 minutes before sunset)

Not working automation after the update (the weather integration I use is Met.no)

trigger:
  - event: sunset
    offset: -00:40:00
    platform: sun
  condition:
  - condition: and
    conditions:
    - condition: state
      entity_id: weather.casa
      state: cloudy
    - condition: or
      conditions:
      - condition: state
        entity_id: weather.casa
        state: rainy
  - condition: or     #<--- I'm unsure about this
    conditions:
    - after: sunset
      after_offset: -00:15:00
      before: sunset
      before_offset: -00:40:00
      condition: sun
  action:
  - data:
      brightness: 128
    entity_id: light.lampada_sala
    service: light.turn_on

(it should turn on the light 40 minutes before sunset if it’s rainy or cloudy, or turn it on 15 minutes before sunset (according to my reasoning).

If someone is able to help me (I bet most of you can) I will be very happy to listen and learn from my errors.

i think it would be easier to just do it in two automations. otherwise the templating gets complicated if you could even do it.

have one automation turn the light on at 40 minutes before sunset if it’s cloudy or raining. the have the other one always turn on the light at 15 minutes before. so if the first one doesn’t turn on the light second one will and if the first one already turned on the light then the second one triggers it won’t matter since the light is already on.

and if you don’t like the idea of the second automation triggering for no reason just make a condition of the second one that the light is off.

first one:

trigger:
  - event: sunset
    offset: -00:40:00
    platform: sun
condition:
  - condition: or
    conditions:
      - condition: state
        entity_id: weather.casa
        state: "cloudy"
      - condition: state
        entity_id: weather.casa
        state: "rainy"
action:
  - data:
      brightness: 128
    entity_id: light.lampada_sala
    service: light.turn_on

second one:

trigger:
  - event: sunset
    offset: -00:15:00
    platform: sun
  condition:
    - condition: state
      entity_id: light.lampada_sala
      state: "off"
  action:
  - data:
      brightness: 128
    entity_id: light.lampada_sala
    service: light.turn_on

Complete newbie here but I have a suggestion (and it may not even work as I’m thinking it in my head). Looking at your code tells me that you’re trying to get both sets of conditions to fire at the same time.

Here’s what I would do: set up the first condition like @finity suggested. Make sure that works. Comment it all out and set up the second condition. Test it out. Now, remove your comments and turn those two sections into OR conditions so then either one will fire the action.

Basically, it would be something like OR(condition1[A&B] , condition2[C&D]). Either condition has to return a True in order for the OR to complete the action.

Derek

PS. I’ve never done any coding like this but I’ve done some work in VBA for Excel and that’s how I would get some complicated IF statements to cooperate.

EDIT: Forgot to take into consideration the triggers. Not sure if my suggestion would work.

I don’t have problems with two automations triggering, I just have 2 lights for now, I might add that second condition in the future. I’m going to test this later, thanks.

EDIT: after fixing my Rpi 2 from the 4.8 HassOS update, I tried it and it worked just fine

This is what I came up with:

- id: '1584817825895'
  alias: Sera/Luce            #30 min before sunset
  description: ''
  trigger:
  - event: sunset
    offset: -00:30:00
    platform: sun
  condition: []
  action:
  - data:
      brightness: 128
    entity_id: light.lampada_sala
    service: light.turn_on
- id: '1591036060168'
  alias: Sera/Nuvolo/luce        #1h before sunset if it's cloudy/rainy
  description: ''
  trigger:
  - event: sunset
    offset: -01:00:00
    platform: sun
  condition:
  - condition: or
    conditions:
    - condition: state
      entity_id: weather.casa
      state: cloudy
    - condition: state
      entity_id: weather.casa
      state: rainy
  action:
  - data:
      brightness: 128
    entity_id: light.lampada_sala
    service: light.turn_on
1 Like