Storing electricity cost for each cycle of device

Hello!

I’ve used this post to get a rolling energy cost for my Ender 3 Pro 3D-printer.
https://community.home-assistant.io/t/solved-calculate-cost-for-individual-device-cost-kwh/523880

One thing I am looking for though is the possibility to add up that cost as long as the printer is running (which I have a sensor for). I’d like this so I can see how much each print costs in electricity.
If possible, it would be great to be able to see a sort of history for this as well in some way.

I am not great with templating and general calculations in HA, that’s why I am coming here.

What I have now, with help from the link:

  • sensor.accumulated_wattage_ender_3_pro - A sensor for the accumulated wattage of the Ender 3 Pro (Reimann sum Integral)
  • sensor.accumulated_kw_per_hour_ender_3_pro - A sensor for accumulated kw per hour of the Ender 3 Pro (Utility Meter)
  • sensor.electricity_price_address - A current electricity cost sensor from Tibber integration
  • sensor.cost_hourly_ender_3_pro - A template sensor calculating the cost by multiplying current cost and accumulated kwh (Template Sensor)

I’d like an additional sensor (or whatever is required) to add together all the costs each hour as long as my input_select.ender_3_pro is in “Printing”-mode. And also add the starting and ending-hours’ costs to the sum as well, so I can send that number with my notification that he print is done.
After the print is done or when I start another, the total cost should reset.

If you don’t understand the 3D-printing part, think of it like I would want the cost of each Washing Machine cycle and not just see the cost for each hour.

Lots of information here, hopefully someone understands what I want and if it is possible!

Thanks guys

Edit: fixed entity names to Preformatted-text

1 Like

Hi
Cool that I might be able to contribute with some of my ideas here :smiley: (the post linked is my OP) :laughing:

I think this is manageable, but we need to work this out together.
Let me draw this up in my head and I’ll come back to you.

:dove:

1 Like

I think I’ve solved it myself!

Took some time, pseudo code, googling and trying stuff out in Developer Tools but I think I have a finished solution.

I made a new entity, input_number.ender_3_pro_print_cost that will save the total cost. It is a Helper/Number that became an input_number.

I made a new Automation that triggers each hour, on 59 minutes and 59 seconds (**:59:59), so it runs before @KaherdinTristan’s accumulated cost resets for the hour. It has a condition that the input_select.ender_3_pro has to be in the Printing state.
If so, Call this service that takes the current value of input_number.ender_3_pro_print_cost and adds the current value from the sensor.cost_hourly_ender_3_pro to it.

service: input_number.set_value
data_template:
  value: >-
    {{ (states("input_number.ender_3_pro_print_cost") | float) +
    (states("sensor.cost_hourly_ender_3_pro") | float) }}
target:
  entity_id: input_number.ender_3_pro_print_cost

When the print ends or rather End-of-print automation runs, its first action is to do the same call as above again, then sets the printer to idle, sends a notification to my phone (with the cost and runtime!) and lastly resets the input_number.ender_3_pro_print_cost to 0. (The -3 minutes is because the trigger waits 3 minutes before telling me the printer is done)

Notification message:

  message: >-
    Ender 3 Pro has finished printing! {{'\n'}}
    The cost of the print was {{states('input_number.ender_3_pro_print_cost') }}kr! {{'\n'}}
    It took {{(as_timestamp(now()-timedelta( minutes = 3)) - as_timestamp(states.input_select.ender_3_pro.last_changed)) | timestamp_custom("%H:%M:%S", false)}} to print.

Reset to 0:

service: input_number.set_value
data:
  value: 0
target:
  entity_id: input_number.ender_3_pro_print_cost

I have only tried it by manually running the automations and trying things in Develepor tools, but it seems to do fine.

This is a test notification i did!

May it help someone with equal issue!

EDIT: I have no idea but it is probably possible to do a more elegant solution and/or remove some stuff and it will work the same. But it works :smiley: