Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save iiidefix/2d170a9cbe92974b25fdd1f88adb2c96 to your computer and use it in GitHub Desktop.

Select an option

Save iiidefix/2d170a9cbe92974b25fdd1f88adb2c96 to your computer and use it in GitHub Desktop.
Home Assistant MQTT Discovery Script for Meross EM06P Smart Energy Monitor
#!/usr/bin/env bash
set -euo pipefail
# configuration
BROKER_HOST="${BROKER_HOST:-localhost}"
BROKER_PORT="${BROKER_PORT:-1883}"
BROKER_AUTH="${BROKER_AUTH:-}" # optional "user:pass"
PREFIX="${PREFIX:-homeassistant}"
DEVICE_NAME="${DEVICE_NAME:-Meross EM06P}" # change device name
DEVICE_ID="${DEVICE_ID:-meross-em06p-c4e7ae000000}" # change device id
STATE_TOPIC="${STATE_TOPIC:-${DEVICE_ID}/notify}"
# sensors list (field|unit|device_class|label|default_state)
SENSORS='
current|A|current|Current|enabeld
voltage|V|voltage|Voltage|enabeld
power|W|power|Power|enabeld
pf||power_factor|Power Factor|enabeld
day_energy|Wh|energy|Energy Day|disabled
month_energy|Wh|energy|Energy Month|disabled
week_energy|Wh|energy|Energy Week|disabled
year_energy|Wh|energy|Energy Year|disabled
day_ret_energy|Wh|energy|Energy returned Day|disabled
month_ret_energy|Wh|energy|Energy returned Month|disabled
week_ret_energy|Wh|energy|Energy returned Week|disabled
year_ret_energy|Wh|energy|Energy returned Year|disabled
'
# helper to publish with curl (uses curl mqtt://host/topic -d payload)
publish() {
local topic=$1 payload=$2 url
url="mqtt://${BROKER_HOST}:${BROKER_PORT}/${topic}"
if [ -n "${BROKER_AUTH}" ]; then
curl --silent --show-error --user "${BROKER_AUTH}" --data "${payload}" "${url}" >/dev/null
else
curl --silent --show-error --data "${payload}" "${url}" >/dev/null
fi
}
abc=(A B C);
for em in {1..6}; do
echo "${SENSORS}" | while IFS= read -r line; do
[[ -z "${line}" ]] && continue
IFS='|' read -r field unit device_class label default_state <<<"${line}"
disc_topic="${PREFIX}/sensor/${DEVICE_ID}/em${em}_${field}/config"
unique_id="${DEVICE_ID}_em${em}_${field}"
value_template="{{ value_json.params['em:${em}'].${field} }}"
# name="Channel ${em} ${label}"
name="EM${em}: ${abc[$(( (em - 1) % 3 ))]}$(( (em - 1) / 3 + 1 )) ${label}"
payload="{\"name\":\"${name}\",\"unique_id\":\"${unique_id}\",\"state_topic\":\"${STATE_TOPIC}\",\"value_template\":\"${value_template}\",\"expire_after\":1800,\"device\":{\"identifiers\":[\"${DEVICE_ID}\"],\"name\":\"${DEVICE_NAME}\",\"manufacturer\":\"Meross\",\"model\":\"EM06P\"},\"device_class\":\"${device_class}\""
[[ -n "${unit}" ]] \
&& payload+=",\"unit_of_measurement\":\"${unit}\""
[[ ${default_state} == "disabled" ]] \
&& payload+=",\"enabled_by_default\":false"
payload+="}"
publish "${disc_topic}" "${payload}"
printf 'Published %s -> %s\n' "${disc_topic}" "${unique_id}"
done
done
@Pitito

Pitito commented Jun 13, 2026

Copy link
Copy Markdown

Hello,
How do we use your script?
Thanks for your help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment