Some background
I'm trying to integrate a Zigbee device into the MSR using zigbee2mqtt bridge and MQTTController. The device in question is a cheap mood light that has following properties that I'd like to control:
- switch (on/off)
- brightness
- color
I'v already managed to get the switch part working and can toggle the light on/off. Also the brightness value is mapped back to MSR. In zigbee2mqtt it has a value range from 0 to 254, so this the reason for the expression:
expr: 'payload.brightness / 254'
Here's the entity definition (don't know whether the type should be something else than the Switch)
zigbee-lidl-mood-light:
name: 'Lidl Mood Light'
friendly_name: 'Mood Light'
type: Switch
uses_template: lidl-moodlight
And the corresponding template (NOTE: rgb_color has not been defined in this example):
lidl-moodlight:
init: "zigbee2mqtt/%friendly_name%/get/state"
query: "zigbee2mqtt/%friendly_name%/get/state"
capabilities:
- power_switch
- toggle
- dimming
primary_attribute: power_switch.state
events:
"zigbee2mqtt/%friendly_name%":
"power_switch.state":
json_payload: true
expr: 'upper(payload.state) == "ON"'
"dimming.level":
json_payload: true
expr: 'payload.brightness / 254'
actions:
power_switch:
"on":
topic: "zigbee2mqtt/%friendly_name%/set/state"
payload: 'ON'
"off":
topic: "zigbee2mqtt/%friendly_name%/set/state"
payload: 'OFF'
set:
topic: "zigbee2mqtt/%friendly_name%/set/state"
payload:
expr: "parameters.state ? 'ON' : 'OFF'"
type: raw
toggle:
topic: "zigbee2mqtt/%friendly_name%/set/state"
payload: 'TOGGLE'
The problem
In order to control the brightness or the RGB color values, I would have send a JSON payload in corresponding actions. But I have no idea how to define it in the template. The reason why the switch part is working is that the zigbee2mqtt accepts also plain ON / OFF / TOGGLE string payloads in that case.
But the brightness should be controlled with the following payload:
{"brightness": 196}
And the RGB color like:
{"color":{"rgb":"46,102,150"}}
Here's the link for the documentation (the Exposes part defines the messages).
So how should I define the JSON payload for example for the dimming action? It definitely should be some sort of expressions since I have to map the MSR real value (0...1) to (0...254) for the zigbee2mqtt.
actions:
dimming:
set:
topic: "zigbee2mqtt/%friendly_name%/set"
payload:
expr: ?????
type: json
Another problem is the RGB value. I could use the rgb_color capability for the setting but the problem is that the zigbee2mqtt only reports the current color in hue/saturation or xy coordinates.
Here's an example of published message after setting the color:
Topic: zigbee2mqtt/Mood Light QoS: 0
{
"brightness":254,
"color":{
"hue":240,
"saturation":100,
"x":0.1355,
"y":0.0399
},
"color_mode":"xy",
"color_temp":574,
"linkquality":96,
"state":"ON"
}
I would have to map those values back to RGB, but is it even possible with existing constructs in MQTTController's templates?
Help would be appreciated @toggledbits
br,
mgvra