Multiple non json values in one MQTT topic

I have a fairly old heat pump that every minute over a rs232 sends a raw text string containing multiple tab separated values. The first value is a time stamp, the rest is a mix of temperature, watts, Hz, binary values, etc.

   6:12:06	 20,0	 50,0	 1,888E+01	 5,052E+01	-1,437E+01	 3,622E+01	10100	11001	 75,0	  75	    8	0	 0,000E-00	 2,892E+03	 5,881E+03	 4,110E+01	 4999	16,69	 5,400E+00	 3,06	   75	 6835	 2000	    0	 3,000E+05	 3,200E+07	 4,000E+07	-4,687E+02	-3,750E+02	-7,812E-02	 1,500E+03	 0,000E-00	 8,880E+02	 9,539E+02	 0,00	 0,00	 0,000E-00	    0	    0	    0	    0	    0	    0    0

Data is sent via a device converting to MQTT and in one Topic forwarding it to the broker. I can see the message arrive in MQTT so the connection between the heat pump and HA MQTT broker works. After that I have two issues.

#1. The data arrives divided up into 4 messages with the same Topic ”Heatpump”. It seems that the heat pump every minute sends the data in four bursts with a slight delay (milliseconds) but enough to have MQTT divide the data up. In HA MQTT it looks like this.


Message 0 received on Heatpump at 21:01:
   6:12:06	 20,0	 50,0	 1,888E+01	 5,052E+01	-1,437E+01	 3,622E+01	10100	11001	 75,0	  75	    8	0
Message 1 received on Heatpump at 21:01:
0,000E-00	 2,892E+03	 5,881E+03	 4,110E+01	 4999	16,69	 5,400E+00	 3,06	   75	 6835	 2000	    0
Message 2 received on Heatpump at 21:01:
3,000E+05	 3,200E+07	 4,000E+07	-4,687E+02	-3,750E+02	-7,812E-02	1,500E+03	 0,000E-00	 8,880E+02	 9,539E+02	 0,00	 0,00
Message 3 received on Heatpump at 21:01:
0,000E-00	    0	    0	    0	    0	    0	    0    0

I am only interested in the first message with the time stamp. The other three can be discarded, however this can’t be done on the client side.

#2. I have tried to extract values but can’t get them into Sensors. I have tried the approach below based on advice found in this forum.

- name: "Heater room temp setting”"
  unique_id: Room_temp_setting
  state_topic: "Heatpump"
  unit_of_measurement: "°C"
  value_template: "{{ trigger.payload[12:-1].split('\t')[0] | float(0) }}"

- name: "Heater tap water temp setting"
  unique_id: Tap_water_temp_setting
  state_topic: "Heatpump"
  unit_of_measurement: "°C"
  value_template: "{{ trigger.payload[12:-1].split('\t')[1] | float(0) }}"
etc.

I get “Unknown” when I try to view the values.

I believe that the different content in the four messages for the same Topic confuses things up.

Any suggestions on how to approach this are greatly appreciated.

Thank’s/TC

The variable value contains your received payload. There is no trigger.payload for mqtt sensors. So this

value_template: "{{ value.split('\t')[0] }}"

Gives you the first value ( 6:12:06 ) and this gives you the second value (20.0):

value_template: "{{ value.split('\t')[1]|replace(',','.')|replace(' ','') }}"

Things to note:

I’m pretty sure the backend HA needs numbers with a . radix. You can use your profile setting to translate this back to , for display in the frontend. Hence the first replace().

Your data is separated with a tab and a space. Hence the second replace().

You do not need to use the float filter in the mqtt sensor template. Only when using the state value produced by this sensor if performing math functions on it.

You can use the Developer Tools → Template editor to test this, but you must assign the value variable with your data first (the mqtt sensor does this automatically, the template editor does not).

Also I just realised you have three different messages on the topic. This complicates things. You will need to test for the first value being a time. This should do (gets first value after the time):

value_template: >
  {% if ':' in value.split('\t')[0] %}
    {{ value.split('\t')[1]|replace(',','.')|replace(' ','') }}
  {% else %}
    {{ this.state }}
  {% endif %}

This keeps the last valid value if one of the other messages without a time are received on the topic.

Just tested, good stuff, works like a charm :smiley:

Thanks’ for quick response, appreciated!

A final touch-up would be to calculate the total effect currently produced. The unit concists of a variable speed heat pump and three immersion heaters at 2kW each.

The formula is: Pump Hz* 67W + # of active Immersion heaters * 2kW

The trick is to extract the number of active Immersion heaters. The information is in the binary string 10100 above (tab position #7). Bit one to three (starting LSB) indicate if the respective immersion heater is on. In the sample string above 10100 indicate that this was a sub zero day where the pump ran at max effect 5kW (75 Hz, tab position 9) + one immersion heater at 2kW = 7 kW.

Since it was 30 years since i did active programming :wink: I attacked this by a brute force method. Masking each immersion heater 00100, 00010, 00001 etc. and then trying to shift the bit as the LSB and finally multiply by 2000W, then do the same for the other two heaters and add up the whole together with the heat pump Hz*67W. However I can’t figure out how to do this type of manipulation and get the typing correct. At least that’s what the faults I get indicate.

Is there a better way to approach this calculation?

BR/TC

See if this helps:

If not, post what you have tried here.

Should be something like this (untested):

value_template: >
  {% if ':' in value.split('\t')[0] %}
    {% set n_heaters = (value.split('\t')[7]|replace(' ','')|string)[-3:].count('1')|int %}
    {% set hp_hz = value.split('\t')[9]|replace(',','.')|replace(' ','')|int %}
    {{ n_heaters * 2000 + hp_hz * 67 }}
  {% else %}
    {{ this.state }}
  {% endif %}

Excellent, thank’s/TC