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.
MIT License
111 stars 14 forks source link

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

Closed ville-juhani-honka closed 1 year ago

ville-juhani-honka commented 1 year ago

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.

T3m3z commented 1 year ago

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)}}
ville-juhani-honka commented 1 year ago

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).

ville-juhani-honka commented 1 year ago

@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.

T3m3z commented 1 year ago

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 }}
ville-juhani-honka commented 1 year ago

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

ville-juhani-honka commented 1 year ago

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

tomi-tyyska commented 1 year ago

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

ville-juhani-honka commented 1 year ago

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.

tomi-tyyska commented 1 year ago

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?

odelma commented 6 months ago

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: