Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # A sensor that indicates how many prepaid units you have left
- input_number:
- prepaid_units:
- name: Enter Prepaid Units from Meter
- min: 0
- max: 999
- step: 1
- unit_of_measurement: "kWh"
- mode: box
- icon: mdi:home-lightning-bolt-outline
- prepaid_units_left:
- name: Enter Prepaid Units Left
- min: -999
- max: 999
- step: 1
- unit_of_measurement: "kWh"
- icon: mdi:home-lightning-bolt-outline
- initial_grid_import:
- name: Initial Grid Import
- min: 0
- max: 99999
- step: 1
- unit_of_measurement: "kWh"
- icon: mdi:counter
- # Input datetime to store the last update time
- input_datetime:
- last_update:
- name: Last Update
- has_date: true
- has_time: true
- # Template sensor to calculate remaining units
- template:
- - sensor:
- - name: "Prepaid Units Left"
- icon: mdi:home-lightning-bolt-outline
- unit_of_measurement: "kWh"
- state: >
- {% set initial_import = states('input_number.initial_grid_import') | float %}
- {% set current_import = states('sensor.sunsynk_total_grid_import') | float %}
- {% set units = states('input_number.prepaid_units') | float %}
- {% set used_units = current_import - initial_import %}
- {{ (units - used_units) | round(2) }}
- - name: "Units Used Since Last Update"
- icon: mdi:flash
- unit_of_measurement: "kWh"
- state: >
- {% set initial_import = states('input_number.initial_grid_import') | float %}
- {% set current_import = states('sensor.sunsynk_total_grid_import') | float %}
- {{ (current_import - initial_import) | round(2) }}
- - name: "Last Update Date"
- icon: mdi:calendar
- state: >
- {{ states('input_datetime.last_update') }}
- ################################### scripts.yaml
- script:
- update_prepaid_units:
- alias: Update Prepaid Units
- sequence:
- - service: input_number.set_value
- target:
- entity_id: input_number.prepaid_units
- data:
- value: "{{ states('input_number.prepaid_units') | float }}"
- - service: input_number.set_value
- target:
- entity_id: input_number.prepaid_units_left
- data:
- value: "{{ states('sensor.prepaid_units_left') | float }}"
- - service: input_number.set_value
- target:
- entity_id: input_number.initial_grid_import
- data:
- value: "{{ states('sensor.sunsynk_total_grid_import') | float }}"
- - service: input_datetime.set_datetime
- target:
- entity_id: input_datetime.last_update
- data:
- datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
- mode: single
Advertisement
Add Comment
Please, Sign In to add comment