Light color based on weather

I’m trying to learn HA, and feel I’m in over my head.
I have a Nortek usb zwave and zigbee stick. I have a Sengled color bulb. I can control the bulb from HA just fine.
I would like to make an automation to make the bulb blue when raining and yellow when sunny (not raining).
I found this post, but it’s still greek to me.

I have the Meteorologisk weather integeration installed and can see weather from it. Thought I could use it for the forecast.


1 Like

Yup, welcome to learning YAML, and jinja2 templates. It is greek for most people lol.

So, you need to create an automation that checks the weather. And you need to know when you want to check. Hourly? Daily? Every time there is a weather update? This will be your ‘trigger’.

First, open the developer tools, go to states, and find the weather sensor you have. You’ll have to find what it looks like and where the information you want is stored. Some of them might be separate sensors, like “sensor.meterologisk_forecast”. Some of them will store it in attributes in a single sensor. This would show up on the right side in the attributes.

If it is it’s own sensor, you can get this value with:

If it’s an attribute, you use:
"state_attr('sensor.weather_sensor', 'attribute_i_care_about')"

Templates in yaml are denoted usually with “{{” and “}}”. Basically you say “try to figure out the value between those brackets based on this info”. The other way is with the “: >” command which says "all following lines are one giant template. Don’t worry about that part for now.

So here’s an example to maybe get you started…I will assume the things you care about are an attribute called ‘forecast’ in a sensor named “”.

I’m going to use variables. It will be greek still, but maybe the comments will help…

- alias: Weather Light
    # Create a 'lookup map' to make things easier. Basically, I'm creating a variable map called
    # weather. Down below I can use this. It makes it easier to change values without having to modify
    # it in multiple places. 
      # Make sure the names here match the possible states. 
      # I'm assuming the weather sensor will have values like "rainy" and "sunny". 
      # Whatever they show, change their names here. For example, if it shows 
      # "Rainy" instead of "rainy", make it capital here as well.
        color: blue
        color: yellow
    # Get the weather value I care about and store it in a variable called 'current_weather'.
    # It's not required, but easier as I only have to define this value one time.
    current_weather: "{{ state_attr('', 'forecast') }}"
    # This will trigger on ALL state/attribute changes for this sensor
    platform: state
    condition: template
    # Only care if the value returned is part of our map. So in this example, this will only 
    # be true if current_weather is either 'rainy' or 'sunny'. We can add more to the map
    # above easily without having to modify anything here!
    value_template: "{{ current_weather in weather }}" 
    - service: light.turn_on
      entity_id: light.my_light
        # Set the color by name.
        # Remember above, we created a map called weather. So if it's rainy, we are essentially doing
        # weather['rainy'].color Which would return weather.rainy.color, or in this case, blue. 
        color_name: "{{ weather[current_weather].color }}"

Hi, I’ve tried something similar using your example code, but I get an error: “UndefinedError: ‘weather’ is undefined”


I tried the solution by @jocnnor but it throws this error:

Executed: October 17, 2023 at 14:04:53
Error: Error rendering data template: UndefinedError: dict object has no element [{'condition': 'partlycloudy', 'datetime': '2023-10-17T10:00:00+00:00', 'wind_bearing': 83.3, 'temperature': 13.3, 'templow': 8.0, 'wind_speed': 18.4, 'precipitation': 0.0, 'humidity': 59}, {'condition': 'cloudy', 'datetime': '2023-10-18T10:00:00+00:00', 'wind_bearing': 90.8, 'temperature': 16.7, 'templow': 7.0, 'wind_speed': 22.0, 'precipitation': 9.4, 'humidity': 58}, {'condition': 'cloudy', 'datetime': '2023-10-19T10:00:00+00:00', 'wind_bearing': 194.6, 'temperature': 20.2, 'templow': 13.3, 'wind_speed': 23.0, 'precipitation': 8.9, 'humidity': 78}, {'condition': 'rainy', 'datetime': '2023-10-20T10:00:00+00:00', 'wind_bearing': 65.5, 'temperature': 14.6, 'templow': 6.2, 'wind_speed': 28.4, 'precipitation': 16.0, 'humidity': 79}, {'condition': 'rainy', 'datetime': '2023-10-21T10:00:00+00:00', 'wind_bearing': 89.0, 'temperature': 7.1, 'templow': 4.5, 'wind_speed': 25.6, 'precipitation': 18.9, 'humidity': 93}, {'condition': 'partlycloudy', 'datetime': '2023-10-22T10:00:00+00:00', 'wind_bearing': 245.5, 'temperature': 11.2, 'templow': 5.4, 'wind_speed': 22.7, 'precipitation': 0.2, 'humidity': 72}]

Any suggestions on what may be causing this?