T3m3z / spotprices2ha

Simple copy-paste approach to fetch data from api.spot-hinta.fi (see https://spot-hinta.fi) to Home Assistant. Includes simple sensors and UI elements to ease automation work.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ranking the hourly prices is not taking Price1 and Price2 into account

ville-juhani-honka opened this issue · comments

Looks like that the rank value is not working properly together with day/night time tariffs on the days, when the spot prices are very low (well, probably it does not work with the higher prices either, but the bug becomes obvious with low spot prices).

I've set up the day/night time tariffs with Price1 and Price2 like this:
image

This is October 7, 2023, spot prices have been record-breaking low the whole day (shown prices include my day/night time tariffs):
image

When writing this, the time is between 22-23, so the rank should be below all the daytime hours between 7-22, however I get this rank:
image
So, looks like that the ranking is not taking the different day/night time tariffs into account at all, but only the spot prices.

Hello! Yes, that is right. Right now the code doesn't calculate Ranks and just takes directly from api.spot-hinta.fi and therefore, the Price1/Price2 modifiers don't have an effect.

This is something that I have known about but I haven't fixed it.

Would it be OK for you to test following code? If it works, I could add it to the main branch of this repository.

So search for lines 50-55 on file spot-price.yaml and you should be able to find definition of SHF Rank now from there. Replace those lines with the following code and see whether it works for you. Please report all findings back to here.

  - sensor:
    - name: SHF Rank now
      unique_id: shf_electricity_rank_now
      unit_of_measurement: Rank
      availability: '{{ state_attr("sensor.shf_data", "data") | selectattr("Timestamp", "lt", now() | as_timestamp) | list | length > 0 }}'
      state: >
        {% set today = state_attr('sensor.shf_data', 'data') | selectattr("Timestamp", "lt", today_at("23:59") | as_timestamp) | selectattr("Timestamp", "ge", today_at("0:00") | as_timestamp) %}
        {% set n = state_attr('sensor.shf_data', 'data') | selectattr("Timestamp", "eq", now().replace(minute=0, second=0, microsecond=0) | as_timestamp) | first %}
        {{ (today | sort(attribute="TotalPrice")).index(n)}}

Awesome, thanks. I took the code in use and right away the ongoing hour got the correct rank.

I will follow how it works in the upcoming days and report back here (I don't want to test by changing Price1 and -2 on the fly as the following rank update might trigger other things in my heating setup).

@T3m3z , I think something in the code is not working; at the moment, with today's spot prices and my Price1 and -2 values, the current hour (18-19) should be rank 21. The three more expensive hours being 19-20, 20-21 and 21-22. After 22, my Price1 kicks in and makes 22-23 and 23-24 again cheaper.

However, the new code gives me rank 20 at the moment for 18-19.

Oh, I see. The original code returned Rank values between 1 and 24. The code that I gave you returns them between 0 and 23. So this is an offset problem.

This should work:

  - sensor:
    - name: SHF Rank now
      unique_id: shf_electricity_rank_now
      unit_of_measurement: Rank
      availability: '{{ state_attr("sensor.shf_data", "data") | selectattr("Timestamp", "lt", now() | as_timestamp) | list | length > 0 }}'
      state: >
        {% set today = state_attr('sensor.shf_data', 'data') | selectattr("Timestamp", "lt", today_at("23:59") | as_timestamp) | selectattr("Timestamp", "ge", today_at("0:00") | as_timestamp) %}
        {% set n = state_attr('sensor.shf_data', 'data') | selectattr("Timestamp", "eq", now().replace(minute=0, second=0, microsecond=0) | as_timestamp) | first %}
        {{ (today | sort(attribute="TotalPrice")).index(n) + 1 }}

Code updated and now it gives me rank 21 as it should.

FYI, I haven't seen any issues with the rank anymore. I'd consider this as resolved.

Any possibility to add possibility to limit price 2 to affect only on mon-sat during Nov-Mar? (kausisähkö/talviarkipäivä)

Any possibility to add possibility to limit price 2 to affect only on mon-sat during Nov-Mar? (kausisähkö/talviarkipäivä)

Wouldn't it be easier to create an automation that runs by set weekdays in set months at certain times and updates the Price2 value based on one's electricity contract? All the prices are updated automatically after this.

Could be done with automation also. Probably not so hard to impement in the yaml with additional conditions here:
{% if t.hour >= state_attr("input_datetime.shf_price2_start", "hour") and t.hour < state_attr("input_datetime.shf_price2_stop", "hour")%}
perhaps something like this?
{% if (t.month>=11 or t.month<=3) and t.weekday<6 and t.hour >= state_attr("input_datetime.shf_price2_start", "hour") and t.hour < state_attr("input_datetime.shf_price2_stop", "hour")%}
Instead of hard coded numbers you could use helpers?

Hi, is it correct that only SHF Rank now -sensor gives the correct rank including SHF Price 1 & SHF Price 2? So e.g. looking at the ranks of individual hours within SHF Electricity price now -sensor the rank is not expected to follow TotalPrice?

e.g. I have now:

  • Rank: 3
    DateTime: '2024-05-04T03:00:00+03:00'
    PriceNoTax: 0.01618
    PriceWithTax: 0.02006
    Timestamp: 1714780800
    TotalPrice: 0.08186

  • Rank: 1
    DateTime: '2024-05-04T15:00:00+03:00'
    PriceNoTax: 0.01334
    PriceWithTax: 0.01654
    Timestamp: 1714824000
    TotalPrice: 0.09744

    Cheapest hour should be 03-04 based on TotalPrice but Rank: 1 is given to 15-16 as the spot-price is lowest. I don't need the ranks among attributes to be correct, but this was just a bit confusing to me at first so wanted to verify.
    I'll try to check tomorrow whether SHF Rank now still gives the rank based on total price.