MQTT Lights and send / receiving byte values

Hello All,
I’m new to HA, but have some experience with MQTT, node-red and writing custom firmware for microcontrollers.

I’ve build custom lights that send and receive MQTT payloads containing status and brightness values in the one message as byte values. The command and status have their own MQTT topics, namely mt/cmd/test0 and mt/stat/test0.

Perhaps oddly, even though they have their own MQTT topic, the messages are also prefixed with command or status codes (see message structure detailed below)

Each message is 4 bytes long and the data format is the following.

  • First two bytes are the message type, ASCII “20” for a command and “21” for a status reply.
  • Third byte is the light state (on / off), represented as 0x00 or 0x01.
  • Fourth byte is the light brightness (0 to 100), represented as 0x00 to 0x64.

I have added the following to my configuration.yaml

# attempt at adding custom light
light:
  - platform: mqtt
    name: "test light"
    encoding: ""
    brightness_scale: 100
    command_topic: "mt/cmd/test0"
    state_topic: "mt/stat/test0"
    state_value_template: '{{value[2]|int}}'
    payload_on: 1
    payload_off: 0
    brightness_state_topic: "mt/stat/test0"
    brightness_value_template: '{{value[3]|int}}'
    brightness_command_topic: "mt/cmd/test0"
    unique_id: mytestlight

The resulting dashboard light card responds correctly to light state and brightness changes (activated externally by node-red dashboard buttons), but when I attempt to change the light state via the card I get the following HA logged error.

> Can't pass-through payload for publishing 1 on mt/cmd/test0 with no encoding set, need 'bytes' got <class 'str'>

I understand I need to modify the value templates to correctly send a message (i.e prefix “20” to the outgoing command etc.), but I’m really not sure how to modify the templates to do what I want / format up the outgoing byte message.

Any help or advise would be most appreciated!

Thanks in advance,

alex

At the risk of sounding like a pain in the a$$, I’d say update your custom firmware to follow “MQTT Standards” (whatever that really means)
If not, can you confirm the message that the topic expects to [say] turn to light on at 50% brightness?

Hello lolouk44,
Thanks for the reply. It is appreciated.

The messages (from my firmware) are coming from a CAN bus that is being bridge into MQTT, hence the desire to keep the messages as small as possible (i.e. no JSON formatting) and limit the number of topics.

So to turn the light to 50% the payload transmitted from HA on topic mt/cmd/test0 would be bytes ‘\0x32’ ‘\0x30’ ‘\0x01’ ‘\0x32’. That breaks down to ASCII 2, ASCII 0 (21) (0x32 0x30) for the cmd prefix, 0x01 for the light status (on) and 0x32 for 50 brightness (i.e. 50%)

The light status message received by HA is basically identical on mt/stat/test0 topic, with the stat prefix of ASCII 2 and ASCII 1 (21), then the light status, and brightness bytes.

It’s like I’m missing a command_value_template that allows me to define the format of the transmitted message. Or am I supposed to be using brightness_value_template? Or is HA just expecting a single value / parameter for each MQTT topic when not using JSON?

An option is to modify / sanitise the CAN to MQTT messages via Node-Red prior to sending to HA, but that adds another link in the chain, and I was encourage by my initial testing and being able to successfully get the HA light card / entity to correctly pickup and display the incoming stat messages with the yaml config I posted previously. I really thought with all the flexibility of templates and HA processing, this would have been possible, i.e. assign byte 0 ‘2’, byte 1 ‘0’, byte 2 to light state, and byte 3 to light brightness and transmit.

If you have any thoughts I’d be most grateful.

Thanks again,

alex

Well… I ended up doing a simple translation of the MQTT messages in node-red. It took about 20 lines of JavaScript and I now have a working light entity in HA. Not my ideal solution, but I’ve burnt enough time on this one today!

I’m only a newbie to HA, but the whole yaml file processing doesn’t seem overly intuitive to me! :yum:

Cheers,

alex