Updating a Template Sensor value every 24rs Using Automation to retrieve a Switch Attribute Value

I have a challenge, I’m trying to capture an incremental power usage value every 24hrs (eg at midnight) and display this value as a daily sensor value which is only updated once a day (so the value shows the previous days 24 hr usage).

I have a the configuration setup to display the cumulative power usage throughout the day as a dynamic value, but I want to capture a static value every 24hrs. Below is the configuration to grab the dynamic HS110 cumulative power consumption (reset every 24hrs)

  • platform: template
    sensors:
    todayenergy:
    value_template: ‘{{states.switch.pc_powerpoint.attributes[“today_energy_kwh”]}}’
    friendly_name: “Today’s Energy Use”
    unit_of_measurement: ‘kWh’

I’m thinking i need to use an automation job to fire off at a designated time which is fine. But i want the action to update a variable or a template sensor value with the switch attribute or a sensor value which im unsure of.

If anyone has any advice on how to store a variable so that it can be displayed and referenced in calculations that would be much appreciated.

How is the static value you need different from the switch attribute? Would you just need the maximum value? The value at 23:59:59?

If you search the forums for ‘cumulative’ you will find a lot of posts that might provide a solution.

Cheers metbril, I’ve scoured the forums, but I’ll do another check for ‘cumulative’ and see what turns up.

Essentially I am just trying to get the switch attribute at 23:59, but i want to lock that away as a value to do calculations from etc and display that as a separate value to be updated at 23:59 every day. So it’s a lil bit different than just displaying the current switch attribute in real time (currently doing that fine).

I’ve started building a automation task to run at 23:59, the challenge i have is that an automation action is largely to do something with a service (e.g. turn something on, turn something off). Whereas I just want to update a switch attribute value. So I’m unsure whether I can update an existing switch attribute using an automation action, and thats the stumbling block.

Not sure if you’ve used automation tasks in this way and whether you have any advice?

I started looking at template values as a means to store a static value at 23:59 daily, but unsure how to manipulate these with automation actions?

The easiest way to do this is to create a (hidden) input_number sensor whose value can be set with an automation that runs at 23:59. Something like this:

input_number:
  todayenergy:
    name: PC Powerpoint Today Energy
    unit_of_measurement: "KWh"
    min: 0
    max: 9999
    initial: 0
    icon: mdi:flash

automation:
  - alias: Save End Of Day Total Energy
    trigger:
      - platform: time
        at: '23:59:30'
    action:
      - service: input_number.set_value
        data_template:
          entity_id: input_number.todayenergy
          value: "{{ states.sensor.todayenery.state }}"

Some more topics to read:

3 Likes

Just another thought…

You could also build an automation that monitors the value of sensor.todayenergy changing to 0 (zero). If so, you can use the trigger.from_state.state to populate the input_select.

2 Likes

I was just reading:

And it sounds exactly the kind of outcome I’m looking for, awesome.

Cheers for the direction!!!

Big thanks to @metbril for this. Works great and I’m using this to keep daily measures for total energy, grid electricity, solar yield and self-sufficiency percentage. Very easy and elegant solution!

You could also create a ‘daily’ utility meter helper. That will record today’s usage, but also has an attribute for ‘previous period’, which in this case would be yesterday.