Automation - light Color change with weather forecast using switches. Need help!

I have an older LED light above my aquarium that takes IR codes. So I have coded all the IR codes into home assistant as switches. I can fire a switch and it changes the setting on the LED light. The cool thing about the light is that it has weather effects built into the light. So I would like to build an automation based on the entity “dark_sky_icon”. So when the dark_sky_icon changes throught the day, it will send the IR codes to the light. So I have a list of the Dark Sky Icons along with the switches that it needs to trigger. What is the best way to code this into home assisant?

dark_sky_icon		switch
clear-day			switch.aqled_sun
clear-night		switch.aqled_bluemoon
cloudy			switch.aqled_cloudy
fog				switch.aqled_cloudy2
hail				switch.aqled_partcloud
lightning			switch.aqled_tstorm
partly-cloudy-day	switch.aqled_cloudsun
partly-cloudy-night	switch.aqled_greymoon
rain				switch.aqled_partcloud2
sleet			switch.aqled_partcloud2
snow			switch.aqled_partcloud2
thunderstorm		switch.aqled_tstormsun
wind				switch.aqled_cloudy

The above isn’t the code, but rather the dark_sky_icon that home assistant has and the left column is what switch to trigger with that state.

Just make an Automation that triggers when the darksky icon Changes and using in that Automation the chooser component to trigger the corresponing switch according to the current darksky icon

Thanks for the advice, this is what I came up with. So one question if I just want this automation to run all day long, is it written right? or will this trigger just once during the time that I have stated? Because I would like for it to run all day.

alias: Aquarium Light Day
description: ''
trigger:
  - platform: time
    at: '13:00:00'
condition:
  - condition: time
    after: '13:00:00'
  - condition: sun
    after: sunset
    after_offset: '01:00:00'
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: clear-day
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_sun
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: cloudy
          - condition: or
            conditions:
              - condition: state
                entity_id: sensor.dark_sky_icon
                state: wind
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_cloudy
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: fog
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_cloudy2
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: hail
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_partcloud
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: lightning
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_tstorm
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: partly-cloudy-day
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_cloudsun
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: rain
          - condition: or
            conditions:
              - condition: state
                entity_id: sensor.dark_sky_icon
                state: sleet
              - condition: or
                conditions:
                  - condition: state
                    entity_id: sensor.dark_sky_icon
                    state: snow
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_partcloud2
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: thunderstorm
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_tstorm
    default: []
mode: single

It doesn’t appear to be working all day. What do I have wrong above?

I think you already know the answer; it will only trigger at 13:00 each day.

Originally you said:

If that’s what you want then use a State Trigger whose entity_id is the dark_sky_icon sensor.

1 Like

I have modified the code above to run a condition from 13:00 to 1 hour after sunset. Does it appear wrong above?

I don’t think you understand how an automation’s condition works. It doesn’t “run” anything. It serves as a filter after the trigger has occurred.

The automation you created:

  • triggers at 13:00
  • the first condition checks if the time is after 13:00:00
  • second condition checks if the time is after sunset (offset by an hour)

Both conditions have to be true to execute the action. I don’t see how that will be possible for the time to be 13:00 and after sunset. However, the main point is that the automation triggers just once a day at 13:00.

Gotcha, what I was trying to do is run the automation from 1:00 pm to 1 hour after sunset. So what would be the best recommendation for this timing? I will play around with it a bit to see if I can get it to trigger that condition. I am open to suggestions though.

Ok this is what I changed it to after reading more about sunset and conditions. It still hasn’t triggered because there has been no change in the entity dark_sky_icon. Do you feel I have the conditions correct now?

alias: Aquarium Light Day
description: ''
trigger:
  - platform: time
    at: '13:00:00'
condition:
  - condition: sun
    before: sunset
    before_offset: '-01:00:00'
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: clear-day
            for: '00:00:01'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_sun
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: cloudy
            for: '00:00:01'
          - condition: or
            conditions:
              - condition: state
                entity_id: sensor.dark_sky_icon
                state: wind
                for: '00:00:01'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_cloudy
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: fog
            for: '00:00:01'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_cloudy2
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: hail
            for: '00:00:01'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_partcloud
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: lightning
            for: '00:00:01'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_tstorm
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: partly-cloudy-day
            for: '00:00:01'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_cloudsun
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: rain
            for: '00:00:01'
          - condition: or
            conditions:
              - condition: state
                entity_id: sensor.dark_sky_icon
                state: sleet
                for: '00:00:01'
              - condition: or
                conditions:
                  - condition: state
                    entity_id: sensor.dark_sky_icon
                    state: snow
                    for: '00:00:01'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_partcloud2
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: thunderstorm
            for: '00:00:01'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_tstorm
    default: []
mode: single
1 Like

… and it never will trigger when the dark_sky_icon entity changes state because your automation isn’t using it as a trigger. You’ve designed your automation to trigger exclusively at a specific time. I’ve explained this twice but I’ve failed to make you understand that so I suggest you review this section of the documentation:

Ok I have it reversed in my brain then. So the trigger is when dark_sky_icon changes and the conditions are after 13:00 and 1 hour before sunset. Makes sense now. Will see if it changes today. Thanks Taras for explaining it! How does this look? Will test this one out today.

id: '1620957029117'
alias: Aquarium Light Day
description: ''
trigger:
  - platform: state
    entity_id: sensor.dark_sky_icon
condition:
  - condition: and
    conditions:
      - condition: time
        after: '13:00'
      - condition: sun
        before: sunset
        before_offset: '-01:00:00'
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: clear-day
            for: '00:00:01'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_sun
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: cloudy
            for: '00:00:01'
          - condition: or
            conditions:
              - condition: state
                entity_id: sensor.dark_sky_icon
                state: wind
                for: '00:00:01'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_cloudy
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: fog
            for: '00:00:01'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_cloudy2
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: hail
            for: '00:00:01'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_partcloud
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: lightning
            for: '00:00:01'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_tstorm
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: partly-cloudy-day
            for: '00:00:01'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_cloudsun
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: rain
            for: '00:00:01'
          - condition: or
            conditions:
              - condition: state
                entity_id: sensor.dark_sky_icon
                state: sleet
                for: '00:00:01'
              - condition: or
                conditions:
                  - condition: state
                    entity_id: sensor.dark_sky_icon
                    state: snow
                    for: '00:00:01'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_partcloud2
      - conditions:
          - condition: state
            entity_id: sensor.dark_sky_icon
            state: thunderstorm
            for: '00:00:01'
        sequence:
          - service: switch.turn_on
            target:
              entity_id: switch.aqled_tstorm
    default: []
mode: single

That’s much better; I believe you now have a better understanding of how an automation’s trigger works.

The only thing that may not work is the additional requirement you added to each one of the conditions in choose, namely this:

            for: '00:00:01'

Think about what’s happening here, the automation triggers the instant the dark_sky_icon sensor changes state. However the conditions in choose require that the new state must be at least 1 second old.

I suggest you remove for: from each one of the conditions and add it to the State Trigger. Now it will trigger only after the sensor changes state and maintains that new state for at least a second.

Although choose is well suited for this purpose, my own choice would be to template the entity_id option using a dictionary that serves as a cross-reference for the sensor’s state and the corresponding switch.

id: '1620957029117'
alias: Aquarium Light Day
description: ''
trigger:
  - platform: state
    entity_id: sensor.dark_sky_icon
    for: '00:00:01'
condition:
  - condition: time
    after: '13:00'
  - condition: sun
    before: sunset
    before_offset: '-01:00:00'
action:
  - service: switch.turn_on
    target:
      entity_id: >
        {% set switches = 
          { 'clear-day': 'sun', 'cloudy': 'cloudy', 
            'wind': 'cloudy', 'fog': 'cloudy2',
            'hail': 'partcloud', 'lightning': 'tstorm',
            'partly-cloudy-day': 'cloudsun', 'rain': 'partcloud2',
            'sleet': 'partcloud2', 'snow': 'partcloud2',
            'thunderstorm': 'tstorm' } %}
          switch.aqled_{{ switches[trigger.to_state.state] }}
mode: single

In addition, when you specify multiple conditions in condition they are logically ANDed by default so it’s not necessary to explicitly indicate they must be ANDed.

This version also employs the trigger variable that is accessible to templates in condition and action when using a State Trigger.

1 Like

I like that much better Taras and had that in mind that there must be a better way to have that matrix in there. You are right choose works, but it is very long winded. Thankfully I worded the switches as such to be able to use this method. Luck on that part, but I like it and will keep it in mind. I have some night switches to do now, so I will code those accordingly. Appreciate the help. I will let you know if it fires off during the day. Here is my updated code based on your recommendations.

alias: Aquarium Light Day
description: ''
trigger:
  - platform: state
    entity_id: sensor.dark_sky_icon
    for: '00:00:01'
condition:
  - condition: time
    after: '13:00'
  - condition: sun
    before: sunset
    before_offset: '01:00:00'
action:
  - service: switch.turn_on
    target:
      entity_id: |
        {% set switches = 
          { 'clear-day': 'sun', 
            'cloudy': 'cloudy', 
            'wind': 'cloudy',
            'fog': 'cloudy2',
            'hail': 'partcloud',
            'lightning': 'tstorm',
            'partly-cloudy-day': 'cloudsun',
            'rain': 'partcloud2',
            'sleet': 'partcloud2',
            'snow': 'partcloud2',
            'thunderstorm': 'tstorm' } %}
          switch.aqled_{{ switches[trigger.to_state.state] }}
mode: single

1 Like

It’s not too difficult to test now if you don’t want to wait until the weather changes.

  1. Go to Developer Tools > States, click on sensor.dark_sky_icon.
  2. Scroll to the top of the page. The sensor’s current state value will be displayed in the State field.
  3. Change it to lightning then click the Set State button.

This should cause the automation to trigger and turn on the corresponding switch.


NOTE

When you set an entity’s state value this way, the value persists until the sensor gets updated normally or Home Assistant is restarted (whichever comes first).