Help with Automation based on precipitation probability

I’m having trouble getting an automation I want to set up to work properly. The goal is to have the light change color based on the precipitation probability.
Here is what I have right now:

alias: "Light based on weather"
trigger:
- entity_id: sensor.dark_sky_precip_probability
  platform: state
condition: []
action:
- service: light.turn_on
  data_template:
    entity_id: light.lamp
    brightness_pct: 100
    rgb_color: >-
      {% if states.sensor.dark_sky_precip_probability.state | int < 30 %}
        [255,255,102]
      {% elif states.sensor.dark_sky_precip_probability.state | int < 60 %}
        [102,255,255]
      {% elif states.sensor.dark_sky_precip_probability.state | int < 90 %}
        [51,153,255]
      {% else %}
        [0,0,204]
      {% endif %}

you have to template each level

alias: "Light based on weather"
trigger:
- entity_id: sensor.dark_sky_precip_probability
  platform: state
condition: []
action:
- service: light.turn_on
  data_template:
    entity_id: light.lamp
    brightness_pct: 100
    rgb_color:
      - {% if.... %} first value R
      - {% if.... %} second value G
      - {% if.... %} third value B

It’s not very nice, but it’s a limitation of yaml.

So something like this? Still haven’t quite got it

  data_template:
    entity_id: light.lamp
    brightness_pct: 100
    rgb_color: >-
      - {% if states.sensor.dark_sky_precip_probability.state | int < 30 %} 255
        {% elif states.sensor.dark_sky_precip_probability.state | int < 60 %} 102
        {% elif states.sensor.dark_sky_precip_probability.state | int < 90 %} 51
        {% else %} 0
        {% endif %}
      - {% if states.sensor.dark_sky_precip_probability.state | int < 30 %} 255
        {% elif states.sensor.dark_sky_precip_probability.state | int < 60 %} 255
        {% elif states.sensor.dark_sky_precip_probability.state | int < 90 %} 153
        {% else %} 0
        {% endif %}
      - {% if states.sensor.dark_sky_precip_probability.state | int < 30 %} 102
        {% elif states.sensor.dark_sky_precip_probability.state | int < 60 %} 255
        {% elif states.sensor.dark_sky_precip_probability.state | int < 90 %} 255
        {% else %} 204
        {% endif %}

yeah that should work but remove the >- on the rgb_color line. Place it in each arrow line

    entity_id: light.lamp
    brightness_pct: 100
    rgb_color:
      - >
        {% if states.sensor.dark_sky_precip_probability.state | int < 30 %} 255
        {% elif states.sensor.dark_sky_precip_probability.state | int < 60 %} 102
        {% elif states.sensor.dark_sky_precip_probability.state | int < 90 %} 51
        {% else %} 0
        {% endif %}
      - >
        {% if states.sensor.dark_sky_precip_probability.state | int < 30 %} 255
        {% elif states.sensor.dark_sky_precip_probability.state | int < 60 %} 255
        {% elif states.sensor.dark_sky_precip_probability.state | int < 90 %} 153
        {% else %} 0
        {% endif %}
      - >
        {% if states.sensor.dark_sky_precip_probability.state | int < 30 %} 102
        {% elif states.sensor.dark_sky_precip_probability.state | int < 60 %} 255
        {% elif states.sensor.dark_sky_precip_probability.state | int < 90 %} 255
        {% else %} 204
        {% endif %}

Learning experience for me! I’ve never seen this arrangement before. This template works because rgb_color is an array with three items?

Yeah, pretty much. I can’t say for certain it works, but people I have helped have said it works. I don’t have any rgb lights. All we are doing here is taking the rgb field from this:

rgb: [ r, g, b ]

to this

rgb:
 - r
 - g
 - b

which, in terms of yaml, is the same.

2 Likes