Advertisement
AndyBonde

EV Charging cost blueprint project

Dec 17th, 2024
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 8.62 KB | Source Code | 0 0
  1. # EV Charging Cost Calculator Blueprint
  2. # -------------------------------------
  3. # Project Description:
  4. # This blueprint is designed to calculate the **cheapest hour** to charge your electric vehicle (EV)
  5. # based on electricity prices fetched from the Nordpool integration.
  6. # It also provides calculations for:
  7. #   - The total cost of charging at the current hour
  8. #   - The total cost of charging at the cheapest hour
  9. #   - Potential savings if you wait for the cheapest hour
  10. #   - Energy needed to fully charge the EV (kWh)
  11. #   - Number of hours required to fully charge the EV
  12. #
  13. # The results are stored in helper entities (input_text), which can then be displayed
  14. # on a Home Assistant dashboard (Lovelace) for easy monitoring and decision-making.
  15. #
  16. # Purpose:
  17. # This blueprint allows EV owners to optimize their charging schedules to save money
  18. # by charging during the cheapest electricity hours.
  19. # It considers additional costs like VAT and extra fees per kWh.
  20. #
  21. # Key Features:
  22. # - Automatically calculates the **cheapest charging window**.
  23. # - Displays **savings** when comparing current charging costs to the cheapest hour.
  24. # - Updates results dynamically when electricity prices or battery level changes.
  25. # - Allows integration into Lovelace dashboards for real-time cost tracking.
  26. #
  27. # -------------------------------------
  28. # Installation Instructions:
  29. # 1. Copy this blueprint YAML file and place it in the 'blueprints/automation/[YourFolderName]' directory in Home Assistant.
  30. # 2. Reload the automations or restart Home Assistant.
  31. # 3. Navigate to the Automations section in Home Assistant.
  32. # 4. Create a new automation, select 'Blueprints', and choose 'EV Charging Cost Calculator'.
  33. # 5. Configure the required inputs:
  34. #    - Nordpool Price Entity (sensor)
  35. #    - Battery Percentage Entity (sensor)
  36. #    - Battery Capacity (kW)
  37. #    - Charger Capacity (kW)
  38. #    - VAT Percentage (%)
  39. #    - Extra Cost per kWh (kr)
  40. #    - Default Cost per kWh (kr)
  41. #    - Helper Entities (input_text) for:
  42. #        - Cheapest Start Hour
  43. #        - Cheapest Cost
  44. #        - Current Cost
  45. #        - Savings
  46. #        - Energy Needed
  47. #        - Hours Needed
  48. # 6. Save the automation and it will begin calculating based on the Nordpool prices and your EV configuration.
  49. # 7. Add the helper entities to your Lovelace dashboard for monitoring.
  50. #
  51. # Disclaimer:
  52. # -------------------------------------------------------------
  53. # This blueprint is provided "as is" without any guarantees or warranties.
  54. # I take no responsibility for any issues, damages, or inaccuracies that
  55. # may occur as a result of using this blueprint.
  56. #
  57. # This was my first blueprint and was created as a fun project.
  58. # There may be calculation errors, bugs, or logical mistakes.
  59. #
  60. # **Use it at your own risk.** You are responsible for verifying
  61. # all results and ensuring it meets your needs. This blueprint
  62. # is not professionally tested, and I cannot provide support.
  63. #
  64. # If something goes wrong, or the numbers seem incorrect,
  65. # double-check all calculations and configurations.
  66. # -------------------------------------------------------------
  67.  
  68.  
  69.  
  70. blueprint:
  71.   name: EV Charging Cost Calculator
  72.   description: Calculate the cheapest hour and cost to charge your EV based on Nordpool prices.
  73.   domain: automation
  74.   input:
  75.     nordpool_entity:
  76.       name: Nordpool Price Entity
  77.       selector:
  78.         entity:
  79.           domain: sensor
  80.     battery_percentage_entity:
  81.       name: Battery Percentage Entity
  82.       selector:
  83.         entity:
  84.           domain: sensor
  85.     battery_capacity:
  86.       name: Battery Capacity (kW)
  87.       default: 40
  88.       selector:
  89.         number:
  90.           min: 1
  91.           max: 200
  92.     charger_capacity:
  93.       name: Charger Capacity (kW)
  94.       default: 3.6
  95.       selector:
  96.         number:
  97.           min: 1
  98.           max: 22
  99.     vat_percentage:
  100.       name: VAT Percentage
  101.       default: 25
  102.       selector:
  103.         number:
  104.           min: 0
  105.           max: 100
  106.     extra_cost_per_kwh:
  107.       name: Extra Cost per kWh (kr)
  108.       default: 0.05
  109.       selector:
  110.         number:
  111.           min: 0
  112.           max: 5
  113.     default_cost_per_kwh:
  114.       name: Default Cost per kWh (kr)
  115.       default: 0.5
  116.       selector:
  117.         number:
  118.           min: 0
  119.           max: 5
  120.     cheapest_start_hour_entity:
  121.       name: Cheapest Start Hour Helper
  122.       selector:
  123.         entity:
  124.           domain: input_text
  125.     cheapest_cost_entity:
  126.       name: Cheapest Cost Helper
  127.       selector:
  128.         entity:
  129.           domain: input_text
  130.     current_cost_entity:
  131.       name: Current Cost Helper
  132.       selector:
  133.         entity:
  134.           domain: input_text
  135.     savings_entity:
  136.       name: Savings Helper
  137.       selector:
  138.         entity:
  139.           domain: input_text
  140.     energy_needed_entity:
  141.       name: Energy Needed Helper
  142.       selector:
  143.         entity:
  144.           domain: input_text
  145.     hours_needed_entity:
  146.       name: Hours Needed Helper
  147.       selector:
  148.         entity:
  149.           domain: input_text          
  150.  
  151. trigger:
  152.   - platform: state
  153.     entity_id: !input nordpool_entity
  154.   - platform: state
  155.     entity_id: !input battery_percentage_entity
  156.  
  157. variables:
  158.   nordpool_entity: !input nordpool_entity
  159.   battery_percentage_entity: !input battery_percentage_entity
  160.   default_cost_per_kwh: !input default_cost_per_kwh
  161.   today_prices: >
  162.    {{ state_attr(nordpool_entity, 'today') | default([default_cost_per_kwh]) | list }}
  163.   tomorrow_prices: >
  164.    {{ state_attr(nordpool_entity, 'tomorrow') | default([default_cost_per_kwh]) | list }}
  165.   combined_prices: >
  166.    {{ today_prices + tomorrow_prices }}
  167.   vat: !input vat_percentage
  168.   extra_cost: !input extra_cost_per_kwh
  169.   battery_capacity: !input battery_capacity
  170.   charger_capacity: !input charger_capacity
  171.   battery_percentage: >
  172.    {{ states(battery_percentage_entity) | float(0) }}
  173.   energy_needed_kwh: >
  174.    {{ ((100 - battery_percentage) / 100) * battery_capacity }}
  175.   hours_needed: >
  176.    {{ (energy_needed_kwh / charger_capacity) | round(0, 'ceil') }}
  177.   current_hour: >
  178.    {{ now().hour }}
  179.  
  180.   this_cheapest_price: >
  181.    {% set ns = namespace(min_cost=None, start_hour=None) %}
  182.     {% for i in range(current_hour, combined_prices | length - hours_needed + 1) %}
  183.       {% set hourly_cost = combined_prices[i:i+hours_needed] | sum %}
  184.       {% if ns.min_cost is none or hourly_cost < ns.min_cost %}
  185.         {% set ns.min_cost = hourly_cost %}
  186.         {% set ns.start_hour = i %}
  187.       {% endif %}
  188.     {% endfor %}
  189.     {% if ns.start_hour is not none %}
  190.       {% if ns.start_hour < 24 %}
  191.         {{ ns.start_hour }}
  192.       {% else %}
  193.         {{ (ns.start_hour - 24) | string.zfill(2) }} (Tomorrow)
  194.       {% endif %}
  195.     {% else %}N/A{% endif %}
  196.  
  197.   this_cheapest_cost: >
  198.    {% set ns = namespace(min_cost=None) %}
  199.     {% for i in range(current_hour, combined_prices | length - hours_needed + 1) %}
  200.       {% set hourly_cost = combined_prices[i:i+hours_needed] | sum %}
  201.       {% if ns.min_cost is none or hourly_cost < ns.min_cost %}
  202.         {% set ns.min_cost = hourly_cost %}
  203.       {% endif %}
  204.     {% endfor %}
  205.     {{ ((ns.min_cost + hours_needed * extra_cost) * (1 + vat / 100)) | round(2) if ns.min_cost is not none else 'N/A' }}
  206.  
  207.   this_current_cost: >
  208.    {% set now_hour = current_hour %}
  209.     {% if now_hour + hours_needed <= combined_prices | length %}
  210.       {% set current_cost = combined_prices[now_hour:now_hour+hours_needed] | sum %}
  211.       {{ ((current_cost + hours_needed * extra_cost) * (1 + vat / 100)) | round(2) }}
  212.     {% else %}N/A{% endif %}
  213.  
  214.   this_saving: >
  215.    {% set cheapest_cost = this_cheapest_cost | float(0) %}
  216.     {% set current_cost = this_current_cost | float(0) %}
  217.     {{ (current_cost - cheapest_cost) | round(2) if cheapest_cost != 'N/A' and current_cost != 'N/A' else 'N/A' }}
  218.  
  219. action:
  220.   - service: input_text.set_value
  221.     data:
  222.       entity_id: !input energy_needed_entity
  223.       value: >
  224.        {{ energy_needed_kwh | round(2) }}
  225.  
  226.   - service: input_text.set_value
  227.     data:
  228.       entity_id: !input hours_needed_entity
  229.       value: >
  230.        {{ hours_needed }}
  231.        
  232.   - service: input_text.set_value
  233.     data:
  234.       entity_id: !input cheapest_start_hour_entity
  235.       value: >
  236.        {{ this_cheapest_price }}
  237.        
  238.   - service: input_text.set_value
  239.     data:
  240.       entity_id: !input cheapest_cost_entity
  241.       value: >
  242.        {{ this_cheapest_cost }}
  243.        
  244.   - service: input_text.set_value
  245.     data:
  246.       entity_id: !input current_cost_entity
  247.       value: >
  248.        {{ this_current_cost }}
  249.        
  250.   - service: input_text.set_value
  251.     data:
  252.       entity_id: !input savings_entity
  253.       value: >
  254.        {{ this_saving }}
  255.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement