Shelly EM into Home Assistant over MQTT: topics, units and the Energy dashboard

There are two ways to bring a Shelly EM into Home Assistant. The native integration finds it on its own (over CoIoT on Gen1), needs no YAML and keeps the Shelly app and cloud: it is the way for most people. MQTT makes sense if you already run a broker, want the whole house on MQTT, or need the topics for another system. It is the one that raises most questions, so it is the one covered here.

Enabling MQTT, and what you lose

On the device’s web page (its IP in a browser): Internet & Security → Advanced — Developer Settings, enable MQTT, enter the broker’s IP, user and password, port 1883, and save. The default prefix is shellies/shellyem-<id>, where <id> is the hexadecimal part of the device’s identifier, shown on its web page.

On the Gen1 EM, enabling MQTT disables the Shelly cloud. You lose remote control from the app and the Shelly Cloud history; on your network and in Home Assistant everything keeps working.

What it publishes, and in which units

Each reading has its own topic under shellies/shellyem-<id>/emeter/<channel>/, with 0 and 1 for the two clamps:

TopicWhat it isUnit
powerActive powerW
reactive_powerReactive powerVAR
voltageVoltageV
totalAccumulated energy consumed, survives rebootsWh
total_returnedAccumulated energy returned, survives rebootsWh
energyConsumed since last bootW·min
returned_energyReturned since last bootW·min

Also, shellies/shellyem-<id>/online publishes true/false for the device’s connection, and the relay is read on relay/0 and commanded on relay/0/command with on, off or toggle.

The rule that matters: to count energy, use the Wh accumulators — total and total_returned — divided by 1000. The W·min ones reset every time the device boots and are in a unit that needs converting twice: they are no good for keeping count.

The sensors in Home Assistant

Three sensors per channel: instantaneous power, to watch consumption live and for automations; and the two energies, which feed the Energy dashboard. Replace XXXXXX with your device’s identifier:

mqtt:
  sensor:
    - name: "EM power"
      state_topic: "shellies/shellyem-XXXXXX/emeter/0/power"
      unit_of_measurement: "W"
      device_class: power
      state_class: measurement
      availability_topic: "shellies/shellyem-XXXXXX/online"
      payload_available: "true"
      payload_not_available: "false"

    - name: "EM energy consumed"
      state_topic: "shellies/shellyem-XXXXXX/emeter/0/total"
      value_template: "{{ (value | float / 1000) | round(3) }}"
      unit_of_measurement: "kWh"
      device_class: energy
      state_class: total_increasing

    - name: "EM energy returned"
      state_topic: "shellies/shellyem-XXXXXX/emeter/0/total_returned"
      value_template: "{{ (value | float / 1000) | round(3) }}"
      unit_of_measurement: "kWh"
      device_class: energy
      state_class: total_increasing

The combination of device_class: energy + state_class: total_increasing on a kWh accumulator is what makes Home Assistant offer it in the Energy dashboard and split it by hour, day and month. Energy consumed goes in as “grid consumption” and energy returned as “return to grid”. The availability_topic makes the sensors show unavailable if the device drops off, instead of freezing at the last value. Repeat the block with /1/ for the second clamp.

Common mistakes

  • Using energy in the Energy dashboard: it resets on every boot and is in W·min.
  • Forgetting state_class: total_increasing: the sensor does not appear in the dashboard.
  • Using returned_energy for export: the same problem as energy; it is total_returned.
  • Expecting the app to work remotely: on Gen1, MQTT and cloud are mutually exclusive.

And with an EM Gen3 or a Pro?

Gen2 and later speak MQTT with a different, RPC-based structure: with “Generic status update over MQTT” enabled they publish each component’s full status as JSON, on <prefix>/status/em1:0, rather than one topic per quantity. And on them MQTT and the cloud coexist. For those devices Home Assistant’s native integration (over WebSocket RPC) is even more direct; MQTT is for when you specifically want it.

Frequently asked questions

Do I need a broker?

Yes. The EM publishes and something has to receive: Mosquitto, for example Home Assistant’s official add-on.

Does the EM measure solar export?

Yes, it meters both directions: what you export appears in total_returned, which is what the Energy dashboard needs for return to grid.

In short: total and total_returned in Wh ÷ 1000, device_class: energy and state_class: total_increasing; power only for the instant; and on Gen1, MQTT at the cost of the cloud.