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