Motivation

I have a home full of KNX controlled color temperature tunable devices. I then added my first Tasmota controlled (non KNX) device. Goal was to have the same color on both, KNX and Tasmota controlled devices.

Approaches that didn't suffice

The LED I wanted to set has a range from 2700 to 6500. I tried configuring that in homeassistants YAML.

homeassistant:
  # Customize entities matching a pattern
  customize_glob:
    "light.eg_ez_deckenlicht_*":
      min_color_temp_kelvin: 2700
      max_color_temp_kelvin: 6500

This, however, didn't work. This only limits the ranges of the sliders displayed in homeassistant - even worsening the issue. If a slider was at "max warm" (2700K) in tasmota, this would send a "CT=446" to Tasmota. As the default range is 153-500 this would leave 54 more steps for warmer temperatures.

Setting CTRange on the Tasmota device

According to tasmota docs the command "CTRange" can be used to set the range the connected LED can do. Using Google, I couldn't find the conversion formula. ChatGPT to the rescue:

[ \text{CT} = 500 - \frac{(\text{Kelvin} - 2000) \cdot 347}{4500} ]

So for my two relevant values:

  1. 2700K: [ \text{CT} = 500 - \frac{(2700 - 2000) \cdot 347}{4500} ] [ \text{CT} = 500 - \frac{700 \cdot 347}{4500} = 500 - 54 = 446 ] So, 2700K ≈ CT 446 in Tasmota.

  2. 6500K: [ \text{CT} = 500 - \frac{(6500 - 2000) \cdot 347}{4500} ] [ \text{CT} = 500 - \frac{4500 \cdot 347}{4500} = 500 - 347 = 153 ] So, 6500K ≈ CT 153 in Tasmota.

Summary:

  • 2700K → CT 446 (warm white)
  • 6500K → CT 153 (cool white)

So the command I want to run is

CTRange 153,446

Running this in the Tasmota CLI on the device looked promising:

Running a command on tasmota

And we're done? Not quite, because the Tasmota docu states "This settings is not persisted in flash" - bummer. So we need a way to run this on homeassistant every time the tasmota device was reset.

Using a homeassistant automation to set the range automatically after device reset

ChatGPT came up with the following YAML:

alias: Run CTRange Command on Tasmota Reboot
description: Send CTRange 153,446 to the correct Tasmota device after a reboot.
trigger:
  - platform: mqtt
    topic: "tele/tasmota_03E632/LWT"
    payload: "Online"
  - platform: mqtt
    topic: "tele/tasmota_4F7F66/LWT"
    payload: "Online"
condition: []
action:
  - service: mqtt.publish
    data:
      topic: >
        {% if trigger.topic == "tele/tasmota_03E632/LWT" %}
        cmnd/tasmota_03E632/CTRange
        {% elif trigger.topic == "tele/tasmota_4F7F66/LWT" %}
        cmnd/tasmota_4F7F66/CTRange
        {% endif %}
      payload: "153,446"
mode: single

For some strange reason, the Tasmota console was showing other values when setting 2700K from homeassistant for me: 370 (instead of 446). So I went for that.

Device topics can be retrieved from the devices' mqtt config portal, i.e. http://192.168.178.42/mq? yielding something like tasmota_03E632 or tasmota_4F7F66. After creating the automation, we can verify the script by resetting the devices and running "CTRange" (without parameters) to see whether they were set. And indeed: It worked 🙂!

Be sure to also set the max_.* and min_color_temp_kelvin as shown above.

Key learning

Key learning for me was the "last will testament" (LWT) topic which is sent on connection to the broker perfectly fits as a event for initialization you might want to do.