Changing rgb color according to owm weather forecast

Hi guys! :smiley:
inspired by this thread: Automation - light Color change with weather forecast
I am trying to change the color of my IKEA lights according to the openweathermap forecast. The YAML files are valid but somehow the code doesnt work. Plz help me out.

automation:
alias: 'Wetter Vorhersage Automatisierung'
hide_entity: False
initial_state: 'on'
trigger:
  - platform: state
    entity_id: sensor.owm_forecast
condition:
  - condition: state
    entity_id: input_boolean.weather_forecast_status
    state: 'on'
action:
  service: light.turn_on
  entity_id: light.stehlampe_rgb
  data_template:
  >      
      {%- if is_state('sensor.owm_forecast', 'Clear') -%}
        brightness: 255
        rgb_color: ['250','250','210']
      {%-elif is_state('sensor.owm_forecast', 'Clouds') -%}
        brightness: 255          
        rgb_color: ['135','206','235']
      {%-elif is_state('sensor.owm_forecast', 'Rain') -%}
        brightness: 255          
        rgb_color: ['30','144','255']
      {%-elif is_state('sensor.owm_forecast', 'Snow') -%}
        brightness: 255          
        rgb_color: ['245','245','245']
      {%- endif %}

I changed the code a bit. HA loads up now and all the input sliders are there BUT it does not trigger the color change:

alias: "Wetter Vorhersage Automatisierung"
hide_entity: False
initial_state: 'off'
trigger:
  - platform: state
    entity_id: sensor.owm_forecast
condition:
  - condition: state
    entity_id: input_boolean.weather_forecast_status
    state: 'on'
action:
  - service: light.turn_on
    entity_id: light.stehlampe_rgb
    data_template:
      brightness: 254
      rgb_color: >-
       {% if states.sensor.owm_forecast.state == 'Clear' %}
         ['250','250','210']
       {% elif states.sensor.owm_forecast.state == 'Clouds' %}
         ['135','206','235']
       {% elif states.sensor.owm_forecast.state == 'Rain' %}
         ['30','144','255']
       {% elif states.sensor.owm_forecast.state == 'Snow' %}
         ['245','245','245']
       {% endif %}

I narrowed it down to the last line, where something with the spacing is going wrong (i guess). If i put the if clause in one line the results in the “templates testing tab” are looking good BUT HA does not boot up with this config. Please help me out :slight_smile:

alias: "Wetter Vorhersage Automatisierung"
hide_entity: False
initial_state: 'off'
trigger:
  - platform: state
    entity_id: sensor.owm_forecast
condition:
  - condition: state
    entity_id: input_boolean.weather_forecast_status
    state: 'on'
action:
  - service: light.turn_on
    entity_id: light.stehlampe_rgb
    data_template:
      brightness: 254
      rgb_color: - {% if states.sensor.owm_forecast.state == 'Clear' %}['250','250','210']{% elif states.sensor.owm_forecast.state == 'Clouds' %}['135','206','235']{% elif states.sensor.owm_forecast.state == 'Rain' %}['30','144','255']{% elif states.sensor.owm_forecast.state == 'Snow' %}['245','245','245']{% endif %}

Giving up. My last try was this

alias: Wetter Vorhersage Automatisierung
hide_entity: False
initial_state: 'off'
trigger:
  - platform: state
    entity_id: sensor.owm_forecast
condition:
  - condition: state
    entity_id: input_boolean.weather_forecast_status
    state: 'on'
action:
  - service: light.turn_on
    entity_id: light.stehlampe_rgb
    data_template: 
      {% if is_state("sensor.owm_forecast", "Clear") -%}
      rgb_color: 250,250,210
      brightness: 255
      {% elif is_state("sensor.owm_forecast", "Clouds") -%}
      rgb_color: 135,206,235
      brightness: 255
      {% elif is_state("sensor.owm_forecast", "Rain") -%}
      rgb_color: 30,144,255
      brightness: 255
      {% elif is_state("sensor.owm_forecast", "Snow") -%}
      rgb_color: 245,245,245
      brightness: 255
      {%- endif %}

The whole yaml stuff is confusing as hell. Escpecially mixing stuff up with a framework like jinja2…

Anyone now :smiley: ?

I am not a yaml expert but when I paste your code in ATOM with a YAML checker then it gives an error, I marked where the error starts in the above code with “!!”

I think the error is in your templating, check this link:

1 Like

thank you, sadly I already know this - and as far as i know, i am following every templating rule

You’re code doesn’t validate in the “Template” tab. Try this;

{% if is_state('sensor.owm_forecast', 'Clear') -%}
rgb_color: 250,250,210
brightness: 255
{% elif is_state('sensor.owm_forecast', 'Clouds') -%}
rgb_color: 135,206,235
brightness: 255
{% elif is_state('sensor.owm_forecast', 'Rain') -%}
rgb_color: 30,144,255
brightness: 255
{% elif is_state('sensor.owm_forecast', 'Snow') -%}
rgb_color: 245,245,245
brightness: 255
{%- endif %}
1 Like

thanks but sadly this isnt a valid yaml…
YAML Validators will not parse the first line but the “template” tester of HA shows the result i want.

Brings me to an Idea, does the code have to be valid reagarding “standard yaml” or jinja2?

Yaml and Jinja2 are two different things. Jinja2 is templating, Yaml is formatting. Your template did not comply to Jinja2. Now it does. If it passed the HA template testing but your automation still doesn’t work you should check the (yaml) formatting.

1 Like

Thanks to you guys a made some improvements. No I am having a problem. If I go single line like this

    data_template:
        rgb_color: -'{% if is_state('sensor.owm_forecast', 'Clear') -%}250,250,210{% elif is_state('sensor.owm_forecast', 'Clouds') -%}135,206,235{% elif is_state('sensor.owm_forecast', 'Rain') -%}30,144,255{% elif is_state('sensor.owm_forecast', 'Snow') -%}245,245,245{%- endif %}'

I get:

Invalid service data for light.turn_on: None for dictionary value @ data['rgb_color']. Got "-'245,245,245'"

How do I get rid of - and " in the parsed code?

If i go multi-line the color codes are one line below the rgb_color and therefore not working…

found another topic tackling this bug:

sadly none of the solutions work for me

wrong post . not mqtt

rgb_color: expects a list of integers: [ 0, 0, 0 ]. From the looks of this thread, the closest you got was a list of strings: [ ‘0’,‘0’,‘0’ ].

So try:

  data_template:
        rgb_color: -'{% if is_state('sensor.owm_forecast', 'Clear') -%}[250,250,210]{% elif is_state('sensor.owm_forecast', 'Clouds') -%}[135,206,235]{% elif is_state('sensor.owm_forecast', 'Rain') -%}[30,144,255]{% elif is_state('sensor.owm_forecast', 'Snow') -%}[245,245,245]{%- endif %}'
1 Like

as for advice on debugging, I would suggest to check results on mqtt dev of your HA , but at the same time, to “see” the mqtt BUS messages, i’ve installed a windows mqtt client (using MQTT.fx ), subscribing all topics.

1 Like

thank you but the output equals:

rgb_color: -'[30,144,255]'

which is which doesnt work because of the - and the ‘’

try:

 data_template:
        rgb_color: >
          {% if is_state('sensor.owm_forecast', 'Clear') %}
            [250,250,210]
          {% elif is_state('sensor.owm_forecast', 'Clouds') %}
            [135,206,235]
          {% elif is_state('sensor.owm_forecast', 'Rain') %}
            [30,144,255]
          {% elif is_state('sensor.owm_forecast', 'Snow') %}
            [245,245,245]
          {% endif %}

I saw a bunch of -%} and {%- all over the place in your syntax. I’ve never had to use that. I’m also not familiar with what those do. They may be needed, but give this a whirl without them.

1 Like

Results in…

Invalid service data for light.turn_on: None for dictionary value @ data['rgb_color']. Got '[135,206,235]'

…like most “multi line attempts” on this.

Do we know the normal shape of the dictionary for changing the color? What would a normal template look like without it being dynamic?

Try:

        data_template:
          {% if is_state('sensor.owm_forecast', 'Clear') %}
             {'rgb_color':[250,250,210] }
          {% elif is_state('sensor.owm_forecast', 'Clouds') %}
            {'rgb_color':[135,206,235] }
          {% elif is_state('sensor.owm_forecast', 'Rain') %}
            {'rgb_color':[30,144,255] }
          {% elif is_state('sensor.owm_forecast', 'Snow') %}
            {'rgb_color':[245,245,245] }
          {% endif %}

That will return a dictionary, which is what the ‘data’ template requires.

1 Like

I have same problems!
I want to change rgb color depending on time.
Use almost same code but it gives me the same error messages.

1 Like