Active Energy Calculator form increasing sensor

I have a sensor named: sensor.forward_energy_kwh, with the following details: raw_state: 1573.73

unit_of_measurement: kWh

device_class: energy

friendly_name: Forward Energy kWh.

This sensor takes reading for a smart meter. This is actually a sensor that tells the total forward energy/electricity consumed in my house. I want to calculate how much electricity I am consuming right now, so I can make a power card out of it. So I was thinking that if we could make an automaton like New Value - Old Value = active Value, We can get how much the current energy consumption is. Can you help me make this sensor/automation. I have been trying to use ChatGPT and other Ai to write me the code, but it is just not working out. I got this from chatgpt.

sensor:
  - platform: template
    sensors:
      current_energy_consumption:
        friendly_name: "Current Energy Consumption"
        unit_of_measurement: "kWh"
        device_class: energy
        value_template: >
          {% set previous_value = states('sensor.forward_energy_kwh') | float %}
          {% set current_value = state_attr('sensor.forward_energy_kwh', 'raw_state') | float %}
          {% set active_power = current_value - previous_value %}
          {{ active_power | round(2) }}

automation:
  - alias: Update Current Energy Consumption
    trigger:
      platform: state
      entity_id: sensor.forward_energy_kwh
    action:
      service: homeassistant.update_entity
      entity_id: sensor.current_energy_consumption

P.S I am using Home Assistant Supervised running on an OrangePi 3 LTS running armbian.

Any advice or help would be apreciated.

Thanks

I’m glad you mentioned the code was ChatGPT, because I would not want to waste my time debugging what that spits out. I took a quick look, but it won’t do what you need, because you do not know when the previous value was generated. It also has a wrong unit of measurement for power. If you want to continue to use chatGPT go ahead, but you won’t make friends asking us to fix the code.

But to help you with your question: It sounds like you are trying to derive (k)W from kWh? A derivative sensor would do pretty much that, but it might not be very accurate unless the kWh value changes quite frequently. But that problem is worse for what you hoped to do.

I have tried derivatives, but they are just not accurate, and won’t work with my inverter and Power Card.
Is there no way to make a sensor by just simply subtracting the old value from the new value on each update?
P.S Thank for your advice.

Because updates only come on change, the time between values varies. So you need to take that time into account if you want to know how much was used per x time. That is what derivative does. If it is not accurate enough, not other method will fare better. If your inverter does not supply watts, you are out of luck.

Getting it to show in the power card can be arranged, though I am surprised because I did not think it checked. Most likely the unit of measurement is wrong kWh/h (?). If that is the problem, you can override it with customize.