Get daily energy from a power sensor

This sensor allows me to know the istantaneous power drawn from the grid. How can I get the daily energy from this sensor? I tried to use the Total Daily Energy Sensor component but it requires an id which I don’t have

sensor:
  - platform: template
    sensors:
      prelievo_istantaneo_corrente:
        friendly_name: Prelievo istantaneo Chint
        device_class: power
        value_template: >
          {% set linea = states('sensor.esp32_chint_potenza_istantanea')|int(0)
          %} {% if linea < 0 %}
              {{ 0 - linea }}
          {% else %} {{ 0 | int }} {% endif %}
        unit_of_measurement: W

Is this in esphome ? Total Daily Energy Sensor is an esphome integration

If not, use this:

or install this, and it does it all for you:

The original sensor (sensor.esp32_chint_instantaneous_power) takes data from the registers of a Meter Chint through an Esp32 microcontroller in EspHome.
This register returns both positive and negative instantaneous power (feed to grid/puchased from grid).
I created a custom yaml file inside the packages folder in which, with the above code, I get two sensors, one for instantaneus purchased power and one for instantaneus feed power (named Prelievo istantaneo Chint and Immissione istantanea Chint).
I used Prelievo istantaneo Chint sensor in Integration - Riemann sum integral but I get som incredible data, as if in one day I purchased about 1.600 Wh :thinking:
Actually my daily withdrawals from the grid are much lower, around 1 to 20 Wh (verified by the electricity distributor)

You can use the Total Daily Energy Sensor in esphome, you need to use the id from the power entity in esphome, not ha.

Can you share your esphome code

esphome:
  name: "chint-meter"
  friendly_name: Esp32

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:
  level: verbose
  baud_rate: 0

#web_server:
  #port: 80

# Enable Home Assistant API
api:
  encryption:
    key: "yyyyyyy"

ota:
  password: "xxxxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
 
  #manual_ip:
    #static_ip: 192.168.1.234
    #gateway: 192.168.1.254
    #subnet: 255.255.255.0
    #dns1: 192.168.1.254
    #dns2: 8.8.4.4

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Esp32 Fallback Hotspot"
    password: "xxxxx"

captive_portal:
###
uart:
  id: mod_bus
  tx_pin: 03
  rx_pin: 01
  baud_rate: 9600
  stop_bits: 1

modbus:
  flow_control_pin: 5
  id: modbus1

modbus_controller:
    address: 0x001
    modbus_id: modbus1
    update_interval: 1s
    setup_priority: -10

sensor:
 - platform: modbus_controller
   id: chint_voltage
   name: "Chint Tensione"
   address: 0x2000
   unit_of_measurement: "V" ## for any other unit the value is returned in minutes
   register_type: holding
   value_type: FP32
   accuracy_decimals: 2
   filters:
     - multiply: 1

 - platform: modbus_controller
   id: chint_current
   name: "Chint Corrente"
   address: 0x2002
   unit_of_measurement: "A" ## for any other unit the value is returned in minutes
   register_type: read
   value_type: FP32
   accuracy_decimals: 2
   filters:
     - multiply: 1

 - platform: modbus_controller
   id: chint_power
   name: "Chint Potenza istantanea"
   address: 0x2004
   unit_of_measurement: "W" ## for any other unit the value is returned in minutes
   state_class: "measurement"
   register_type: read
   value_type: FP32
   accuracy_decimals: 2
   filters:
     - multiply: 1000

 - platform: modbus_controller
   id: chint_frequency
   name: "Chint Frequenza Rete"
   address: 0x200E
   unit_of_measurement: "Hz" ## for any other unit the value is returned in minutes
   register_type: read
   value_type: FP32
   accuracy_decimals: 2
   filters:
     - multiply: 1

 - platform: modbus_controller
   id: chint_energy_sold
   name: "Chint Energia ceduta"
   address: 0x4000
   unit_of_measurement: "KWh" ## for any other unit the value is returned in minutes
   register_type: read
   value_type: FP32
   accuracy_decimals: 2
   filters:
     - multiply: 1
 
 - platform: modbus_controller
   id: chint_energy_purchased
   name: "Chint Energia acquistata"
   address: 0x400A
   unit_of_measurement: "Wh" ## for any other unit the value is returned in minutes
   register_type: read
   value_type: FP32
   accuracy_decimals: 3
   filters:
     - multiply: 1000   

 - platform: modbus_controller
   id: chint_reactive_power
   name: "Chint Potenza reattiva"
   address: 0x2004
   unit_of_measurement: "Var" ## for any other unit the value is returned in minutes
   register_type: read
   value_type: FP32
   accuracy_decimals: 2
   filters:
     - multiply: 1000

Try adding this to the bottom of your sensors in esphome:

- platform: total_daily_energy
    name:  Chint Total Daily Energy
    power_id: chint_power
    id: totaldailyenergy
    filters:
        - multiply: 0.001
    unit_of_measurement: kWh
    icon: mdi:clock-alert
    device_class: energy
1 Like

I divided the power sensor into two sensors, negative and positive.
So I used these two sensors for Total_daily_energy.
Then I created two sensors for yesterday’s energy using Time.
Do you think it can work?

 - platform: template
   name: Prelievo Rete Chint
   id: prelievorete
   lambda: |-
      if (id(chint_power).state > 0) {
        return 0;
      } else {
        return abs(id(chint_power).state) ;
      }
   accuracy_decimals: 1
   unit_of_measurement: W
   icon: "mdi:flash-circle"
   update_interval: 1s

 - platform: template
   name: Immissione Rete Chint
   id: immissionerete
   lambda: |-
      if (id(chint_power).state < 0) {
        return 0;
      } else {
        return abs(id(chint_power).state) ;
      }
   accuracy_decimals: 1
   unit_of_measurement: W
   icon: "mdi:flash-circle"
   update_interval: 1s

 - platform: total_daily_energy
   name: Energia prelevata Chint
   power_id: immissionerete
   filters:
    - multiply: 0.001
   unit_of_measurement: KWh
   accuracy_decimals: 2
   icon: mdi:clock-alert
   device_class: energy

 - platform: total_daily_energy
   name: Energia immessa Chint
   power_id: prelievorete
   filters:
    - multiply: 0.001
   unit_of_measurement: KWh
   accuracy_decimals: 2
   icon: mdi:clock-alert
   device_class: energy

 - platform: template
   name: Energy Purchased Yesterday
   id: purchased_yesterday
   unit_of_measurement: Wh
   accuracy_decimals: 0
   icon: mdi:lightning-bolt

 - platform: template
   name: Energy Feed Yesterday
   id: feed_yesterday
   unit_of_measurement: Wh
   accuracy_decimals: 0
   icon: mdi:lightning-bolt

time:
  - platform: homeassistant
    on_time:
      - seconds: 59
        minutes: 59
        hours: 23
        then:
          - sensor.template.publish:
              id: purchased_yesterday
              state: !lambda |-
                return id(prelievorete).state;
          - sensor.template.publish:
              id: feed_yesterday
              state: !lambda |-
                return id(immissionerete).state;  

Should do, give it a try

It seems to work but the state of the sensor yesterday remains only a short time, then it becomes unknown.