Three phase PZEM004T Energy Monitor with esphome

I have finally got a working config with three PZEM004T monitoring my boiler/heater to report the fundamental data through an Esp32 set up in esphome.

However, I fail to get an accumulated energy sensor to be available for the HA Energy dashboard. I can get the separate L1, L2 and L3 energy readings into the Energy dashboard, but not the template sensor.

I have looked at a few other examples in the community but neither of them have solved the problem, so what am I doing wrong?
I am expecting either either/both bvp_energy or bvp_energy_2 to be avialable to add to the Energy dashboard…

excerp of configuration.yaml:


  - platform: integration
    source: sensor.bvp_power
    name: bvp_energy_2
    unit_prefix: k
    unit_time: h
    round: 2
    type or paste code here
...
  - platform: template
    sensors:
      bvp_current:
        friendly_name: "BVP Current"
#        unique_id: bvpcurrent
        entity_id:
          - sensor.bvp_l1_current
          - sensor.bvp_l2_current_2
          - sensor.bvp_l3_current_2
        value_template: "{{ (states('sensor.bvp_l1_current')|float + states('sensor.bvp_l2_current_2')|float + states('sensor.bvp_l3_current_2')|float)|round(3) }}"
        unit_of_measurement: "A"
        icon_template: "{{'mdi:current-ac'}}"
        
  - platform: template
    sensors:
      bvp_power:
        friendly_name: "BVP Power"
        unique_id: bvp_power
        entity_id:
          - sensor.bvp_l1_power
          - sensor.bvp_l2_power_2
          - sensor.bvp_l3_power_2
        value_template: "{{ (states('sensor.bvp_l1_power')|float + states('sensor.bvp_l2_power_2')|float + states('sensor.bvp_l3_power_2')|float)|round(3) }}"
        unit_of_measurement: "W"
#        icon_template: "{{'mdi:flash'}}"
        device_class: power
        
  - platform: template
    sensors:
      bvp_energy:
        friendly_name: "BVP Energy"
        unique_id: bvp_energy
        entity_id:
          - sensor.bvp_l1_energy
          - sensor.bvp_l2_energy_2
          - sensor.bvp_l3_energy_2
        value_template: "{{ ((states('sensor.bvp_l1_energy')|float + states('sensor.bvp_l2_energy_2')|float + states('sensor.bvp_l3_energy_2')|float))/1000|round(3) }}"
        unit_of_measurement: "kWh"
# state_class thows error when checking config
#        state_class: total_increasing
        device_class: energy
#        icon_template: "{{'mdi:lightning-bolt'}}"

1 Like

You have to add this using customize if using the legacy template sensor platform. Or switch to using the new template integration that does support state_class.

Also entity_id: is a depreciated option for the legacy template sensor, remove it.

Also you are only rounding the number 1000 and have not defined default values for your float filters.

So, using the legacy sensor platform:

sensor:
  - platform: template
    sensors:
      bvp_energy:
        friendly_name: "BVP Energy"
        unique_id: bvp_energy
        value_template: "{{ ( ( states('sensor.bvp_l1_energy')|float(0) + states('sensor.bvp_l2_energy_2')|float(0) + states('sensor.bvp_l3_energy_2')|float(0) )/1000 )|round(3) }}"
        availability_template: >
          {{ states('sensor.bvp_l1_energy') not in ['unavailable', 'unknown', 'none'] and
             states('sensor.bvp_l2_energy_2') not in ['unavailable', 'unknown', 'none'] and
             states('sensor.bvp_l3_energy_2') not in ['unavailable', 'unknown', 'none'] }}
        unit_of_measurement: "kWh"
        icon_template: "{{'mdi:flash'}}"
        device_class: energy

With this in customize:

sensor.bvp_energy:
  state_class: total_increasing

Or by using the template integration:

template:
  - sensor:
      - name: "BVP Energy"
        unique_id: bvp_energy
        state: "{{ ( ( states('sensor.bvp_l1_energy')|float(0) + states('sensor.bvp_l2_energy_2')|float(0) + states('sensor.bvp_l3_energy_2')|float(0) )/1000 )|round(3) }}"
        availability: >
          {{ states('sensor.bvp_l1_energy') not in ['unavailable', 'unknown', 'none'] and
             states('sensor.bvp_l2_energy_2') not in ['unavailable', 'unknown', 'none'] and
             states('sensor.bvp_l3_energy_2') not in ['unavailable', 'unknown', 'none'] }}
        unit_of_measurement: "kWh"
        icon: 'mdi:lightning-bolt'
        device_class: energy
        state_class: total_increasing

Thanks @tom_l,

So it was not the question of what I was doing wrong, but more of what I was doing right… :thinking:

The subtle difference of sensors vs sensor in order to use the new syntax went all below my radar, I’ll will have a go at my other template sensors too and sanitize them. Same thing with float…