Changing rgb color according to owm weather forecast

http://www.yamllint.com/ + HA config validator + HA template tab

The template works fine, just tested it. If it fails yamllint this is due to the syntax. As mentioned before these are two different things.

I got you there.

According to yamlint petros code is valid.
HA inbuild config validator says the file doesnt work

using the template tester, this works fine in my system:

{% if is_state('sensor.yweather_condition', 'Snow') %}
  {% set x = {'rgb_color':[1,2,3]} %}
{% endif %}
{{ x }}

I’m not sure why its not working for you. What is the error?

The data template in my example has a different dictionary shape than yours. Thats why it looks different:

This:

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

translates to this dictionary shape:

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

This:

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

translates to:

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

1 Like
 - action: ?
        - data_template: ?
            rgb_color: [source /config/automations.yaml:65]
              - <homeassistant.helpers.template.Template object at 0x73af2d70>
              - <homeassistant.helpers.template.Template object at 0x73af2ed0>
              - <homeassistant.helpers.template.Template object at 0x73aebeb0>
            transition: <homeassistant.helpers.template.Template object at 0x73af2db0>
          entity_id: ?

can you paste your yaml? That error doesn’t help at all.

input_boolean:
  weather_forecast_status:
    initial: 'on'

automation:
  - 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') %}
             {% 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 }}

Try this. Added an else clause at the end incase its none of those scenarios so that it has a data template.

input_boolean:
  weather_forecast_status:
    initial: 'on'

automation:
  - 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') %}
            {% 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] } %}
          {% else %}
            {% set d = {'rgb_color':[0,0,0] } %}
          {% endif %}
          {{ d }}
1 Like

thank you so far :slight_smile: really glad you are putting time into it. Inbuilt config validator shows me this (hope it helps):

[automation]: expected a dictionary for dictionary value @ data['action'][0]['data_template']. Got None. (See /config/configuration.yaml, line 77). Please check the docs at https://home-assistant.io/components/automation/

I’m not sure, that doesn’t make sense because it always returns a dictionary.

I donno… try:

input_boolean:
  weather_forecast_status:
    initial: 'on'

automation:
  - 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') %}
            {{ dict(rgb_color=[250,250,210]) }}
          {% elif is_state('sensor.owm_forecast', 'Clouds') %}
            {{ dict(rgb_color=[135,206,235]) }}
          {% elif is_state('sensor.owm_forecast', 'Rain') %}
            {{ dict(rgb_color=[30,144,255]) }}
          {% elif is_state('sensor.owm_forecast', 'Snow') %}
            {{ dict(rgb_color=[245,245,245]) }}
          {% else %}
            {{ dict(rgb_color=[0,0,0]) }}
          {% endif %}

If this doesn’t work then I don’t know what will.

sorry - no luck. inbuild validator throws an error

Hi guys,
I-ve it working with code below , but its implemented in BRUH multisensor and other mqtt light as well
note that trigger for showing the "weather color " is movement but IF then ELSE is the same , plus with some new states from OpenWeather API

  • alias: ‘sensor.mq_pir turn_on light.galo_tempo motion_detected’
    trigger:
    platform: state
    entity_id: sensor.mq_pir
    to: ‘motion detected’
    action:
    • service: mqtt.publish
      data_template:
      topic: “bruh/mqsen/set”
      payload: >
      {% if is_state(“sensor.openw_forecast”, “Clear”) %} {“state”: “ON”, “brightness”: 254, “color”: {“r”: 0, “g”: 0,“b”: 250}}
      {% elif is_state(“sensor.openw_forecast”, “clear sky”) %} {“state”: “ON”, “brightness”: 254, “color”: {“r”: 0, “g”: 0,“b”: 250}}
      {%-elif is_state(“sensor.openw_forecast”, “broken clouds”) %} {“state”: “ON”, “brightness”: 254, “color”: {“r”: 0, “g”: 100,“b”: 250}}
      {%-elif is_state(“sensor.openw_forecast”, “Clouds”) %} {“state”: “ON”, “brightness”: 254, “color”: {“r”: 0, “g”: 250,“b”: 0}}
      {%-elif is_state(“sensor.openw_forecast”, “Rain”) %} {“state”: “ON”, “brightness”: 254, “color”: {“r”: 250, “g”: 0,“b”: 0}}
      {%-elif is_state(“sensor.openw_forecast”, “light rain”) %} {“state”: “ON”, “brightness”: 254, “color”: {“r”: 150, “g”: 100,“b”: 0}}
      {%-elif is_state(“sensor.openw_forecast”, “Snow”) %} {“state”: “ON”, “brightness”: 254, “color”: {“r”: 0, “g”: 250,“b”: 250}}
      {% endif %}

… I would recommend a MQTT client , with subscrition to all mqtt topics, aiming to troubleshooting the wrong code …

I too have been struggling with this error

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

all day, trying to get my hue lights to turn on more blue if the weather is cold, in the morning. I just couldn’t get the template to pass the correct info for xy_color.

I’ve managed to bodge it by calling a scene instead. I created 3 scenes (ice / medium / hot)…

- name: KitchenIce
  entities:
    light.kitchen_strip:
      state: on
      xy_color: [0.2309,0.2889]
- name: KitchenMed
  entities:
    light.kitchen_strip:
      state: on
      xy_color: [0.3542,0.362]
- name: KitchenHot
  entities:
    light.kitchen_strip:
      state: on
      xy_color: [0.4949,0.4202]

Then, I call the scene depending on how cold the met office sensor is.

- id: WeekdayMorningLightBoost
  alias: Weekday Morning Light Boost
  trigger:
    platform: time
    at: '06:55:00'
  condition:
    condition: time
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
  action:
    service: scene.turn_on
    data_template:
      entity_id: >-
        {%- if states.sensor.met_office_temperature.state|int >= 10 -%}
          scene.kitchenhot
        {%- elif states.sensor.met_office_temperature.state|int <= 2 -%}
          scene.kitchenice
        {%- else -%}
          scene.kitchenmed
        {%- endif -%}

I’m a complete beginner, but through trial and error, and googling all day, this seems to be working for me.

ps. Is there a guide somewhere as to what >- means, and the difference between {% and {%- ??

This right here is telling you what you need to change. According to the docs, rgb_color requires a list of integers. It’s saying that you gave it a string ‘245, 245, 245’. Essentially this:

‘245, 245, 245’

vs

[ 245, 245, 245 ]

Sorry, I wasn’t clear. The above error is the OP’s. I’m trying to change the xy_color depending on the sensor.met_office_temperature. I’ve been following this thread, this one and this one, and trying to get it to work.

Here’s my current error

Invalid service data for light.turn_on: None for dictionary value @ data['xy_color']. Got '[0.4949,0.4202]'

and this is the automation

- id: test
  alias: test
  trigger:
    platform: time
    at: '10:00:00'
  action:
    service: light.turn_on
    data_template:
      entity_id: light.kitchen_strip
      brightness: 200
      xy_color: > 
        {% if states.sensor.met_office_temperature.state|int >=10 %}
          [{{0.2309|float}},{{0.2889|float}}]
        {% elif states.sensor.met_office_temperature.state|int <= 2 %}
          [{{0.3542|float}},{{0.362|float}}]
        {% else %}
          [{{0.4949|float}},{{0.4202|float}}]
        {% endif %}

floats are redundant and that jinja looks odd. You don’t need {{}} unless you are pulling the information from a variable. Because you are setting the lights directly, you should only need the number. You don’t even need to convert it to a float because you have a decimal place.

Personally, I’m starting to think that value templates don’t work for lists.

Anyways, try this out:

- id: test
  alias: test
  trigger:
    platform: time
    at: '10:00:00'
  action:
    service: light.turn_on
    data_template:
      entity_id: light.kitchen_strip
      brightness: 200
      xy_color: > 
        {% if states.sensor.met_office_temperature.state|int >=10 %}
          [ 0.2309, 0.2889 ]
        {% elif states.sensor.met_office_temperature.state|int <= 2 %}
          [ 0.3542, 0.362 ]
        {% else %}
          [ 0.4949, 0.4202 ]
        {% endif %}

Thank you for the reply, and your suggested yaml. The reason I tried floats and excessive {{}}'s was due to this post and this one. I don’t think I can use int as xy_color are decimals, so I tried floats.

Unfortunately, this is the log when I try your suggestion, so I think you might be right.

Invalid service data for light.turn_on: None for dictionary value @ data['xy_color']. Got '[ 0.4949, 0.4202 ]'

Maybe I need to do it like

{% if states.sensor.met_office_temperature.state|int >=10 %}
  {% set x as 0.2309 set y as 0.2889 %}
{% elif states.sensor.met_office_temperature.state|int <= 2 %}
  {% set x as  0.3542 set y as 0.362 %}
{% else %}
  {% set x as 0.4949 set y as 0.4202 %}
{% endif %}
['{{x|float}}','{{y|float}}']

???

you don’t want the quotes if you were to try that. quotes turn items into a string. It’s expecting floats. The issue is that it doesn’t see that item as a dictionary value. I don’t think templates support lists, so all this is a lost cause. You’re better off creating the 3 automations.

{% if states.sensor.met_office_temperature.state|int >=10 %}
  {% set l = [0.2309, 0.2889] %}
{% elif states.sensor.met_office_temperature.state|int <= 2 %}
  {% set l = [0.3542, 0.362] %}
{% else %}
  {% set l = [0.4949, 0.4202] %}
{% endif %}
{%- for v in l -%}
  {{ v }}
{%- endfor -%}

Just try this… make sure the for loop has the {%- and -%}. You don’t need it on the {{ v }}.