Changing rgb color according to owm weather forecast

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

Sadly none of the codes work yet.
…Aaaannnd OWM just changed their API, so there are more than 4 Conditions now.

So you don’t know what changing rgb in a data section would look like without using a data_template and just using yaml?

Example, for a IOS notification, the data shape looks like this in yaml:

- service: nofity.ios_phone
  data:
    push:
      badge: 5

So if i were to make a data template as a dictionary, it would look like this:

data_template: “{“push”:{“badge”:5}}”

If you can show me the actual shape of the yaml, i can help you make a data_template that works.

thank you for your help.

     action:
        - service: light.turn_on
          entity_id: light.stehlampe_rgb
          data:
            rgb_color: [30,144,255]

this works without any problems.

odd, then a data_template like this should work:

{“rgb_color”:[30,144,255]}

try this:

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

Sorry but this fails at config validation :frowning:

I might be wrong on this but the template might be

"{“rgb_color”:[30,144,255]}"

because in your example it was:

data_template: “{“push”:{“badge”:5}}”

How did you test if it’s valid?