Split payload to state and attributes for MQTT sensors

Hi,
I am trying to find a solution for the problem stated in the title. I have a custom MQTT sensor that receives several data as plain bytes, divided by ‘,’ in the payload.
Subscription and setting the state works just fine.

The payload contains both data for the sensors “state” as well as other attributes. The problem is, that I can’t set attributes as described in the docs, because the payload is not in common json-format.

Lets say this is a message I receive:

home/co_gateway/sdo/581/67,32,2,1,31,47,214,123

The payload is eight numbers (bytes), starting from 67 and ending at 123.
The meaning of these bytes is as follows:

  • byte 0: marker about how many bytes of status data will be there
  • byte [1-2]: object number
  • byte 3: sub-object number
  • byte [4-7]: status data

Here is my current sensor config:

- platform: mqtt
  name: "SDO 1"
  state_topic: "home/co_gateway/sdo/581/"
  value_template:  >-
    {% if value.split(',')[0]|int == 67 %}
      {{ ((value.split(',')[4]|int + value.split(',')[5]|int + value.split(',')[6]|int + value.split(',')[7]|int)|int) }}
    {% elif value.split(',')[0]|int == 71%}
      {{ ((value.split(',')[4]|int + value.split(',')[5]|int + value.split(',')[6]|int)|int) }}
    {% elif value.split(',')[0]|int == 75%}
      {{ ((value.split(',')[4]|int + value.split(',')[5]|int)|int) }}
    {% elif value.split(',')[0]|int == 79%}
      {{ ((value.split(',')[4]|int)|int) }}
    {% else %}
      {{ states('sensor.sdo_1') }}
    {% endif %}

As you can see I’ve found a way to use byte 0 to handle the actual status data (byte [4-7]) and use it as the sensors state.

Now I like to set two sensor attributes according to the other bytes:
Sensor attribute “Object” = byte 1 + byte 2
Sensor attribute “Sub Object” = byte 3

Is there a way to do this? Or that other solutions would you propose?

Thanks a ton!
Greetings,
Jojo

Hi,
I’ve not been lazy in the meantime. So I figured out a solution that seems to work for me :slight_smile: :

- platform: mqtt
  name: "SDO 1"
  state_topic: "home/co_gateway/sdo/581/"
  value_template:  >-
    {% if value.split(',')[0]|int == 67 %}
      {{ ((value.split(',')[4]|int + value.split(',')[5]|int + value.split(',')[6]|int + value.split(',')[7]|int)|int) }}
    {% elif value.split(',')[0]|int == 71%}
      {{ ((value.split(',')[4]|int + value.split(',')[5]|int + value.split(',')[6]|int)|int) }}
    {% elif value.split(',')[0]|int == 75%}
      {{ ((value.split(',')[4]|int + value.split(',')[5]|int)|int) }}
    {% elif value.split(',')[0]|int == 79%}
      {{ ((value.split(',')[4]|int)|int) }}
    {% else %}
      {{ states('sensor.sdo_1') }}
    {% endif %}
  json_attributes_topic: "home/co_gateway/sdo/581/"
  json_attributes_template: >-
    { "object": {{value.split(',')[1]|int + value.split(',')[2]|int}},
      "sub_object": {{value.split(',')[3]|int}} }

As expected you can see that I used the “json_attributes_topic” and “json_attributes_template” parameters.
I just did not know that I can freely define custom attributes by… well… just naming them.

I am really not an expert in this. But as I understand it I indeed used the json parameters, but just not making use of any json-parsing or so. Only plain python.

Maybe this helps anyone. Any feedback is welcome :slight_smile: .

Greetings,
Jojo

1 Like