Light automation by weather condition

Hi, I’ve read a lot, many topics and solved lots of problems on my own, but I’m stuck and I have a feeling that with every new possible solution I find somewhere it gets more and more complicated.
I simply want to take the state of a weather sensor and change the color of my lights depending on it (if that works, I would maybe use forecast instead, implement an on/off switch and a 10 second flash every hour before reverting to the state before).
I read about templates and template sensors, years ago I worked with Python (but never got really fluent) and in general I know what a dictionary is and I guess this might be useful to shorten this automation, but I didn’t get it implemented so I chose this solution with choose… Which is not working (“Message malformed: required key not provided @ data[‘action’]”).
Here’s my code:

alias: Licht Wetterinfo
description: ""
trigger:
  - entity_id: sensor.openweathermap_condition
    platform: state
action:
  - choose:
    - alias: Option 1
      - conditions:
          - condition: state
          entity_id: sensor.openweathermap_condition
          state: clear-night
      sequence:
        - data:
            scene_id: before
            snapshot_entities: light.color_schlafzimmer
          service: scene.create
        - data:
            color_name: navy
            brightness_pct: 100
            entity_id: light.color_schlafzimmer
            flash: long
          entity_id: light.color_schlafzimmer
          service: light.turn_on
        - delay: "00:00:10"
        - data: {}
          entity_id: scene.before
          service: scene.turn_on
    default: []
  - choose:
    - alias: Option 2
      - conditions:
          - condition: state
          entity_id: sensor.openweathermap_condition
          state: sunny
      sequence:
        - data:
            scene_id: before
            snapshot_entities: light.color_schlafzimmer
          service: scene.create
        - data:
            color_name: yellow
            brightness_pct: 100
            entity_id: light.color_schlafzimmer
            flash: long
          entity_id: light.color_schlafzimmer
          service: light.turn_on
        - delay: "00:00:10"
        - data: {}
          entity_id: scene.before
          service: scene.turn_on
    default: []
  - choose:
    - alias: Option 3
      - conditions:
          - condition: state
          entity_id: sensor.openweathermap_condition
          state: cloudy
      sequence:
        - data:
            scene_id: before
            snapshot_entities: light.color_schlafzimmer
          service: scene.create
        - data:
            color_name: maroon
            brightness_pct: 100
            entity_id: light.color_schlafzimmer
            flash: long
          entity_id: light.color_schlafzimmer
          service: light.turn_on
        - delay: "00:00:10"
        - data: {}
          entity_id: scene.before
          service: scene.turn_on
    default: []
  - choose:
    - alias: Option 4
      - conditions:
          - condition: state
          entity_id: sensor.openweathermap_condition
          state: fog
      sequence:
        - data:
            scene_id: before
            snapshot_entities: light.color_schlafzimmer
          service: scene.create
        - data:
            color_name: yellowgreen
            brightness_pct: 100
            entity_id: light.color_schlafzimmer
            flash: long
          entity_id: light.color_schlafzimmer
          service: light.turn_on
        - delay: "00:00:10"
        - data: {}
          entity_id: scene.before
          service: scene.turn_on
    default: []
  - choose:
    - alias: Option 5
      - conditions:
          - condition: state
          entity_id: sensor.openweathermap_condition
          state: partlycloudy
      sequence:
        - data:
            scene_id: before
            snapshot_entities: light.color_schlafzimmer
          service: scene.create
        - data:
            color_name: indianred
            brightness_pct: 100
            entity_id: light.color_schlafzimmer
            flash: long
          entity_id: light.color_schlafzimmer
          service: light.turn_on
        - delay: "00:00:10"
        - data: {}
          entity_id: scene.before
          service: scene.turn_on
    default: []
  - choose:
    - alias: Option 6
      - conditions:
          - condition: state
          entity_id: sensor.openweathermap_condition
          state: rainy
      sequence:
        - data:
            scene_id: before
            snapshot_entities: light.color_schlafzimmer
          service: scene.create
        - data:
            color_name: lawngreen
            brightness_pct: 100
            entity_id: light.color_schlafzimmer
            flash: long
          entity_id: light.color_schlafzimmer
          service: light.turn_on
        - delay: "00:00:10"
        - data: {}
          entity_id: scene.before
          service: scene.turn_on
    default: []
  - choose:
    - alias: Option 7
      - conditions:
          - condition: state
          entity_id: sensor.openweathermap_condition
          state: windy
      sequence:
        - data:
            scene_id: before
            snapshot_entities: light.color_schlafzimmer
          service: scene.create
        - data:
            color_name: darkorange
            brightness_pct: 100
            entity_id: light.color_schlafzimmer
            flash: long
          entity_id: light.color_schlafzimmer
          service: light.turn_on
        - delay: "00:00:10"
        - data: {}
          entity_id: scene.before
          service: scene.turn_on
    default: []
  - choose:
    - alias: Option 8
      - conditions:
          - condition: state
          entity_id: sensor.openweathermap_condition
          state: snowy
      sequence:
        - data:
            scene_id: before
            snapshot_entities: light.color_schlafzimmer
          service: scene.create
        - data:
            color_name: green
            brightness_pct: 100
            entity_id: light.color_schlafzimmer
            flash: long
          entity_id: light.color_schlafzimmer
          service: light.turn_on
        - delay: "00:00:10"
        - data: {}
          entity_id: scene.before
          service: scene.turn_on
    default: []
  - choose:
    - alias: Option 9
      - conditions:
          - condition: state
          entity_id: sensor.openweathermap_condition
          state: snowy-rainy
      sequence:
        - data:
            scene_id: before
            snapshot_entities: light.color_schlafzimmer
          service: scene.create
        - data:
            color_name: limegreen
            brightness_pct: 100
            entity_id: light.color_schlafzimmer
            flash: long
          entity_id: light.color_schlafzimmer
          service: light.turn_on
        - delay: "00:00:10"
        - data: {}
          entity_id: scene.before
          service: scene.turn_on
    default: []
  - choose:
    - alias: Option 10
      - conditions:
          - condition: state
          entity_id: sensor.openweathermap_condition
          state: pouring
      sequence:
        - data:
            scene_id: before
            snapshot_entities: light.color_schlafzimmer
          service: scene.create
        - data:
            color_name: teal
            brightness_pct: 100
            entity_id: light.color_schlafzimmer
            flash: long
          entity_id: light.color_schlafzimmer
          service: light.turn_on
        - delay: "00:00:10"
        - data: {}
          entity_id: scene.before
          service: scene.turn_on
    default: []
  - choose:
    - alias: Option 11
      - conditions:
          - condition: state
          entity_id: sensor.openweathermap_condition
          state: lightning
      sequence:
        - data:
            scene_id: before
            snapshot_entities: light.color_schlafzimmer
          service: scene.create
        - data:
            color_name: red
            brightness_pct: 100
            entity_id: light.color_schlafzimmer
            flash: long
          entity_id: light.color_schlafzimmer
          service: light.turn_on
        - delay: "00:00:10"
        - data: {}
          entity_id: scene.before
          service: scene.turn_on
    default: []
  - choose:
    - alias: Option 12
      - conditions:
          - condition: state
          entity_id: sensor.openweathermap_condition
          state: hail
      sequence:
        - data:
            scene_id: before
            snapshot_entities: light.color_schlafzimmer
          service: scene.create
        - data:
            color_name: aquamarine
            brightness_pct: 100
            entity_id: light.color_schlafzimmer
            flash: long
          entity_id: light.color_schlafzimmer
          service: light.turn_on
        - delay: "00:00:10"
        - data: {}
          entity_id: scene.before
          service: scene.turn_on
    default: []
  - choose:
    - alias: Option 13
      - conditions:
          - condition: state
          entity_id: sensor.openweathermap_condition
          state: windy-variant
      sequence:
        - data:
            scene_id: before
            snapshot_entities: light.color_schlafzimmer
          service: scene.create
        - data:
            color_name: lightsalmon
            brightness_pct: 100
            entity_id: light.color_schlafzimmer
            flash: long
          entity_id: light.color_schlafzimmer
          service: light.turn_on
        - delay: "00:00:10"
        - data: {}
          entity_id: scene.before
          service: scene.turn_on
    default: []
  - choose:
    - alias: Option 14
      - conditions:
          - condition: state
          entity_id: sensor.openweathermap_condition
          state: lightning-rainy
      sequence:
        - data:
            scene_id: before
            snapshot_entities: light.color_schlafzimmer
          service: scene.create
        - data:
            color_name: crimson
            brightness_pct: 100
            entity_id: light.color_schlafzimmer
            flash: long
          entity_id: light.color_schlafzimmer
          service: light.turn_on
        - delay: "00:00:10"
        - data: {}
          entity_id: scene.before
          service: scene.turn_on
    default: []

Thank you for ideas, hints or help in advance.

Your sequence blocks are incorrectly indented (which is important in yaml).

The sequence and conditions should be in line like this:

action:
  - choose:
    - alias: Option 1
      - conditions:
          - condition: state
          entity_id: sensor.openweathermap_condition
          state: clear-night
        sequence:
          - data:
              scene_id: etc...

See the example here: https://www.home-assistant.io/docs/scripts#choose-a-group-of-actions

1 Like

You have structured your automation incorrectly for Choosing amongst exclusive options and you are repeating steps that don’t need to be repeated. You only need 1 Choose action.

alias: Licht Wetterinfo
description: ""
trigger:
  - entity_id: sensor.openweathermap_condition
    platform: state
condition: []
action:
  - alias: Save current light State to Scene
    service: scene.create
    data:
      scene_id: before
      snapshot_entities: light.color_schlafzimmer
  - alias: Flash light color based on weather condition
    choose:
      - conditions:
          - condition: state
            entity_id: sensor.openweathermap_condition
            state: clear-night
        sequence:
          - data:
              color_name: navy
              brightness_pct: 100
              entity_id: light.color_schlafzimmer
              flash: long
            service: light.turn_on
      - conditions:
          - condition: state
            entity_id: sensor.openweathermap_condition
            state: sunny
        sequence:
          - data:
              color_name: yellow
              brightness_pct: 100
              entity_id: light.color_schlafzimmer
              flash: long
            service: light.turn_on
      - conditions:
          - condition: state
            entity_id: sensor.openweathermap_condition
            state: cloudy
        sequence:
          - data:
              color_name: maroon
              brightness_pct: 100
              entity_id: light.color_schlafzimmer
              flash: long
            service: light.turn_on
      - conditions:
          - condition: state
            entity_id: sensor.openweathermap_condition
            state: fog
        sequence:
          - data:
              color_name: yellowgreen
              brightness_pct: 100
              entity_id: light.color_schlafzimmer
              flash: long
            service: light.turn_on
      - conditions:
          - condition: state
            entity_id: sensor.openweathermap_condition
            state: partlycloudy
        sequence:
          - data:
              color_name: indianred
              brightness_pct: 100
              entity_id: light.color_schlafzimmer
              flash: long
            service: light.turn_on
      - conditions:
          - condition: state
            entity_id: sensor.openweathermap_condition
            state: rainy
        sequence:
          - data:
              color_name: lawngreen
              brightness_pct: 100
              entity_id: light.color_schlafzimmer
              flash: long
            service: light.turn_on
      - conditions:
          - condition: state
            entity_id: sensor.openweathermap_condition
            state: windy
        sequence:
          - data:
              color_name: darkorange
              brightness_pct: 100
              entity_id: light.color_schlafzimmer
              flash: long
            service: light.turn_on
      - conditions:
          - condition: state
            entity_id: sensor.openweathermap_condition
            state: snowy
        sequence:
          - data:
              color_name: green
              brightness_pct: 100
              entity_id: light.color_schlafzimmer
              flash: long
            service: light.turn_on
      - conditions:
          - condition: state
            entity_id: sensor.openweathermap_condition
            state: snowy-rainy
        sequence:
          - data:
              color_name: limegreen
              brightness_pct: 100
              entity_id: light.color_schlafzimmer
              flash: long
            service: light.turn_on
      - conditions:
          - condition: state
            entity_id: sensor.openweathermap_condition
            state: pouring
        sequence:
          - data:
              color_name: teal
              brightness_pct: 100
              entity_id: light.color_schlafzimmer
              flash: long
            service: light.turn_on
      - conditions:
          - condition: state
            entity_id: sensor.openweathermap_condition
            state: lightning
        sequence:
          - data:
              color_name: red
              brightness_pct: 100
              entity_id: light.color_schlafzimmer
              flash: long
            service: light.turn_on
      - conditions:
          - condition: state
            entity_id: sensor.openweathermap_condition
            state: hail
        sequence:
          - data:
              color_name: aquamarine
              brightness_pct: 100
              entity_id: light.color_schlafzimmer
              flash: long
            service: light.turn_on
      - conditions:
          - condition: state
            entity_id: sensor.openweathermap_condition
            state: windy-variant
        sequence:
          - data:
              color_name: lightsalmon
              brightness_pct: 100
              entity_id: light.color_schlafzimmer
              flash: long
            service: light.turn_on
      - conditions:
          - condition: state
            entity_id: sensor.openweathermap_condition
            state: lightning-rainy
        sequence:
          - data:
              color_name: crimson
              brightness_pct: 100
              entity_id: light.color_schlafzimmer
              flash: long
            service: light.turn_on
  - delay: "00:00:10"
  - alias: Return light to previous state
    entity_id: scene.before
    service: scene.turn_on
mode: single

FWIW, this automation can be simplified considerably by using a few variables and a couple templates:

alias: Licht Wetterinfo
description: ""
trigger:
  - entity_id: sensor.openweathermap_condition
    platform: state
condition: []
action:
  - variables:
      color_options:
        clear-night: navy
        sunny: yellow
        cloudy: maroon
        fog: yellowgreen
        partlycloudy: indianred
        rainy: lawngreen
        windy: darkorange
        snowy: green
        snowy-rainy: limegreen
        pouring: teal
        lightning: red
        hail: aquamarine
        windy-variant: lightsalmon
        lightning-rainy: crimson
      color: '{{ color_options.get(trigger.to_state.state) }}'
  - alias: Save current light State to Scene
    service: scene.create
    data:
      scene_id: before
      snapshot_entities: light.color_schlafzimmer
  - alias: Flash light color based on weather condition
    service: light.turn_on
    data:
      color_name: '{{color}}'
      brightness_pct: 100
      flash: long
    target:
      entity_id: light.color_schlafzimmer
  - delay: "00:00:10"
  - alias: Return light to previous state
    entity_id: scene.before
    service: scene.turn_on
3 Likes

@Didgeridrew Brilliant, thank you! That’s what I was trying to do before, defining colors as variables and if the state of sensor.openweathermap_condition matches a color option, use the defined color as color_name for the light (i.e. state of condition in open weather map is sunny, take yellow as color_name for light since color_option sunny has the value yellow). I tried the above, getting the message “Message malformed: expected a dictionary for dictionary value @ data[‘action’][2][‘target’]”. Might an “if” statement work where I compare the state of sensor.openweathermap_condition with the variables in color_options and take the resulting match as a string as input for color_name? And how would the proper syntax for this be? Sorry for asking, I somehow have the feeling I am able to figure out the logical structure and how it could work, but I have some kind of problem translating it in a machine readable format…
@tom_l thank you a lot as well! Concerning the indentation, I know it’s crucial, but I guess I didn’t really understand where to put it right here, like if you know there’s orthography but don’t understand the rules or the logic behind it. Thank you for the clarification and correction

Sorry, for that… there was an errant hyphen in front of entity_id. I have corrected it above.

An if/then approach could work too, but it would be less efficient than the dictionary method I posted.

1 Like

Thanks for the correction, spotting and fixing such errors takes me days which you saved me.
For if/then I guess I would have to specify each possibility with repetition of the whole function (kind of hard coded), where your approach takes only one function with just the variables changing which need to be defined once in the dictionary?

I just tested the automation, manually set the state of the sensor to clear and got an error. Was wondering what might have gone wrong and then noticed that there’s clear-night specified, but not clear😅. Meanwhile, the light flashed yellow for sunny, everything is working greatly, thanks a lot (to @tom_l as well)! Since I have a working example of an automation with variables defined in a dictionary, I will try to transfer this to other usecases to learn and understand it myself better. Was a great help for me.