dvdgeisler / DirigeraClient

Dirigera Client API: Java written client API to interface IKEA's new smarthome hub DIRIGERA
MIT License
76 stars 11 forks source link

Colour Temperature issues #33

Closed daveabbott007 closed 1 year ago

daveabbott007 commented 1 year ago

Firstly Dvdgeisler - you rock!! thank you for all your hard work on this api.

I am using the MQTT > HomeAssistant api

is there a know CT issue, i keep getting the below error when trying to change the ColourTemperature of any CT bulb, everything else (Brightness - on/of etc) works perfectly

Could this be fallout from the recent shift to HA Frontend switching to using KELVIN whilst Ikea's backend is using MIREDS?

2022-11-24 14:37:26.821 DEBUG 64 --- [-355b36306149_1] d.d.i.d.c.mqtt.hass.HassEventHandler : Received state command: {"state":"ON","color_temp":2202} 2022-11-24 14:37:26.888 ERROR 64 --- [-355b36306149_1] d.d.i.d.c.mqtt.hass.HassEventHandler : Light Temperature must be between 2202 and 4000

dvdgeisler commented 1 year ago

Hi daveabbott007,

Thx for reporting this. Honestly, i was a bit lost when i was implementing it. HA shows the temperature in Kelvin (at least in the version i have running). However, the MQTT light module reported the temperature in Mireds. So indeed, whatever temperature comes over the MQTT, i convert it to Kelvin after checking its bounds . The relevant code is located in LightEventHandler.java:

Optional.ofNullable(status.color_temp)
                .filter(t -> canReceive(device, "colorTemperature"))
                .map(t -> max(t, getMaxTemperatureMireds(device))) // todo: getMaxTemperatureMireds may return null
                .map(t -> min(t, getMinTemperatureMireds(device))) // todo: getMinTemperatureMireds may return null
                .map(LightUtils::miredsToKelvin)
                .map(t -> api.device.light.setTemperature(device, t))
                .ifPresent(Mono::block);

So yes, I think your assumption is pointing in the right direction. Could you share the actual MQTT messages? image

dvdgeisler commented 1 year ago

While looking at this snippet, i think the bounds checking is broken as well... Maybe im completely confused now, but i think, in contrast to kelvin, larger Mireds corresponds to a higher temperature. Hence, min/max is swapped.

daveabbott007 commented 1 year ago

here is the MQTT log for turning on the light, changing the CT then turning the light off again

2022-11-24 15:33:07.858 DEBUG 64 --- [or-http-epoll-3] d.d.iot.dirigera.client.mqtt.MqttBridge : Publish message to MQTT: topic=homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/state, payload={"state":"ON","brightness":50,"color_mode":"color_temp","color_temp":418,"color":{}}, factory=LightEventHandler, deviceId=7e69e364-17b6-4c15-9c18-1835d0e44d87_1, deviceType=LIGHT, deviceCategory=LIGHT 2022-11-24 15:33:10.659 DEBUG 64 --- [-355b36306149_1] d.d.i.d.c.mqtt.hass.HassEventHandler : Received state command: {"state":"ON","color_temp":2392} 2022-11-24 15:33:10.717 ERROR 64 --- [-355b36306149_1] d.d.i.d.c.mqtt.hass.HassEventHandler : Light Temperature must be between 2202 and 4000 2022-11-24 15:33:11.515 DEBUG 64 --- [-355b36306149_1] d.d.i.d.c.mqtt.hass.HassEventHandler : Received state command: {"state":"ON","color_temp":2392} 2022-11-24 15:33:11.585 ERROR 64 --- [-355b36306149_1] d.d.i.d.c.mqtt.hass.HassEventHandler : Light Temperature must be between 2202 and 4000 2022-11-24 15:33:17.318 DEBUG 64 --- [-355b36306149_1] d.d.i.d.c.mqtt.hass.HassEventHandler : Received state command: {"state":"OFF"} 2022-11-24 15:33:17.576 DEBUG 64 --- [or-http-epoll-4] d.d.iot.dirigera.client.mqtt.MqttBridge : Publish message to MQTT: topic=homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/state, payload={"state":"OFF","brightness":50,"color_mode":"color_temp","color_temp":418,"color":{}}, factory=LightEventHandler, deviceId=7e69e364-17b6-4c15-9c18-1835d0e44d87_1, deviceType=LIGHT, deviceCategory=LIGHT

Here is the diagnostic.json file

{ "home_assistant": { "installation_type": "Home Assistant Supervised", "version": "2022.11.4", "dev": false, "hassio": true, "virtualenv": false, "python_version": "3.10.7", "docker": true, "arch": "x86_64", "timezone": "Europe/London", "os_name": "Linux", "os_version": "5.16.10-051610-generic", "supervisor": "2022.11.2", "host_os": "Ubuntu 22.04.1 LTS", "docker_version": "20.10.21", "chassis": "desktop", "run_as_root": true },

"device": {
  "id": "fa998640cafaef0f8f9ab2dac7ae786c",
  "name": "Bathroom light",
  "name_by_user": null,
  "disabled": false,
  "disabled_by": null,
  "entities": [
    {
      "device_class": null,
      "disabled_by": null,
      "disabled": false,
      "entity_category": null,
      "entity_id": "light.7e69e364_17b6_4c15_9c18_1835d0e44d87_1",
      "icon": null,
      "original_device_class": null,
      "original_icon": null,
      "state": {
        "entity_id": "light.7e69e364_17b6_4c15_9c18_1835d0e44d87_1",
        "state": "off",
        "attributes": {
          "min_color_temp_kelvin": 2202,
          "max_color_temp_kelvin": 418,
          "min_mireds": 2392,
          "max_mireds": 454,
          "supported_color_modes": [
            "color_temp"
          ],
          "friendly_name": "Bathroom light",
          "supported_features": 40
        },
        "last_changed": "2022-11-24T15:33:17.583193+00:00",
        "last_updated": "2022-11-24T15:33:17.583193+00:00"
      },
      "unit_of_measurement": null
    }
  ]
},
"mqtt_debug_info": {
  "entities": [
    {
      "entity_id": "light.7e69e364_17b6_4c15_9c18_1835d0e44d87_1",
      "subscriptions": [
        {
          "topic": "homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/availability",
          "messages": [
            {
              "payload": "\"ONLINE\"",
              "qos": 0,
              "retain": 0,
              "time": "2022-11-24T14:30:14.081005+00:00",
              "topic": "homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/availability"
            }
          ]
        },
        {
          "topic": "homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/state",
          "messages": [
            {
              "payload": "{\"state\":\"OFF\",\"brightness\":50,\"color_mode\":\"color_temp\",\"color_temp\":418,\"color\":{}}",
              "qos": 0,
              "retain": 0,
              "time": "2022-11-24T14:30:14.075820+00:00",
              "topic": "homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/state"
            },
            {
              "payload": "{\"state\":\"ON\",\"brightness\":50,\"color_mode\":\"color_temp\",\"color_temp\":418,\"color\":{}}",
              "qos": 0,
              "retain": 0,
              "time": "2022-11-24T15:07:31.120649+00:00",
              "topic": "homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/state"
            },
            {
              "payload": "{\"state\":\"OFF\",\"brightness\":50,\"color_mode\":\"color_temp\",\"color_temp\":418,\"color\":{}}",
              "qos": 0,
              "retain": 0,
              "time": "2022-11-24T15:13:33.972027+00:00",
              "topic": "homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/state"
            },
            {
              "payload": "{\"state\":\"ON\",\"brightness\":50,\"color_mode\":\"color_temp\",\"color_temp\":418,\"color\":{}}",
              "qos": 0,
              "retain": 0,
              "time": "2022-11-24T15:32:57.613827+00:00",
              "topic": "homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/state"
            },
            {
              "payload": "{\"state\":\"OFF\",\"brightness\":50,\"color_mode\":\"color_temp\",\"color_temp\":418,\"color\":{}}",
              "qos": 0,
              "retain": 0,
              "time": "2022-11-24T15:33:00.230375+00:00",
              "topic": "homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/state"
            },
            {
              "payload": "{\"state\":\"ON\",\"brightness\":50,\"color_mode\":\"color_temp\",\"color_temp\":418,\"color\":{}}",
              "qos": 0,
              "retain": 0,
              "time": "2022-11-24T15:33:07.864313+00:00",
              "topic": "homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/state"
            },
            {
              "payload": "{\"state\":\"OFF\",\"brightness\":50,\"color_mode\":\"color_temp\",\"color_temp\":418,\"color\":{}}",
              "qos": 0,
              "retain": 0,
              "time": "2022-11-24T15:33:17.583034+00:00",
              "topic": "homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/state"
            }
          ]
        }
      ],
      "discovery_data": {
        "topic": "homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/config",
        "payload": {
          "object_id": "7e69e364-17b6-4c15-9c18-1835d0e44d87_1",
          "unique_id": "7e69e364-17b6-4c15-9c18-1835d0e44d87_1",
          "name": "Bathroom light",
          "device": {
            "identifiers": [
              "000D6FFFFE12EC29"
            ],
            "manufacturer": "IKEA of Sweden",
            "model": "LEPTITER Recessed spot light",
            "hw_version": "1",
            "sw_version": "2.3.087",
            "name": "Bathroom light",
            "via_device": "d705fc32-eea6-4dde-8f06-355b36306149_1"
          },
          "brightness": true,
          "brightness_scale": 100,
          "color_mode": true,
          "supported_color_modes": [
            "color_temp"
          ],
          "command_topic": "homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/set",
          "state_topic": "homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/state",
          "schema": "json",
          "max_mireds": 454,
          "min_mireds": 2392,
          "availability": {
            "topic": "homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/availability",
            "payload_available": "\"ONLINE\"",
            "payload_not_available": "\"OFFLINE\""
          },
          "platform": "mqtt"
        }
      },
      "transmitted": [
        {
          "topic": "homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/set",
          "messages": [
            {
              "payload": "{\"state\":\"ON\"}",
              "qos": 0,
              "retain": false,
              "time": "2022-11-24T15:32:57.270366+00:00",
              "topic": "homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/set"
            },
            {
              "payload": "{\"state\":\"OFF\"}",
              "qos": 0,
              "retain": false,
              "time": "2022-11-24T15:32:59.960817+00:00",
              "topic": "homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/set"
            },
            {
              "payload": "{\"state\":\"ON\"}",
              "qos": 0,
              "retain": false,
              "time": "2022-11-24T15:33:07.599452+00:00",
              "topic": "homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/set"
            },
            {
              "payload": "{\"state\":\"ON\",\"color_temp\":2392}",
              "qos": 0,
              "retain": false,
              "time": "2022-11-24T15:33:10.656834+00:00",
              "topic": "homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/set"
            },
            {
              "payload": "{\"state\":\"ON\",\"color_temp\":2392}",
              "qos": 0,
              "retain": false,
              "time": "2022-11-24T15:33:11.514241+00:00",
              "topic": "homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/set"
            },
            {
              "payload": "{\"state\":\"OFF\"}",
              "qos": 0,
              "retain": false,
              "time": "2022-11-24T15:33:17.316623+00:00",
              "topic": "homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/set"
            }
          ]
        }
      ]
    }
  ],
  "triggers": []
}

} }

When i was digging around for something else i came across this, Updated proposal 2022 which goes on about mireds and kelvin. i just wish i understood it

dvdgeisler commented 1 year ago

{ "payload": "{"state":"ON","color_temp":2392}", "qos": 0, "retain": false, "time": "2022-11-24T15:33:10.656834+00:00", "topic": "homeassistant/light/d705fc32-eea6-4dde-8f06-355b36306149_1/7e69e364-17b6-4c15-9c18-1835d0e44d87_1/set" },

Looks like your HA talks in Kelvin via MQTT

TheMrBooyah commented 1 year ago

I believe HA switched to Kelvin by default. So everything related to Colour temperature within HA is in Kelvin.

I believe the MQTT client needs to convert from Kelvin to Mired when MQTT -> API and Mired to Kelvin when API -> MQTT

dvdgeisler commented 1 year ago

It's exactly as TheMrBooyah wrote. The error was basically in calculating the correct limits in Mireds. I already merged the fix into the main branch. Could you test if it now works as expected?

daveabbott007 commented 1 year ago

Tested and all CT bulbs working perfectly!! Thank you so much, i can now move from the broken Ikea homekit (CT does not work there) setup.

Again, great work!!

please add the "Buy me a Beer/Coffee" button