#Increment/decrement a lights color value

1 messages · Page 1 of 1 (latest)

teal trout
#

I'm trying to increment/decrement one of the rgb values for a light. The use case is I have a standard 44 key IR remote made for controlling lights. It has Inc/Dec buttons for each color. I'd like to be able to use those to fine tune the light's color from the remote.

I thought a script might be a good way to go so here's my attempt at adding 10 to the current red value from an rgbww bulb. Unfortunately it doesn't do anything. Am I close?

  alias: Increase Livingroom Red
  fields:
    entity_id:
      name: athom_rgbww_light_9980ba_rgbcw_bulb
      required: true
      selector:
        entity:
          domain: light
  sequence:
  - variables:
      current_state: '{{ states(athom_rgbww_light_9980ba_rgbcw_bulb) }}'
      current_rgbww: '{{ state_attr(athom_rgbww_light_9980ba_rgbcw_bulb, ''rgb_color'')
        | default([0, 255, 255, 0, 255]) }}'
      current_r: '{{ current_rgbww[0] }}'
      new_r: '{{ ([current_r + 10, 255]) | min }}'
      new_rgbww: '[{{ new_r }}, {{ current_rgbww[1] }}, {{ current_rgbww[2] }}, {{
        current_rgbww[3] }}, {{ current_rgbww[4] }}]'
  - target:
      entity_id: '{{ athom_rgbww_light_9980ba_rgbcw_bulb }}'
    data:
      rgbww_color: '{{ new_rgbww }}'
    action: light.turn_on```
small sail
#

What does the trace of the script tell you?

teal trout
#

Executed: October 19, 2025 at 2:37:24 PM
Error: UndefinedError: 'athom_rgbww_light_9980ba_rgbcw_bulb' is undefined

small sail
#

That's because of this line

- target:
      entity_id: '{{ athom_rgbww_light_9980ba_rgbcw_bulb }}'
#

I assume that should be

- target:
      entity_id: light.athom_rgbww_light_9980ba_rgbcw_bulb
teal trout
#

The problem seems to be with the "current_state" declaration.
If I change that to
current_state: '{{ states(light.athom_rgbww_light_9980ba_rgbcw_bulb) }}'
the error changes to "Error: UndefinedError: 'light' is undefined".

Here's the script as it now sits.

  alias: Increase Livingroom Red
  fields: {}
  sequence:
  - variables:
      current_state: "{{ states(light.athom_rgbww_light_9980ba_rgbcw_bulb) }}"
      current_rgbww: "{{ state_attr(light.athom_rgbww_light_9980ba_rgbcw_bulb, 'rgbww_color') }}"
      current_value: "{{ current_rgbww[0] }}"
      new_value: "{{ ([current_value + 10, 255]) | min }}"
      new_rgbww: "[{{ new_value }}, {{ current_rgbww[1] }}, {{ current_rgbww[2] }},
        {{ current_rgbww[3] }}, {{ current_rgbww[4] }}]"
  - target:
      entity_id: light.athom_rgbww_light_9980ba_rgbcw_bulb
    data:
      rgbww_color: "{{ new_rgbww }}"
    action: light.turn_on```

This is being called from an ESPHome device using the following:
```#---------------------------------------------------
  - platform: remote_receiver
    name: "Toggle Test"
    id: toggle_test
#   Remote 'Flash'
    internal: True
    filters:
      - delayed_off: 100ms
    pioneer:
      rc_code_1: 0x000B
    on_release:
      - homeassistant.service: 
          service: script.inc_lvrm_red
          data:
            entity_id: light.athom_rgbww_light_9980ba_rgbcw_bulb
#---------------------------------------------------```

I've tried removing/adding the "light." in every possible combination I can think of but still get the error.
opal mulch
#

States is a function and 'light.yourlight' is a string and must be passed as a string. Use quotes.

#

This:
current_state: '{{ states(light.athom_rgbww_light_9980ba_rgbcw_bulb) }}'
and this:
current_state: '{{ states('light.athom_rgbww_light_9980ba_rgbcw_bulb') }}'

Is not the same

#

In first code, it implies that the light is a dictionary variable with athom_rgbww_light_9980ba_rgbcw_bulb subentry, while second example is just a string, and this is what you want.

small sail
#

as you are using a script there with an entity_id selector, I'd assume you actually want to use the input from that selector, and not a fixed entity_id

#

but that selector creates a variable called entity_id not a vaiable called athom_rgbww_light_9980ba_rgbcw_bulb

#
  fields:
    entity_id: # << this field determines the name of the variable
      name: athom_rgbww_light_9980ba_rgbcw_bulb # << this determines how it is displayed in the GUI
      required: true
      selector:
        entity:
          domain: light
teal trout
#

I think we have that issue resolved (Thanks for that 🍻 ) because I'm now getting an error when it's building the String.

The error is "Error: TypeError: can only concatenate str (not "int") to str".

I've tried a few variations to build the new_rgbww string but keep getting the same error. I didn't think any explicit int to string conversion was needed because Jinja would do the conversion for me when it determines we're building a string.

Here's the variations I've tried.

new_rgbww: >
  "[{{ new_value | string }}, + {{ current_rgbww[1] | string }}, + {{ current_rgbww[2] | string }}, + {{ current_rgbww[3] | string }}, + {{ current_rgbww[4] | string }}]"```

new_rgbww: >
"[{{ new_value }}, + {{ current_rgbww[1] }}, + {{ current_rgbww[2] }}, + {{ current_rgbww[3] }}, + {{ current_rgbww[4] }}]" new_rgbww: "[{{ new_value | string }}, + {{ current_rgbww[1] | string }}, + {{ current_rgbww[2] | string }}, + {{ current_rgbww[3] | string }}, + {{ current_rgbww[4] | string }}]" new_rgbww: "[{{ new_value }}, {{ current_rgbww[1] }}, {{ current_rgbww[2] }}, {{ current_rgbww[3] }}, {{ current_rgbww[4] }}]" new_rgbww: "[{{ 'new_value' }}, {{ 'current_rgbww[1]' }}, {{ 'current_rgbww[2]' }}, {{ 'current_rgbww[3]' }}, {{ 'current_rgbww[4]' }}]"```

small sail
#

Either create a Jinja list like this

new_rgbww: >
  {{
    [
      new_value,
      current_rgbww[1],
      current_rgbww[2],
      current_rgbww[3],
      current_rgbww[4]
    ]
  }}

Or a YAML list like this

new_rgbww: 
  - "{{ new_value }}"
  - "{{ current_rgbww[1] }}"
  - "{{ current_rgbww[2] }}"
  - "{{ current_rgbww[3] }}"
  - "{{ current_rgbww[4] }}"
teal trout
#

Same error with either version 🤔

small sail
#

This error? "Error: TypeError: can only concatenate str (not "int") to str".

teal trout
#

Yes. Can't seem to figure that one out.

small sail
#

That must come from something else, there is no concatenation going on in both of my suggestions

teal trout
#

I think we have a data type mismatch.
I'm thinking the "current_rgbww" declaration is returning as a String, and when we try to add 10 to the Red value is where we're getting the concatenate error.
If I just set "current_rgbww" to [128,128,128,0,200] as int's, it almost works. It does set the light color, but with the wrong values.
This should be sending [138,128,128,0,200]

inc_lvrm_red:
  alias: Increase Livingroom Red
  sequence:
  - variables:
      current_state: '{{ states(''light.athom_rgbww_light_9980ba_rgbcw_bulb'') }}'
      current_rgbww: [128,128,128,0,200] #'{{ state_attr(''light.athom_rgbww_light_9980ba_rgbcw_bulb'', rgbww_color)}}' #[128,128,128,0,200] 
      current_value: '{{ current_rgbww[0] }}'
      new_value: '{{ ([current_value + 10, 255]) | min }}'
      new_rgbww: "[{{ new_value }}, {{ current_rgbww[1] }}, {{ current_rgbww[2] }}, {{ current_rgbww[3] }}, {{ current_rgbww[4] }}]"
  - target:
      entity_id: light.athom_rgbww_light_9980ba_rgbcw_bulb
    data:
      rgbww_color: '{{ new_rgbww }}'
    action: light.turn_on```

The details of the sequence show this which looks correct to me.
```Executed: October 23, 2025 at 2:00:15 PM
Result:
params:
  domain: light
  service: turn_on
  service_data:
    rgbww_color:
      - 138
      - 128
      - 128
      - 0
      - 200
    entity_id:
      - light.athom_rgbww_light_9980ba_rgbcw_bulb
  target:
    entity_id:
      - light.athom_rgbww_light_9980ba_rgbcw_bulb
running_script: false```

But the bulb's States show this. The RGB values are always these values.
```min_color_temp_kelvin: 3000
max_color_temp_kelvin: 6000
min_mireds: 166
max_mireds: 333
supported_color_modes: color_temp, rgb
color_mode: rgb
brightness: 191
color_temp_kelvin: null
color_temp: null
hs_color: 25.479, 36.5
rgb_color: 200, 158, 127
xy_color: 0.425, 0.364
friendly_name: Athom RGBCW Bulb RGBCW_Bulb
supported_features: 40```
small sail
#

Doesn't seem like your bulb supports rgbww

#

Only rgb

#

And color_temp

#

But I'm not completely sure here,

teal trout
#

That confused me as well when I was working to send a specific color from ESPHome.

This is what I ended up with and it works. Just using rgb_color doesn't work.

      - homeassistant.service:
          service: light.turn_on
          data:
            entity_id: light.athom_rgbww_light_9980ba_rgbcw_bulb
            brightness_pct: "75"
          data_template:
            rgbww_color: "[0, 255, 0, 0, 255]" # red, green, blue, cold white, warm white
            color_temp_kelvin: "3000"```
small sail
#

That do you mean with using rgb_color

small sail
#

Going to bed now

teal trout
#

Under the data_template section I send the value array as the rgbww_color attribute.
If I send that as rgb_color instead it doen't work.

teal trout
#

If I create a simple script using the UI script builder, both rgb_color and rgbww_color work.

metadata: {}
data:
  rgb_color:
    - 0
    - 0
    - 255
target:
  entity_id: light.athom_rgbww_light_9980ba_rgbcw_bulb```
```action: light.turn_on
metadata: {}
data:
  rgbww_color:
    - 255
    - 0
    - 0
    - 0
    - 200
target:
  entity_id: light.athom_rgbww_light_9980ba_rgbcw_bulb```
small sail
#

can you share a trace of the script run?

loud oysterBOT
#

Please use a code share site to share code or logs, for example:

Please don't use Pastebin, since it can randomly add spaces to the main view. Please also don't share text as images since it makes it harder for people to help you. Remember that others may have colour blindness, impaired vision, etc.

teal trout
#

Here's the trace.
https://paste.debian.net/1402869/

The error is "Error: TypeError: can only concatenate str (not "int") to str" and is happening on the "new_value" declaration.
I commented out everything, then remove the comment mark one line ata time until the error showed up.

  alias: Increase Livingroom Red
  fields: {}
  sequence:
  - variables:
      current_state: "{{ states('light.athom_rgbww_light_9980ba_rgbcw_bulb') }}"
      current_rgbww: "{{ state_attr('light.athom_rgbww_light_9980ba_rgbcw_bulb', 'rgbww_color') }}"
      current_value: "{{ current_rgbww[0] }}"
      new_value: "{{ ([current_value + 10, 255]) | min }}"
#      new_rgbww: "[{{ new_value }}, {{ current_rgbww[1] }}, {{ current_rgbww[2] }},
#        {{ current_rgbww[3] }}, {{ current_rgbww[4] }}]"
  - target:
      entity_id: light.athom_rgbww_light_9980ba_rgbcw_bulb
    data:
      rgbww_color: "{{ new_rgbww }}"
    action: light.turn_on```
wraith whale
#

Share the trace from when it ran successfully when you had everything after and including the new_value declaration commented out. That will show you what the current_value variable was

#

My guess is that the bulb was off when you ran the script and so those attributes aren’t there. So in that case:

  • current_rgbww will be null
  • current_value will be an empty string
  • new_value will give you the string & int concatenation error
small sail
#

Yep, this is probably what's happening

teal trout
#

Pretty sure the bulb was on but I'll run it again later today when I have time.

teal trout
#

Here's the trace with new_value commented out and verified bulb was on.
https://paste.debian.net/1402981/

The pertinent part seems to be this bit.

              "id": "01K8K3GSZ0AS2QR6WYERNRXY09",
              "parent_id": null,
              "user_id": "3c04e5ae2fe84491b19dc81c364e2d82"
            },
            "current_state": "on",
            "current_rgbww": null,
            "current_value": ""```

current_state shows as 'on' but current_rgbww shows 'null' .

Changing 
```"current_rgbww: '{{ state_attr(''light.athom_rgbww_light_9980ba_rgbcw_bulb'', ''rgbww_color'') }}'``` to
```"current_rgbww: '{{ state_attr(''light.athom_rgbww_light_9980ba_rgbcw_bulb'', ''rgb_color'') }}'``` 
seems to work as the trace now shows
```            "current_state": "on",
            "current_rgbww": [
              0,
              0,
              255
            ],
            "current_value": 0```

So it seems the bulb doesn't actually support the rgbww attribute even though I can use that in a script to set the color as shown in one of my previous replies. HA must be doing some interpreting int he background to just send the rgb colors.
wraith whale
#

Looking back it seems silly that any of us expected the state_attr function to return anything other than null for that attribute 😅