Custom MQTT device with multiple payload values

Hello,
I am integrating a Casambi Lithernet gateway and have my commands working so far manually with MQTT Explorer.

I would like to control groups and scenes from HA directly.
The MQTT command looks like :

  • for groups:
    casambi/0/set/group_level
    with payload {
    “group”:1,
    “duration”:2,
    “level”:120
    }
  • for scenes:
    casambi/0/set/scene_level
    with payload {
    “level”:165,
    “scene”:2,
    “duration”:2
    }

What is the easiest way to create a custom device?
I will have multiple groups and scenes and would like to set the duration and level for each group/scene as well.

Is this doable with a MQTT switch? or is it limited to 1 fix payload only?

Thanks for your help

I made some progress.

2 groups are now partially working using 2 MQTT Discovery packets to create each in the same device.
I can now send On / Off commands (level = 0 or 254).

However I can’t adjust the brightness as it always send back a frame with a level=254 whatever the slider value. How to solve this ?

Packet sent on topic “homeassistant/light/casambi_group_1/config”:

{
  "name": "Casambi Group 1",
  "unique_id": "casambi_group_1",
  "command_topic": "casambi/0/set/group_level",
  "state_topic": "casambi/0/get/poll_group/1",
  "brightness_command_topic": "casambi/0/set/group_level",
  "brightness_state_topic": "casambi/0/get/poll_group/1",
  "payload_on": "{\"group\": 1, \"level\": 254, \"duration\": 50}",
  "payload_off": "{\"group\": 1, \"level\": 0, \"duration\": 50}",
  "brightness_scale": 254,
  "json_attributes_topic": "casambi/0/get/poll_group/1",
  "device": {
    "identifiers": ["casambi_gateway_0"],
    "name": "Casambi Gateway 0",
    "manufacturer": "Casambi",
    "model": "Casambi MQTT Gateway",
    "sw_version": "4.35"
  }
}

(same for Group 2)

I finally have it working !!

Only drawback, I have to use the device status as the group status is updated only each 30s.
But it finally works as intended !!

Here is my solution:

{
  "name": "Casambi Group 1",
  "unique_id": "casambi_group_1",
  "on_command_type": "brightness",
  "command_topic": "casambi/0/set/group_level",
  "payload_off": "{\"group\": 1, \"level\": 0, \"duration\": 50}",
  "state_topic": "casambi/0/get/poll_device/11/values",
  "state_value_template": "{{ 'ON' if value_json.level > 0 else '{\"group\": 1, \"level\": 0, \"duration\": 50}' }}",
  "brightness_command_topic": "casambi/0/set/group_level",
  "brightness_command_template": "{\"group\": 1, \"level\": {{ value }}, \"duration\": 50}",
  "brightness_state_topic": "casambi/0/get/poll_device/11/values",
  "brightness_value_template": "{{ value_json.level }}",
  "brightness_scale": 254,
  "json_attributes_topic": "casambi/0/get/poll_device/11/values",
  "device": {
    "identifiers": ["casambi_gateway_0"],
    "name": "Casambi Gateway 0",
    "manufacturer": "Casambi",
    "model": "Casambi MQTT Gateway",
    "sw_version": "4.35"
  }
}

EDIT: Lithernet also support direct MQTT discovery using the latest dev firmware. Only drawback is that the group and schene feedback is very slow due to Casambi firmware. Hence using one of the light as feedback as described here is much more responsive.