Closed fursov closed 1 year ago
Would it be possible for someone to test this change locally? Also wonder how useful this seems?
The automation in the examples folder requires some testing. I'll keep pull request in draft state because of that.
I have an alternative approach using pyscript, i have a service defined like this:
from dateutil.parser import parse
from datetime import timedelta
from datetime import datetime, timezone
@service
def calculate_required_state(number_of_hours=None, on=None, off=None):
"""Calculate required state state based on time"""
log.info(f"Calculating state for {number_of_hours} setting to {on} or {off}")
hours = sensor.average_electricity_price_today.prices_today
hours.sort(key=lambda item: item.get("price"))
on_hours = [parse(h.get("time")) for h in hours[:int(number_of_hours)] ]
# Check if current time is in that time
now = datetime.now(timezone.utc)
time_delta = timedelta(hours=1)
for hour in on_hours:
next_hour = hour + time_delta
if hour <= now <= next_hour:
print(f"{now} is between {hour} and {next_hour}")
domain, name = on["service_name"].split(".")
service.call(domain, name, **on["service_data"])
return
# else turn off
domain, name = off["service_name"].split(".")
service.call(domain, name, **off["service_data"])
With and automation that has this as an action:
service: pyscript.calculate_required_state
data:
number_of_hours: "{{ states.input_number.warmtepomp_minimaal_uuren.state }}"
"on":
service_name: climate.set_temperature
service_data:
temperature: 20.5
entity_id: climate.bdr_thermostat
"off":
service_name: climate.set_temperature
service_data:
temperature: 18
entity_id: climate.bdr_thermostat
That makes it possible to easily set the thermostat to high based on the number of hours it should be on on a day.
the only disadvantage is that you need the pyscript plugin for this.
This makes sense. I did not hear about pyscript for HA before :) I will investigate this option and may be revoke my PR later as not needed.
There's also integration nordpool_diff which can be used to create a custom filter for your specific use case. It supports this integration as a source price sensor.
… usage
If electricity usage assumes that some device should be fed with electricity for a certain time, then it makes sense to find best possible time window where prices sum would be optimal. For example this can be applicable to dishwashers, water heaters, car charging stations.