Smart Home Energy Monitor by Fusion Energy

Below is the script I am using to publish the entities for this meter instead of defining them in YAML. Obviously enter your device’s MAC address at the top. This will need to be called by an automation on every HA startup, or you can change “retain” to true for every message to keep them in your MQTT broker. I’m not a code wizard so pardon my ignorance if this could be even more streamlined.

I left out Phase C since I am not using it, but it should be straightforward to add it in. I made three sensors per channel (current, power, and energy) which might be overkill; the Emporia esphome config only tracks power and energy on each channel. Note also I had to add a 1-second delay in the loop because HA wasn’t keeping up with messages arriving too fast, so this takes 16s to run.

alias: Create Fusion Power Device Sensors
variables:
  macaddr: YOURMACALLCAPS
  topic: SEMMETER/{{macaddr}}/HA
  device_block: >-
    "device":{ "identifiers": ["{{macaddr}}"], "manufacturer": "Fusion Energy","model": "SEM METER", "name": "sem-meter" }
  current_sensor: >-
    "state_class":"measurement","device_class":"current","unit_of_measurement":"A","icon":"mdi:current-ac","state_topic":"{{topic}}",
  voltage_sensor: >-
    "state_class":"measurement","device_class":"voltage","unit_of_measurement":"V","icon":"mdi:flash","state_topic":"{{topic}}",
  power_sensor: >-
    "state_class":"measurement","device_class":"power","unit_of_measurement":"W","icon":"mdi:power-plug","state_topic":"{{topic}}",
  energy_sensor: >-
    "state_class":"total_increasing","device_class":"energy","unit_of_measurement":"kWh","icon":"mdi:counter","state_topic":"{{topic}}",
sequence:
  - action: mqtt.publish
    data:
      retain: false
      topic: homeassistant/sensor/{{macaddr}}_total_power/config
      payload: >-
        {"name": "Total Power", "unique_id":"SEM{{macaddr}}_total_power","value_template": "{% raw %}{{ (value_json.sense[16][2] | float | multiply(0.01) +  value_json.sense[17][2] | float | multiply(0.01) + value_json.sense[18][2] | float | multiply(0.01)) | round(3) }}{% endraw %}", {{power_sensor}} {{device_block}} }
  - action: mqtt.publish
    data:
      retain: false
      topic: homeassistant/sensor/{{macaddr}}_total_energy/config
      payload: >-
        {"name": "Total Energy", "unique_id":"SEM{{macaddr}}_total_energy", "value_template": "{% raw %}{{ (value_json.sense[16][3] | float | multiply(0.001) +  value_json.sense[17][3] | float | multiply(0.001) + value_json.sense[18][3] | float | multiply(0.001)) | round(3) }}{% endraw %}", {{energy_sensor}} {{device_block}} }
  - action: mqtt.publish
    data:
      retain: false
      topic: homeassistant/sensor/{{macaddr}}_phase_a_voltage/config
      payload: >-
        {"name": "Phase A Voltage", "unique_id":"SEM{{macaddr}}_phase_a_voltage", "value_template": "{% raw %}{{ value_json.sense[16][0] | float | multiply(0.1) | round(3) }}{% endraw %}", {{voltage_sensor}} {{device_block}} }
  - action: mqtt.publish
    data:
      retain: false
      topic: homeassistant/sensor/{{macaddr}}_phase_b_voltage/config
      payload: >-
        {"name": "Phase B Voltage","unique_id":"SEM{{macaddr}}_phase_b_voltage", "value_template": "{% raw %}{{ value_json.sense[17][0] | float | multiply(0.1) | round(3) }}{% endraw %}", {{voltage_sensor}} {{device_block}} }
  - action: mqtt.publish
    data:
      retain: false
      topic: homeassistant/sensor/{{macaddr}}_phase_a_current/config
      payload: >-
        {"name": "Phase A Current", "unique_id":"SEM{{macaddr}}_phase_a_current",  "value_template": "{% raw %}{{ value_json.sense[16][1] | float | multiply(0.01) | round(3) }}{% endraw %}", {{current_sensor}} {{device_block}} }
  - action: mqtt.publish
    data:
      retain: false
      topic: homeassistant/sensor/{{macaddr}}_phase_b_current/config
      payload: >-
        {"name": "Phase B Current", "unique_id":"SEM{{macaddr}}_phase_b_current", "value_template": "{% raw %}{{ value_json.sense[17][1] | float | multiply(0.01) | round(3) }}{% endraw %}", {{current_sensor}} {{device_block}} }
  - repeat:
      until:
        - condition: template
          value_template: "{{ repeat.index == 16}}"
      sequence:
        - delay: 1
        - action: mqtt.publish
          data:
            retain: false
            topic: homeassistant/sensor/{{macaddr}}_channel_{{repeat.index}}_current/config
            payload: >-
              {"name": "Channel {{repeat.index}} Current",  "unique_id": "SEM{{macaddr}}_channel_{{repeat.index}}_current", "value_template": "{% raw %}{{ value_json.sense[{% endraw %}{{repeat.index-1}}{% raw %}][1] | float | multiply(0.01) | round(3) }}{% endraw %}", {{current_sensor}} {{device_block}} }
        - action: mqtt.publish
          data:
            retain: false
            topic: homeassistant/sensor/{{macaddr}}_channel_{{repeat.index}}_power/config
            payload: >-
              {"name": "Channel {{repeat.index}} Power",  "unique_id": "SEM{{macaddr}}_channel_{{repeat.index}}_power", "value_template": "{% raw %}{{ value_json.sense[{% endraw %}{{repeat.index-1}}{% raw %}][2] | float | multiply(0.01) | round(3) }}{% endraw %}", {{power_sensor}} {{device_block}} }
        - action: mqtt.publish
          data:
            retain: false
            topic: homeassistant/sensor/{{macaddr}}_channel_{{repeat.index}}_energy/config
            payload: >-
              {"name": "Channel {{repeat.index}} Energy",  "unique_id": "SEM{{macaddr}}_channel_{{repeat.index}}_energy", "value_template": "{% raw %}{{ value_json.sense[{% endraw %}{{repeat.index-1}}{% raw %}][3] | float | multiply(0.001) | round(3) }}{% endraw %}", {{energy_sensor}} {{device_block}} }

3 Likes