Splitting mqtt in separate sensors

I receive these 3 (6) sensors on the same topic :



QoS: 0 - Retain: false
Message 2 received on home/OpenMQTTGateway2/PilighttoMQTT at 18:50:

{
    "message": {
        "id": 162,
        "channel": 1,
        "battery": 1,
        "temperature": 19.3,
        "humidity": 79
    },
    "protocol": "nexus",
    "length": "162",
    "value": "162",
    "repeats": 2,
    "status": 2
}

QoS: 0 - Retain: false
Message 1 received on home/OpenMQTTGateway2/PilighttoMQTT at 18:50:

{
    "message": {
        "id": 69,
        "channel": 2,
        "battery": 1,
        "temperature": 23.1,
        "humidity": 56
    },
    "protocol": "nexus",
    "length": "69",
    "value": "69",
    "repeats": 2,
    "status": 2
}

QoS: 0 - Retain: false
Message 0 received on home/OpenMQTTGateway2/PilighttoMQTT at 18:50:

{
    "message": {
        "id": 228,
        "channel": 1,
        "battery": 0,
        "temperature": 26.9,
        "humidity": 56
    },
    "protocol": "nexus",
    "length": "228",
    "value": "228",
    "repeats": 2,
    "status": 2
}

What is the best way to split them into 6 different sensors ?

I suggest using a demultiplexer automation similar to the one I posted here.

- alias: 'mqttsensor demultiplexer'
  trigger:
  - platform: mqtt
    topic: home/OpenMQTTGateway2/PilighttoMQTT
  action:
  - service: mqtt.publish
    data:
      topic: "omg2/{{ trigger.payload_json.message.id }}/temperature"
      payload: "{{ trigger.payload_json.message.temperature}}"
  - service: mqtt.publish
    data:
      topic: "omg2/{{ trigger.payload_json.message.id }}/humidity"
      payload: "{{ trigger.payload_json.message.humidity}}"

Then define the six sensors:

 - platform: mqtt
    name: "Temperature 162"
    state_topic: omg2/162/temperature
    device_class: temperature
 - platform: mqtt
    name: "Humidity 162"
    state_topic: omg2/162/humidity
    device_class: humidity

... etc ...


2 Likes

As usual, a working solution from you :slight_smile:

1 Like

Sorry to hijack but seems appropriate - thanks for the ideas @123 , I think this is what I too need! However I’m not having much luck using this or the other example in a similar manner for my data - obviously user error…

This is my single MQTT message, coming in over inverter/stats , hope you can assist and adjust the above for my scenario perhaps please?

{
  "daily_power_yield": 47600,
  "total_power_yield": 12.25,
  "internal_temp": 46.9,
  "pv1_voltage": 101.2,
  "pv1_current": 0,
  "pv2_voltage": 101.5,
  "pv2_current": 0,
  "total_pv_power": 0,
  "grid_voltage": 234.3,
  "inverter_current": 0,
  "total_active_power": 0,
  "grid_frequency": 50,
  "export_power": 905,
  "export_power_indicator": 0,
  "power_meter": 905,
  "daily_purchased_energy": 2.9,
  "daily_energy_consumption": 11500,
  "total_energy_consumption": 376.4,
  "year": 2021,
  "month": 9,
  "day": 10,
  "hour": 18,
  "minute": 5,
  "second": 26,
  "timestamp": "10/9/2021 18:05:26"
}

I managed to make this message: solar/daily_power_yield = 47600.00 using the below automation, but I’m not sure how to do this to the other messages without creating individual automations. Appreciate any advice :slight_smile:

  alias: 'MQTT DEMULTIPLEXER '
  description: For inverter
  trigger:
  - platform: mqtt
    topic: inverter/stats
  condition: []
  action:
  - service: mqtt.publish
    data:
      topic: solar/daily_power_yield
      payload: '{{ trigger.payload_json.daily_power_yield }}'
      retain: true
  mode: parallel
  max: 3

What is your goal because the payload you posted doesn’t appear to be multiplexed. In other words, the payload published to the topic inverter/stats represents data from a single device (and the topic isn’t re-used for data from other devices).

Is your goal to create a separate sensor to represent each field (if not all fields then several of them)?

I assume you are aware that a single MQTT Sensor can store received data as attributes?

json_attributes_topic

Thanks very much for the reply - yes, the intention is to create a seperate sensor for each field (at least the more useful ones as you suggested). I wasn’t aware of the MQTT attributes behaviour, but the more I look at the dozen or so other devices I have on MQTT, I can see this happening, so perhaps I am looking for a solution the wrong way? I think I’m spoilt because the other devices all had discovery most likely available.

I haven’t had any experience with json as it seemed confusing but I will certainly have a look and see if I can structure something to work with this based on your link.

I ended up getting this working - it wasn’t necessary as you mentioned to demux, just needed to understand how to create sensors via json, so thanks for the heads up.

This link shows how I did it for various sensors if helpful for anyone else: