MQTT Light - Command template missing for HS value

Hello there,

When using MQTT light, there is generally a template option:

  • to extract value (incoming), for instance: rgb_value_template, color_temp_value_template, hs_value_template
  • to format the payload (outgoing): rgb_command_template, color_temp_command_template, …

But unfortunately, formatting the payload for HS (Hue-Saturation) variable is not possible.

It is possible to extract incoming value using hs_value_template.

But I was expecting an equivalent hs_command_template. Not to be found in the documentation, and when adding to the config, Hassio not happy.

Is this config variable yet to be added, or was there a particular constraint?

Thanks.

Can you not convert hs to rgb and/or color temp? I’d assume HA does this in the background and only lets you choose the command for rgb and color temp.

Yes indeed, but would be easier if the hs_command_template was present.

In this particular case, the device is expecting hue/saturation values in JSON:

{ "h": xxx, "s": xxx }

Following your hint, now using RGB options:

  • rgb_value_template: template converts HSV to RGB
  • rgb_command_template: template converts RGB to HSV

How to make conversions using Jinja2? Check out this topic: Using HSV / HSB to set colored lights

Pretty long templates, but they do their job :+1:
In my case, minified the code into a single line.

  - unique_id: board_01_rgb_01
    name: "Board 01 RGB 01"
    availability:
      topic: "board-01/availability/status"
      payload_available: "online"
      payload_not_available: "offline"
    state_topic: "board-01/light/relay_01/state"
    state_value_template: "{{ value_json.state | lower }}"
    command_topic: "board-01/light/relay_01/state"
    payload_on: "on"
    payload_off: "off"
    brightness_state_topic: "zigbee2mqtt/rgb_cw_01_01/set"
    brightness_value_template: "{{ value_json.brightness }}"
    brightness_command_topic: "zigbee2mqtt/rgb_cw_01_01/set"
    brightness_command_template: '{ "brightness": {{ value }} }'
    color_temp_state_topic: "zigbee2mqtt/rgb_cw_01_01/set"
    color_temp_value_template: "{{ value_json.color_temp }}"
    color_temp_command_topic: "zigbee2mqtt/rgb_cw_01_01/set"
    color_temp_command_template: '{ "color_temp": {{ value }} }'
    hs_state_topic: "zigbee2mqtt/rgb_cw_01_01/set"
    hs_value_template: "{{ value_json.color.hue }}, {{ value_json.color.saturation }}"
    hs_command_topic: "zigbee2mqtt/rgb_cw_01_01/set"
    hs_command_template: '{ "color": {"hue": {{ hue }}, "saturation": {{ sat }}} }'
    qos: 0

This is my configuration.
This is a special rgb light. When i push on-off button i send command to mqtt relay.
When i change color, brightness or color temperature, i send command to mqtt light.