Find the minimum energy cost in allowed timespan

Hi there,

my washing machine has been running about fine with the setup I patched together here: Find time to start device (washing machine) from price and time shedule

Now Tibber has switched to 15 Min updates, and additionally I happens that the price minimum often happens to be just before 7am - which I defined to be outside the allowed times (I like to sleep in at the weekends, and I wouldn’t want my neighbours waking up to the sound of my washing machine loading water :D)

So I’m trying to find the perfect time:

  • the minimal energy cost WITHIN the predefined timespan of the day…

  • First step would be to calculate it once per day.

  • But later I want to integrate it into my existing automation:

alias: WaMa_on
triggers:
  - entity_id:
      - sensor.electricity_price_viktoriastr_94
    for:
      minutes: 3
    trigger: state
conditions:
  - condition: state
    entity_id: schedule.wama_allowed
    state: "on"
  - condition: or
    conditions:
      - condition: template
        value_template: >-
          {{ state_attr('sensor.electricity_price','next_16h_min_price') ==
          state_attr('sensor.electricity_price','current').total }}
      - condition: state
        entity_id: sensor.electricity_price
        attribute: is_at_min
        state: "True"
      - condition: state
        entity_id: sensor.electricity_price
        attribute: price_level_combined
        state: Cheap

So I’d try to replace the conditions that check for the 16h_min_price or overall (I think 4-5 day) minimum that comes from the energy provider via the REST API.

What I’m missing is how to start:

What entity would I use? Or just try to script via template?

How to get the starting and end times from the schedule in script?
There’s the get_schedule action, but I don’t know how I use that in template/script environment?

action: schedule.get_schedule
target:
  entity_id: schedule.wama_allowed
data: {}

I can copy the result into the templates for now…

How do I access the current price (for comparison and as starting point)?
state_attr("sensor.electricity_price","current") will give me

'total': 0.3304, 'startsAt': '2025-11-09T16:30:00.000+01:00', 'level': 'NORMAL'

Is there a built-in way to narrow down to “total” and “startsAt” ?
Or am I gonna need to divide it with regex or similar things?

How do I access all the future values? (REST Api sensor…)
I’ll need to to it more, because `state_attr(“sensor.electricity_price”,“future”) brings me an array of json objects:

[{'total': 0.3304, 'startsAt': '2025-11-09T16:30:00.000+01:00', 'level': 'NORMAL'}, {'total': 0.3326, 'startsAt': '2025-11-09T16:45:00.000+01:00', 'level': 'NORMAL'}, {'total': 0.3307, 'startsAt': '2025-11-09T17:00:00.000+01:00', 'level': 'NORMAL'} (...) ]

How do I access all the future values starting and ending with the allowed times?
I think this would explain itself after solving the above, if I can read this array in a way that I can look for the timestamp, I have multiple options:

  • iterate from current timestamp, adding 15min and searchign for that timestamp in the array and comparing the “total” value, and if it is lower note down both (total and timestamp)
  • cut off all future values after the timestamp that comes from `state_attr(“schedule.wama-allowed”, “next_event”) IF schedule currently returns On

I’d be happy if someone gives me some hints on how to tackle this, I think this is mostly scripting related.

PS: Note for myself: check if the washing machine heats for more than 15 minutes and if yes, find out if it is worth averaging the energy cost of 2 neighbouring 15 minute intervals…