Philips Hue and weather.home and met office (UK) to display forecasts for temperature and state

I have a little project on the go - I’m using two bulbs, #1 displays the temperature forecast in terms of weather chart colours and #2 displays different colours for conditions.
For example. Sunny produces yellow/orange. Cloudy produces off white, rain produces blue etc.

I have the temperature colours changing as expected:

service: light.turn_on
target:
  entity_id: light.hue_color_lamp_1
data_template:
  brightness_pct: 75
  transition: 10
  hs_color: >
    {% set temperature =
    state_attr('weather.met_office_saltdean_beach_3_hourly', 'temperature') %}
    {% set hue = 0 %} {% if temperature < -10 %}
      {% set hue = 240 %}
    {% elif temperature < 0 %}
      {% set hue = (240 - ((temperature + 10) / 10 * 30)) | round(0) %}
    {% elif temperature < 10 %}
      {% set hue = (180 - ((temperature - 0) / 10 * 60)) | round(0) %}
    {% elif temperature < 15 %}
      {% set hue = (120 - ((temperature - 10) / 5 * 30)) | round(0) %}
    {% elif temperature < 25 %}
      {% set hue = (60 - ((temperature - 15) / 10 * 30)) | round(0) %}
    {% elif temperature < 35 %}
      {% set hue = (30 - ((temperature - 25) / 10 * 15)) | round(0) %}
    {% else %}
      {% set hue = (330 - ((temperature - 35) / 5 * 30)) | round(0) %}
    {% endif %} {{ hue }}, 100

but the forecast automation isn’t changing as expected.

service: light.turn_on
target:
  entity_id: light.hue_go_1_4
data_template:
  brightness_pct: 60
  transition: 10
  hs_color: >
    {% set forecast = state_attr('weather.home', 'forecast') | lower %} {% set
    hue = 0 %} {% if 'partlycloudy' in forecast %}
      {% set hue = 195 %}
    {% elif 'sunny' in forecast or 'sun' in forecast or 'mostlysunny' in
    forecast or 'mostly sunny' in forecast %}
      {% set hue = 60 %}
    {% elif 'cloudy' in forecast %}
      {% set hue = 220 %}
    {% elif 'mostlycloudy' in forecast %}
      {% set hue = 195 %}
    {% elif 'rainy' in forecast or 'rain' in forecast or 'heavyrain' in forecast
    or 'thunderstorms' in forecast %}
      {% set hue = 240 %}
    {% endif %} {% if 'partlycloudy' in forecast or 'cloudy' in forecast or
    'mostlycloudy' in forecast or 'fog' in forecast %}
      {% set saturation = 15 %}
    {% else %}
      {% set saturation = 100 %}
    {% endif %} {{ hue }}, {{ saturation }}

Is it obvious to anyone else where I have gone wrong?
cheers.

I just put that* (the first one) into Developer tools ➜ Template and changed to the Met office for my area.

My current temp is 24℃ and the output was 30,100 which is correct, based on:

{% elif temperature < 25 %} {% set hue = (60 - ((temperature - 15) / 10 * 30)) | round(0)

[edit] clarified ‘that’*

[edit2] my temp just changed to 25℃ and the output became 30,100 - also correct based on:

{% elif temperature < 35 %} {% set hue = (30 - ((temperature - 25) / 10 * 15)) | round(0) %}

The temperature works for me too (glad it’s working for you).
It’s the 2nd one I’m having trouble with. I’m trying Met office to see if that works

service: light.turn_on
target:
  entity_id: light.hue_go_1_4
data_template:
  brightness_pct: 60
  transition: 10
  hs_color: >
    {% set forecast = state_attr('weather.met_office_saltdean_beach_3_hourly', 'forecast') | lower %} {% set
    hue = 0 %} {% if 'partlycloudy' in forecast %}
      {% set hue = 195 %}
    {% elif 'sunny' in forecast or 'sun' in forecast or 'mostlysunny' in
    forecast or 'mostly sunny' in forecast %}
      {% set hue = 60 %}
    {% elif 'cloudy' in forecast %}
      {% set hue = 220 %}
    {% elif 'mostlycloudy' in forecast %}
      {% set hue = 195 %}
    {% elif 'rainy' in forecast or 'rain' in forecast or 'heavyrain' in forecast
    or 'thunderstorms' in forecast %}
      {% set hue = 240 %}
    {% endif %} {% if 'partlycloudy' in forecast or 'cloudy' in forecast or
    'mostlycloudy' in forecast or 'fog' in forecast %}
      {% set saturation = 15 %}
    {% else %}
      {% set saturation = 100 %}
    {% endif %} {{ hue }}, {{ saturation }}

ugh - sorry. Tryng the 2nd one now…

Ok great, thanks!

OK, I think you need an {% else %} statement for forecast as mine is `Lightning, rainy’ right now which isn’t covered in your code.

    {% if 'partlycloudy' in forecast %} {% set hue = 195 %}
      {% elif 'sunny' in forecast or 'sun' in forecast or 'mostlysunny' in forecast or 'mostly sunny' in forecast %} {% set hue = 60 %}
      {% elif 'cloudy' in forecast %} {% set hue = 220 %}
      {% elif 'mostlycloudy' in forecast %} {% set hue = 195 %}
      {% elif 'rainy' in forecast or 'rain' in forecast or 'heavyrain' in forecast or 'thunderstorms' in forecast %} {% set hue = 240 %}

      {% else %} {% set hue = <xxx> %}
           ^

      {% endif %}

The forecast attribute contains a list value. Each item in the list is a dictionary containing several keys including a condition key. That’s the one whose value can be rainy, sunny, etc.

To get the value of the first item’s (zeroth index position) condition key, you would do this:

state_attr('weather.home', 'forecast')[0].condition

Example

service: light.turn_on
target:
  entity_id: light.hue_go_1_4
data:
  brightness_pct: 60
  transition: 10
  hs_color: >
    {% set forecast = state_attr('weather.home', 'forecast')[0].condition | lower %}
    {% set hue = 0 %}
    {% if 'partlycloudy' in forecast %}
      {% set hue = 195 %}
    {% elif 'sunny' in forecast or 'sun' in forecast or 'mostlysunny' in
    forecast or 'mostly sunny' in forecast %}
      {% set hue = 60 %}
    {% elif 'cloudy' in forecast %}
      {% set hue = 220 %}
    {% elif 'mostlycloudy' in forecast %}
      {% set hue = 195 %}
    {% elif 'rainy' in forecast or 'rain' in forecast or 'heavyrain' in forecast
    or 'thunderstorms' in forecast %}
      {% set hue = 240 %}
    {% endif %} {% if 'partlycloudy' in forecast or 'cloudy' in forecast or
    'mostlycloudy' in forecast or 'fog' in forecast %}
      {% set saturation = 15 %}
    {% else %}
      {% set saturation = 100 %}
    {% endif %}
    [{{ hue }}, {{ saturation }}]
2 Likes

Here are all the Met Office conditions that I. am aware of:

extreme heat
fog
ice
lightning
rain
rain, wind
snow
snow, ice
thunderstorm
thunderstorms
wind

Brilliant! I’m gonna try it.
Thank you!

following on from this, if you want to use the met_office, it would be:

{% set forecast = state_attr('weather.met_office_saltdean_beach_3_hourly', 'forecast')[0].condition %}
1 Like

I’ve just changed it to the met office and now the light is Yellow to represent the Sun as it’s nice and sunny here atm. Looking good! Hopefully this is going to work. I appreciate your help!

That’s kind to mark my post as the solution, but @123 fixed that part of the problem (I only really did the missing {% if statement)

2 Likes

It was a good combined effort :+1:

1 Like