Defining entities from MQQT topic (Shelly PM mini Gen3)

I am new to Home Assistant and struggling with defining an entity from a json topic from mqtt.

This is the json value from the Shelly PM Mini Gen3…

{
  "id": 0,
  "voltage": 244.1,
  "current": 5.884,
  "apower": 1442,
  "freq": 49.9,
  "aenergy": {
    "total": 3709.993,
    "by_minute": [
      23831.155,
      23209.473,
      23416.701
    ],
    "minute_ts": 1746281640
  },
  "ret_aenergy": {
    "total": 0,
    "by_minute": [
      0,
      0,
      0
    ],
    "minute_ts": 1746281640
  }
}

I can define the first five entities fine, eg… value_template: “{{ value_json.voltage }}”

ie.

mqtt:
  sensor:
    - name: "Power"
      unique_id: 2eca5cb5-562a-4cea-9282-4f016b1ae50d
      state_topic: "shelly/pmminig3/DEVICE-ID/status/pm1:0"
      value_template: "{{ value_json.apower }}"
      icon: mdi:solar-power
      device_class: power
      state_class: measurement
      unit_of_measurement: "W"
      json_attributes_template: "{{ value_json | float(0) | round(1) }}"
      device: {
        identifiers: [
          "PM Mini Gen3"
        ],
        manufacturer: "Shelly",
        model: "PM Mini Gen3",
        name: "PM Mini Gen3"}

but how do I get aenergy total and ret_aenergy total? everything I tried doesn’t work.
Note: I tried the Shelly integration but it polls every 5 minutes, I added a script to the PM mini and it polls every second on MQTT. Tried, but could not find a way to increase the polling frequency in the integration.

{{ value_json.aenergy.total }}
{{ value_json.ret_aenergy.total }}

You can experiment with templates in the Template Editor (Developer tools → Template).

Copy-paste the following into the Template Editor and experiment with it:

{% set value_json = 
{
  "id": 0,
  "voltage": 244.1,
  "current": 5.884,
  "apower": 1442,
  "freq": 49.9,
  "aenergy": {
    "total": 3709.993,
    "by_minute": [
      23831.155,
      23209.473,
      23416.701
    ],
    "minute_ts": 1746281640
  },
  "ret_aenergy": {
    "total": 0,
    "by_minute": [
      0,
      0,
      0
    ],
    "minute_ts": 1746281640
  }
}
%}

{{ value_json.aenergy.total }}
{{ value_json.ret_aenergy.total }}
{{ value_json.ret_aenergy.by_minute[0] }}

OMG, thank you so much. Worked perfectly and I learned a little more. So simple when you know. And I had never used the editor before, so trying it now.

The time that Template Editor saves is amazing, and no constant restarts, as I say still learning…

Another tip:

Sometimes the dictionary’s key name is the same name as a built-in dictionary method.

Let’s say we have the following dictionary:

{
  "id": 0,
  "foo": {
    "total": 10,
    "values": 42
   }
}

It has a key named values but there’s a dictionary method named values. In that case, you cannot use “dot” notation to reference it (because it will get confused and think you’re trying to use the values method).

{{ value_json.foo.values }}    <-- Invalid

You have to use “bracket” notation. Here’s a mix of dot and bracket notation.

{{ value_json.foo['values'] }}

Here’s full bracket notation.

{{ value_json['foo']['values'] }}

You’re welcome!

Please consider marking my post above with the Solutions tag. It will automatically place a check-mark next to the topic’s title indicating this topic has been solved. This helps users find answers to similar questions.

Thank you, that makes perfect sense.

1 Like