Energy price calculation incorrect?

Hi

Im on Octopus flux so we have 3 rates for import, and 3 for export per day

I’ve set these rates up as a sensor:

octopus_flux_import:
        friendly_name: Octopus Flux Import
        unit_of_measurement: GBP/kWh
        value_template: >
          {% set tariff = { "Peak": 0.4581, "Day": 0.3272, "OffPeak": 0.1963 } %}
          {% set time = { "month": (now().strftime('%m') | int), "hour": (now().strftime('%H') | int), "weekday": (now().weekday() | int ) } %}
          {%if (time.hour >=2) and (time.hour <5) %}
            {{ tariff.OffPeak }}
          {%elif (time.hour >=16) and (time.hour < 19) %}
            {{ tarriff.Peak }}
          {% else %}
            {{ tariff.Day }}
          {% endif %}

octopus_flux_export:
        friendly_name: Octopus Flux Export
        unit_of_measurement: GBP/kWh
        value_template: >
          {% set tariff = { "Peak": 0.3481, "Day": 0.2172, "OffPeak": 0.0863 } %}
          {% set time = now()  %}
          {%if (time.hour >=2) and (time.hour <5) %}
            {{ tariff.OffPeak }}
          {%elif (time.hour >=16) and (time.hour < 19) %}
            {{ tarriff.Peak }}
          {% else %}
            {{ tariff.Day }}
          {% endif %}


All seems good I can see the sensors in my dashboard which are correct

However the daily cost on the energy dashboard is incorrect

For example today all our export was between 10am and 8pm.

So therefore either 21.72p or 34.81p

However the dashboard shows 59.77kWh exported at a “cost” of -£7.43

That averages 12.4p. Which is far too low. It should probably average around 30p.

Import averages 21p which I think is correct as we only import during the off peak period.

How can I debug this further?

Ps both sensors were using the time variable shown under import. I just changed the export to simplify it but that difference has only just occurred so won’t explain the discrepancy in costs today.

I think when you dont have your own total cost entity HA creates one. I can’t recall it’s name but try searching your entities page for “cost” or “consumption” or similar and see if you can locate them. Might provide some guidance on how that number has changed over the day and point you to the issue

1 Like

I’ve managed to find the issue. Id spelt “tariff” wrong on the Peak time. So for 3 hours it wasn’t adding any cost!

Thanks.

How/where do you create this sensor for this to work?

Hi, @bensebborn

Thanks for sharing your Flux tariff config. Would you be willing to share more about how you have this configured in addition to the above sensors? I’m still getting my head around templates, sensors and automation etc.

Kind Regards
Chris

Seeing as OP didn’t reply, here’s my fixed version. You just put this block in configuration.yaml and then add the cost sensor in the energy dashboard grid consumption configuration


sensor:
  - platform: template
    sensors:
      octopus_flux_import_cost:
        friendly_name: Octopus Flux Import Cost
        unit_of_measurement: GBP/kWh
        value_template: >
          {% set tariff = { "Peak": 0.4301, "Day": 0.3072, "OffPeak": 0.1843 } %}
          {% set time = { "month": (now().strftime('%m') | int), "hour": (now().strftime('%H') | int), "weekday": (now().weekday() | int ) } %}
          {%if (time.hour >=2) and (time.hour <5) %}
            {{ tariff.OffPeak }}
          {%elif (time.hour >=16) and (time.hour < 19) %}
            {{ tariff.Peak }}
          {% else %}
            {{ tariff.Day }}
          {% endif %}

      octopus_flux_export_cost:
        friendly_name: Octopus Flux Export Cost
        unit_of_measurement: GBP/kWh
        value_template: >
          {% set tariff = { "Peak": 0.3201, "Day": 0.1972, "OffPeak": 0.0743 } %}
          {% set time = { "month": (now().strftime('%m') | int), "hour": (now().strftime('%H') | int), "weekday": (now().weekday() | int ) }  %}
          {%if (time.hour >=2) and (time.hour <5) %}
            {{ tariff.OffPeak }}
          {%elif (time.hour >=16) and (time.hour < 19) %}
            {{ tariff.Peak }}
          {% else %}
            {{ tariff.Day }}
          {% endif %}

Hi amelluk,

Thanks for this code, which I have added to my HomeAssistant setup. After a few days, HA is showing numbers for the Import and Export in the energy dashboard. However, both Import and Export Energy and Cost figures are negative. I would have thought one or other should be positive?

I notice that the tariff rates you’ve entered above (which I’ve adjusted for my current Flux tariff) are all positive when surely the Export Cost should be negative?

Apologies if I’ve misunderstood anything! I’m still an HA newbie…

Thanks for any suggestions.

David