MQTT - Power Meter - Extract value for each sensor in same topic messages

Hello everybody!

I have a power meter that has MQTT and my Mosquitto are receiving messages from this power meter on the same topic(so each line below is a message that comes on the same topic), how do I extract each value of these messages to a corresponding sensor via templates?

{“variable”:“EP”, “value”:3283.88,“unit”:“Wh”}
{“variable”:“I”, “value”:3.81,“unit”:“A”}
{“variable”:“PF”, “value”:0.94,“unit”:“1”}
{“variable”:“U”, “value”:220.00,“unit”:“V”}
{“variable”:“P”, “value”:787.90,“unit”:“W”}

I helped someone else with a similar question:

We can adapt it to meet your requirements. For example, here’s an MQTT Sensor for “EP”:

  - platform: mqtt
    name: EP Sensor
    state_topic: tele/energy/RESULT
    value_template: >-
      {% if value_json.variable == 'EP' %}
        {{ value_json.value }}
      {% else %}
        {{ states('sensor.ep_sensor') }}
      {% endif %}
    expire_after: 120
    device_class: energy
    unit_of_measurement: Wh
2 Likes

Wow, that worked really nice.

Only removed device_class as I got an error with Yaml.

Anyway you really solved it!!!

Now got the values and start making the interface…appears ugly, but will improve later.

1 Like