Setting the light color depending on openweather forecast condition

Hello,
I would like to make an automation to change the color of a light, depending on openweather forecast condition.
At this moment I am trying to make a script because it’s easyer to test it.
So far my script looks like this:

meteo_light_test:
  alias: Meteo Light Test
  sequence:
  - service: light.turn_on
    data_template: >
      {% if is_state('sensor.openweathermap_forecast_condition', 'clear') %}
        {% set d = {'rgb_color':[250,250,210]} %}
      {% elif is_state('sensor.openweathermap_forecast_condition', 'clouds') %}
        {% set d = {'rgb_color':[135,206,235] } %}
      {% elif is_state('sensor.openweathermap_forecast_condition', 'snowy-rainy') %}
        {% set d = {'rgb_color':[215,215,245] } %}
      {% elif is_state('sensor.openweathermap_forecast_condition', 'rain') %}
        {% set d = {'rgb_color':[30,144,255] } %}
      {% elif is_state('sensor.openweathermap_forecast_condition', 'snow') %}
      {% set d = {'rgb_color':[245,245,245] } %} {% else %}
        {% set d = {'rgb_color':[0,245,0] } %}
      {% endif %}  
      {{ d }}
    entity_id: light.living_room_spotlight
  mode: single

During constant modifications, I managed to turn the light on with it, but the color was bright white (now it’s not even working at all anymore).
I am not sure what I am doing wrong.
Any help or suggestions welcomed.

Thanks

key: value

You can only template the value. Not the key. You have both in your template.

Also I think you may have trouble returning the RGB value as a list. It will be returned as a string. Or that may have been fixed. I’m not sure.

Also, also you no longer need to use data_template:. Just data: is fine.

So try this and see if it works (it may not):

  - service: light.turn_on
    data:
      entity_id: light.living_room_spotlight
      rgb_color: >
        {% if is_state('sensor.openweathermap_forecast_condition', 'clear') %}
          [250,250,210]
        {% elif is_state('sensor.openweathermap_forecast_condition', 'clouds') %}
          [135,206,235]
        {% elif is_state('sensor.openweathermap_forecast_condition', 'snowy-rainy') %}
          [215,215,245]
        {% elif is_state('sensor.openweathermap_forecast_condition', 'rain') %}
          [30,144,255]
        {% elif is_state('sensor.openweathermap_forecast_condition', 'snow') %}
          [245,245,245]
        {% else %}
          [0,245,0]
        {% endif %}  

Edit: it seems the list/string issue has been fixed, so the above should work.

1 Like

Some time ago I did something very similar that adjusts my light color in the morning according to the traffic situation.
However, I used a Python script because it was easier for me to understand than the jinja2 templates in Homeassistant Scripts. I only have a script and an automation that calls it up in the morning.

Automation:

- id: '1595445444526'
  alias: Beleuchtung - Wohnzimmer Verkehrslicht an
  description: ''
  trigger:
  - minutes: /1
    platform: time_pattern
  condition:
  - after: 06:00:00
    before: 07:30:00
    condition: time
    weekday:
    - mon
    - tue
    - wed
    - thu
    - fri
  - condition: state
    entity_id: group.anwesenheit
    state: home
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: 'on'
  action:
  - data:
      entity_id: light.stehlampe_wohnzimmer_links
    service: python_script.traffic
  mode: restart

Python Script:

# ID address for light
entity_id  = data.get('entity_id')

# Current state of traffic
# ~sensor.von_zu_hause_zur_arbeit is my google_travel_time sensor
traffic_status = (hass.states.get('sensor.von_zu_hause_zur_arbeit')).state

# Adjust times for different severity   
# Good traffic  
if int(traffic_status) < 35:
    hass.services.call('light', 'turn_on', { "entity_id" : entity_id, 'color_name': 'darkgreen', 'brightness': '255'  })
# OK traffic
elif int(traffic_status) <= 40:
    hass.services.call('light', 'turn_on', { "entity_id" : entity_id, 'color_name': 'darkorange', 'brightness': '255'  })
# Bad traffic
elif int(traffic_status) > 40:
   hass.services.call('light', 'turn_on', { "entity_id" : entity_id, 'color_name': 'darkred', 'brightness': '255'  })
# Unknown condtions
else:
   hass.services.call('light', 'turn_on', { "entity_id" : entity_id, 'color_name': 'purple', 'brightness': '255' })

I think that can be easily adapted from the traffic sensor to the weather sensor :slight_smile:

The corrected code works like a charm :star_struck:
It’s good to know that data_template not needed. I got a lot of errors because of that.
Now I need to write the automation and will post the resulting script (if it’s working :man_facepalming:)

Thank you!

Same for me.
jinja2 templates, specially if I would like to figure out something complicated would be not to understandable for me either.
Thanks for sharing you python script

Meanwhile I made an automation from the script, but I have no clue why never triggers :pensive:
Maybe you could further help me with my problem.

Here is the script:

- id: wake_up_meteo_light
  alias: Wake Up Meteo Light
  description: ''
  trigger:
  - platform: time
    at: '16:17:00'
  - platform: time
    at: '09:00:00'
  condition:
  - condition: time
    weekday:
    - mon
    - tue
    - wed
    - thu
    - fri
    after: '15:55:00'
    before: '17:00:00'
  - condition: or
    conditions:
    - condition: time
      weekday:
      - sat
      - sun
      after: '08:55:00'
      before: '09:05:00'
  action:
  - service: light.turn_on
    data:
        rgb_color: |
        {% if is_state('sensor.openweathermap_forecast_condition', 'clear') %}
          [250,250,210]
        {% elif is_state('sensor.openweathermap_forecast_condition', 'clouds') %}
          [135,206,235]
        {% elif is_state('sensor.openweathermap_forecast_condition', 'snowy-rainy') %}
          [215,215,245]
        {% elif is_state('sensor.openweathermap_forecast_condition', 'rain') %}
          [30,144,255]
        {% elif is_state('sensor.openweathermap_forecast_condition', 'snow') %}
          [245,245,245]
        {% else %}
          [0,245,0]
        {% endif %}  
      brightness: 10
      transition: 2.5
    entity_id: light.quest
  - delay: '00:01:00'
  - service: light.turn_off
    data: {}
    entity_id: light.quest
  mode: single

And please, I just started with HA for a week now…

I think this is what you were after:

  condition:
    - condition: or
      conditions:
        - condition: time
          weekday:
            - mon
            - tue
            - wed
            - thu
            - fri
          after: '15:55:00'
          before: '17:00:00'
        - condition: time
          weekday:
            - sat
            - sun
          after: '08:55:00'
          before: '09:05:00'

Weekdays in the evening or weekends in the morning.

1 Like

Thank you again!

Actually 8 am at weekdays and 9 am at weekend but I had to test it somehow.
It was a bit confusing for me to start the conditioning with “OR” but I learned the lesson and I feel will be able to do way more after some accommodation time.

I tried this, however. My lights only turn green and not the other colors even if it’s raining. :sweat_smile:

Did you test it in the template editor?

Are your forecast states the same?

Thanks, i have another entitiy name: weather.home instead of sensor.openweathermap_forecast_condition. It seems to work for now! Lets wait till the weather changes to see what it does

Here I post an automation example to change a light color according to the default weather forecast

alias: Weather Light
description: ""
trigger:
  - platform: state
    entity_id:
      - weather.forecast_home
action:
  - entity_id: light.vocolinc_flb_1abaca_mood_light
    data:
      color_name: >-
        {{ weather.get(states.weather.forecast_home.state,{'color':'red'}).color }}
    action: light.turn_on
variables:
  weather:
    clear-night:
      color: blue
    rainy:
      color: cyan
    sunny:
      color: orange
    cloudly:
      color: purple
  current_weather: "{{ states.weather.forecast_home.state }}"