Guest User

PrePaid

a guest
Apr 8th, 2025
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. # A sensor that indicates how many prepaid units you have left
  2.  
  3. input_number:
  4. prepaid_units:
  5. name: Enter Prepaid Units from Meter
  6. min: 0
  7. max: 999
  8. step: 1
  9. unit_of_measurement: "kWh"
  10. mode: box
  11. icon: mdi:home-lightning-bolt-outline
  12. prepaid_units_left:
  13. name: Enter Prepaid Units Left
  14. min: -999
  15. max: 999
  16. step: 1
  17. unit_of_measurement: "kWh"
  18. icon: mdi:home-lightning-bolt-outline
  19. initial_grid_import:
  20. name: Initial Grid Import
  21. min: 0
  22. max: 99999
  23. step: 1
  24. unit_of_measurement: "kWh"
  25. icon: mdi:counter
  26.  
  27. # Input datetime to store the last update time
  28. input_datetime:
  29. last_update:
  30. name: Last Update
  31. has_date: true
  32. has_time: true
  33.  
  34. # Template sensor to calculate remaining units
  35. template:
  36. - sensor:
  37. - name: "Prepaid Units Left"
  38. icon: mdi:home-lightning-bolt-outline
  39. unit_of_measurement: "kWh"
  40. state: >
  41. {% set initial_import = states('input_number.initial_grid_import') | float %}
  42. {% set current_import = states('sensor.sunsynk_total_grid_import') | float %}
  43. {% set units = states('input_number.prepaid_units') | float %}
  44. {% set used_units = current_import - initial_import %}
  45. {{ (units - used_units) | round(2) }}
  46. - name: "Units Used Since Last Update"
  47. icon: mdi:flash
  48. unit_of_measurement: "kWh"
  49. state: >
  50. {% set initial_import = states('input_number.initial_grid_import') | float %}
  51. {% set current_import = states('sensor.sunsynk_total_grid_import') | float %}
  52. {{ (current_import - initial_import) | round(2) }}
  53. - name: "Last Update Date"
  54. icon: mdi:calendar
  55. state: >
  56. {{ states('input_datetime.last_update') }}
  57.  
  58. ################################### scripts.yaml
  59. script:
  60. update_prepaid_units:
  61. alias: Update Prepaid Units
  62. sequence:
  63. - service: input_number.set_value
  64. target:
  65. entity_id: input_number.prepaid_units
  66. data:
  67. value: "{{ states('input_number.prepaid_units') | float }}"
  68. - service: input_number.set_value
  69. target:
  70. entity_id: input_number.prepaid_units_left
  71. data:
  72. value: "{{ states('sensor.prepaid_units_left') | float }}"
  73. - service: input_number.set_value
  74. target:
  75. entity_id: input_number.initial_grid_import
  76. data:
  77. value: "{{ states('sensor.sunsynk_total_grid_import') | float }}"
  78. - service: input_datetime.set_datetime
  79. target:
  80. entity_id: input_datetime.last_update
  81. data:
  82. datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
  83. mode: single
Advertisement
Add Comment
Please, Sign In to add comment