Automation: Update Virtual Sensor Value at Midnight

I have an energy sensor that accumulates kWh for “ever”. I want to obtain/retain the value of this sensor at midnight each day in order I can collect daily energy usage. If this was python I might have code that looks like this:

def midnightHour():
   gblMidnightValue = accumulatedkwh

def dailyUsage():
   kwhUsedToday = accumulatedkwh - gblMidnightValue

I have automation that is triggered at midnight but I have no idea how to assign/retain this value