Adjusting template to obtain value from only one kind of MQTT message and ignore others

Hi, I’d like to kindly ask for some help with templates.

I’ve created an MQTT sensor to obtain Total Active Power data from MQTT messages passed down by an Energy Meter device, using value_template:

value_template: '{{ value_json.params["emdata:0"].total_act }}'

It does the job, but floods logs with error messages like these every second:

ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'dict object' has no attribute 'emdata:0' when rendering '{{ value_json.params["emdata:0"].total_act }}'

The reason is that there are three types of MQTT messages published by the device:

  1. Live data reported every second:
{
    "src": "shellypro3em-ec626089191c",
    "dst": "shellypro3em-ec626089191c/events",
    "method": "NotifyStatus",
    "params": {
        "ts": 1681768676.6,
        "em:0": {
            "id": 0,
            "a_act_power": 79.5,
            "a_aprt_power": 158.8,
            "a_current": 0.692,
            "a_pf": -0.66,
            "a_voltage": 229.7,
            "b_act_power": 27.2,
            "b_aprt_power": 125.6,
            "b_current": 0.542,
            "b_pf": -0.56,
            "b_voltage": 231.6,
            "c_act_power": 279,
            "c_aprt_power": 409,
            "c_current": 1.746,
            "c_pf": -0.76,
            "c_voltage": 234.5,
            "n_current": null,
            "total_act_power": 385.608,
            "total_aprt_power": 693.411,
            "total_current": 2.981
        }
    }
}
  1. Notification data reported every 60 seconds:
{
    "src": "shellypro3em-ec626089191c",
    "dst": "shellypro3em-ec626089191c/events",
    "method": "NotifyEvent",
    "params": {
        "ts": 1681768620.74,
        "events": [
            {
                "component": "emdata:0",
                "id": 0,
                "event": "data",
                "ts": 1681768560,
                "data": {
                    "ts": 1681768560,
                    "period": 60,
                    "values": [
                        [
                            1.2853,
                            1.3073,
                            0,
                            0,
                            0,
                            1.9402,
                            79,
                            75.7,
                            158.5,
                            155.6,
                            230.319,
                            229.546,
                            230.008,
                            0.689,
                            0.677,
                            0.682,
                            0.4456,
                            0.45,
                            0,
                            0,
                            0,
                            1.8687,
                            27.7,
                            25.8,
                            124.7,
                            122.5,
                            230.206,
                            228.962,
                            229.65,
                            0.542,
                            0.535,
                            0.538,
                            4.6027,
                            4.6403,
                            0,
                            0,
                            0,
                            3.68,
                            281.7,
                            273,
                            408.8,
                            401.5,
                            234.054,
                            232.952,
                            233.657,
                            1.75,
                            1.72,
                            1.731,
                            0.028,
                            0.027,
                            0.027
                        ]
                    ]
                }
            }
        ]
    }
}
  1. Cumulative energy summary data reported every 60 seconds:
{
    "src": "shellypro3em-ec626089191c",
    "dst": "shellypro3em-ec626089191c/events",
    "method": "NotifyStatus",
    "params": {
        "ts": 1681768620.74,
        "emdata:0": {
            "id": 0,
            "a_total_act_energy": 244420.52,
            "a_total_act_ret_energy": 286691.18,
            "b_total_act_energy": 154730.79,
            "b_total_act_ret_energy": 313863.02,
            "c_total_act_energy": 329816.08,
            "c_total_act_ret_energy": 253809.31,
            "total_act": 728967.39,
            "total_act_ret": 854363.52
        }
    }
}

I only need to retrieve total_act parameter from the 3rd type of message. However, because the template analyzes every message, it throws the aforementioned error for every message where emdata:0 parameter isn’t present.

Unfortunately all three messages are published to the same MQTT topic.

Is there a way to update the template to recognize the 3rd message only and try obtaining data only from those, while ignoring the others?

Thanks in advance!

      value_template: >
        {{ value_json.params["emdata:0"].total_act if value_json.params["emdata:0"] is defined else this.state }}

OMG you’re a beautiful person, and so are my logs now! Thank you! :slight_smile:

1 Like