Light Template Help : Set_rgb

Hello, I have been trying to create a custom light template to control my Govee lights via API calls.
the problem is that Govee API only takes a RGB number using a formula : The instance colorRgb can change light color,you can get RGB number follow this formula ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | ((b & 0xFF) << 0)

but set_rgb only takes R,G,B numbers.

How do I implement it?

Here is my code:

platform: template
  lights:
    heater_light:
      friendly_name: "Heater Light"
      unique_id: heater_light
      value_template: "{{ is_state('binary_sensor.heater_nightlight_toggle', 'on') }}"
      level_template: "{{ (states('sensor.heater_brightness') | int * 255 / 100) | int }}"
      availability_template: "{{ is_state('binary_sensor.heater_online', 'on') }}"
      rgb_template: "({{ states('sensor.heater_color_rgb').split(',')[0] | int}} , {{ states('sensor.heater_color_rgb').split(',')[1] | int }}, {{ states('sensor.heater_color_rgb').split(',')[2] | int }})"
      turn_on:
        service: rest_command.set_heater_light_toggle
        data:
          value: 1
      turn_off:
        service: rest_command.set_heater_light_toggle
        data:
          value: 0
      set_level:
        service: rest_command.set_heater_light_brightness
        data:
          value: "{{ (brightness * 100 / 255) | int }}"
      set_rgb:
        service: rest_command.set_govee_light_color
        data:
            rgbw_color:
              - "{{ r }}"
              - "{{ g }}"
              - "{{ b }}"
              - "{{ w }}"

all the other features work but not the Set RGB.
here is my rest command:

set_govee_light_color:
  url: "https://openapi.api.govee.com/router/api/v1/device/control"
  method: "post"
  headers:
    Content-Type: "application/json"
    Govee-API-Key: !secret govee_api_key
  payload: '{"requestId": "uuid", "payload": {"sku": "H7131", "device": "7A:67:D4:AD:FC:F5:69:7B", "capability": {"type": "devices.capabilities.color_setting", "instance": "colorRgb", "value": "{{ rgb_number }}"}}}'


now my question is how do i convert the R,G,B values to RGB number and then pass it to the API?

maybe i’m misunderstanding the question. apologies if i am. but do you mean this?

      set_rgbw:
        service: rest_command.set_govee_light_color
        data:
          rgb_color: >
            {{ ((((r * 256 ) + g) * 256) + b)  }}

HI, Thank you for you response. the API only takes RGB number using the formula, now RGBW. thats why I used RGB/ Can you or anyone tell me how to use use set_rgbw. I cannot seem to get it to work. also I cant Debug as there is no print statement for me to see what values are being sent

i think my code implements your formula. did you try it? does it do what you need?

this:
((r & 0xFF) << 16) | ((g & 0xFF) << 8) | ((b & 0xFF) << 0)

is mathematically the same as

{{ ((((r * 256 ) + g) * 256) + b) }}

unless i have a bug or am misunderstanding.

so if you have r, g, and b values in your light template and you want to service the set_rgb method by calling set_govee_light_color with the numeric formula they specified, i’d try my code to calculate that formula. if i’m misunderstanding what you need, i’ll need you to clarify.

by the way, you didn’t specify the foruma for rgbw, only for rgb, so i switched it to use rgb_color, not rgbw_color. if you have the forumla for how to include w, i’m sure modifying the formula would be easy.

Yes I tried, still dosent work … :frowning:
My updated code

- platform: template
  lights:
    heater_light:
      friendly_name: "Heater Light"
      unique_id: heater_light
      value_template: "{{ is_state('binary_sensor.heater_nightlight_toggle', 'on') }}"
      level_template: "{{ (states('sensor.heater_brightness') | int * 255 / 100) | int }}"
      availability_template: "{{ is_state('binary_sensor.heater_online', 'on') }}"
      rgb_template: "({{ states('sensor.heater_color_rgb').split(',')[0] | int}} , {{ states('sensor.heater_color_rgb').split(',')[1] | int }}, {{ states('sensor.heater_color_rgb').split(',')[2] | int }})"
      turn_on:
        service: rest_command.set_heater_light_toggle
        data:
          value: 1
      turn_off:
        service: rest_command.set_heater_light_toggle
        data:
          value: 0
      set_level:
        service: rest_command.set_heater_light_brightness
        data:
          value: "{{ (brightness * 100 / 255) | int }}"
      set_rgb:
        service: rest_command.set_heater_light_color_test
        data:
          rgb_color: >
            {{ ((((r * 256 ) + g) * 256) + b) }}


and my updated rest command


set_heater_light_color_test:
  url: "https://openapi.api.govee.com/router/api/v1/device/control"
  method: POST
  headers:
      Content-Type: application/json
      Govee-API-Key: !secret govee_api_key
  payload: '{"requestId": "uuid", "payload": {"sku": "H7131", "device": "7A:67:D4:AD:FC:F5:69:7B", "capability": {"type": "devices.capabilities.color_setting", "instance": "colorRgb", "value": "{{rgbw_color}}"}}}'

when the payload is this, it works Here:

set_heater_light_color_test:
  url: "https://openapi.api.govee.com/router/api/v1/device/control"
  method: POST
  headers:
      Content-Type: application/json
      Govee-API-Key: !secret govee_api_key
  payload: '{"requestId": "uuid", "payload": {"sku": "H7131", "device": "7A:67:D4:AD:FC:F5:69:7B", "capability": {"type": "devices.capabilities.color_setting", "instance": "colorRgb", "value": "13206715"}}}'

if you check the value of the payload youll see
FYI, 13206715 is the RGB number that the API uses, (13206715 = R 201, G 132, B 187 using the formula)

Is there anyway to debug the code? and see what RGB values HA is trying to send to the api?

Currently everything works except set RGB, just I cant set the Color, but it does detect the correct color when I change it from the Govee Phone app, and I see my Icon color changing, but I cant set the color from HA, hope this clarifies your question

If you scroll down in the documentation in Color_setting section youll see how it works
for your reference govee API documentation:

another thing is that my turn_on and Turn_off uses a different service , and set_rgb uses a different service (I just send 1, 0 to turn and off the device). and upon debugging and looking at others implementation, they have the same service for Turn ON a, Turn Off and Set RGB. Could that be the reason? cuz Sometimes I see a error that set_rgb is tryng to call the service light/turn_on

ok, if you put those same rgb values into
{{ ((((201 * 256 ) + 132) * 256) + 187) }}
youll seethat it comes to the same value 13206715

try it in dev-tools template

1 Like

in the code above you had:

- platform: template
  lights:
    heater_light:
      friendly_name: "Heater Light"
      unique_id: heater_light
      value_template: "{{ is_state('binary_sensor.heater_nightlight_toggle', 'on') }}"
      level_template: "{{ (states('sensor.heater_brightness') | int * 255 / 100) | int }}"
      availability_template: "{{ is_state('binary_sensor.heater_online', 'on') }}"
      rgb_template: "({{ states('sensor.heater_color_rgb').split(',')[0] | int}} , {{ states('sensor.heater_color_rgb').split(',')[1] | int }}, {{ states('sensor.heater_color_rgb').split(',')[2] | int }})"
      turn_on:
        service: rest_command.set_heater_light_toggle
        data:
          value: 1
      turn_off:
        service: rest_command.set_heater_light_toggle
        data:
          value: 0
      set_level:
        service: rest_command.set_heater_light_brightness
        data:
          value: "{{ (brightness * 100 / 255) | int }}"
      set_rgb:
        service: rest_command.set_heater_light_color_test
        data:
          rgb_color: >
            {{ ((((r * 256 ) + g) * 256) + b) }}

using rgb_color

but in the rest command defintion, you used rgbw_color

set_heater_light_color_test:
  url: "https://openapi.api.govee.com/router/api/v1/device/control"
  method: POST
  headers:
      Content-Type: application/json
      Govee-API-Key: !secret govee_api_key
  payload: '{"requestId": "uuid", "payload": {"sku": "H7131", "device": "7A:67:D4:AD:FC:F5:69:7B", "capability": {"type": "devices.capabilities.color_setting", "instance": "colorRgb", "value": "{{rgbw_color}}"}}}'

they need to be the same. change the rest command definition to be rgb_color

1 Like

Broo, you’re a genius! your formula worked, there were some other small typos, but after fixing those , everything is working.
I cant express how happy I am I spent Days trying to figure. Thank you so much God bless you…

Corrected and working code:

(changelog): now there is only one rest command that does everything instead of having a rest command for individual buttons

- platform: template
  lights:
    heater_light:
      friendly_name: "Heater Light"
      unique_id: heater_light
      value_template: "{{ is_state('binary_sensor.heater_nightlight_toggle', 'on') }}"
      level_template: "{{ (states('sensor.heater_brightness') | int * 255 / 100) | int }}"
      rgb_template: "({{ states('sensor.heater_color_rgb').split(',')[0] | int}} , {{ states('sensor.heater_color_rgb').split(',')[1] | int }}, {{ states('sensor.heater_color_rgb').split(',')[2] | int }})"
      turn_on:
        service: rest_command.set_heater_light
        data:
          capability: "devices.capabilities.on_off"
          instance: "powerSwitch"
          value: 1
      turn_off:
        service: rest_command.set_heater_light
        data:
          capability: "devices.capabilities.on_off"
          instance: "powerSwitch"
          value: 0
      set_level:
        service: rest_command.set_heater_light
        data:
          capability: "devices.capabilities.range"
          instance: "brightness"
          value: "{{ (brightness * 100 / 255) | int }}"
      set_rgb:
        service: rest_command.set_heater_light
        data:
          capability: "devices.capabilities.color_setting"
          instance: "colorRgb"
          value: "{{ ((((r * 256 ) + g) * 256) + b) | int }}"
            


updated rest command:

set_heater_light:
  url: "https://openapi.api.govee.com/router/api/v1/device/control"
  method: POST
  headers:
    Content-Type: application/json
    Govee-API-Key: !secret govee_api_key
  payload: '{"requestId": "uuid", "payload": {"sku": "H7131", "device": "7A:67:D4:AD:FC:F5:69:7B", "capability": {"type": "{{ capability }}", "instance": "{{ instance }}", "value": {{ value }}}}}'


cool! i’m glad you got it working! :slight_smile:

1 Like