I have managed to set up 2 tariff system for electricity in home assistant and it works just fine, there is automation that switches between tariffs.
But this works only from the moment I added the cost measurement, which is totally fine and understandable.
Does anybody know, if it is possible, how to apply tariff rules for the past period?
If not possible, does anybody know what is wrong with my script that does the calculation for the past period, because it always return 0kwh spent…
What am I missing here?
Thanks
the script:
alias: Test Energy Cost Calculation
sequence:
- variables:
peak_price: 13.7
offpeak_price: 3.4
offpeak_start: "23:00"
offpeak_end: "07:00"
start_date: "2024-01-04"
end_date: "{{ now().strftime('%Y-%m-%d') }}"
- data:
title: Energy Consumption Debugging
message: >
{% set peak_total = 0 %} {% set offpeak_total = 0 %} {% set
total_consumption = 0 %}
{% for entity in ['sensor.l1_unutrasnja_jedinica_energy',
'sensor.l2_aktuatori_energy', 'sensor.l3_spoljnja_jedinica_energy'] %}
{% set entity_state = states[entity] %}
{% if entity_state %}
{% set total_consumption = total_consumption + (entity_state.state | float) %}
{% for entry in entity_state.attributes.history %}
{% set time = as_timestamp(entry.timestamp) %}
{% set value = entry.state | float %}
{% if time | timestamp_custom('%H:%M') >= offpeak_start and time | timestamp_custom('%H:%M') < offpeak_end %}
{% set offpeak_total = offpeak_total + value %}
{% else %}
{% set peak_total = peak_total + value %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
Total Consumption: {{ total_consumption }} kWh Peak Total: {{ peak_total
}} kWh Offpeak Total: {{ offpeak_total }} kWh Total Cost: RSD {{
(peak_total * peak_price) + (offpeak_total * offpeak_price) }}
action: persistent_notification.create