Hi,
I am trying to create some automation for energy saving. My idea is to create an automation to charge my car in the cheapest hours.
My idea is to create 3 helpers that are updated daily with the cheapest, second cheapest and third cheapest price the next 12 hours. And the program the car to charge in these hours.
But I run in to problems with this and I hope that there might be some one in here that see the issue.
the source for the prices are an integration with the following output:
raw_today:
- hour: ‘2023-09-26T00:00:00+02:00’
price: 2.35 - hour: ‘2023-09-26T01:00:00+02:00’
raw_tomorrow: - hour: ‘2023-09-27T00:00:00+02:00’
price: 2.27 - hour: ‘2023-09-27T01:00:00+02:00’
price: 2.237
The helpers that I want to update are:
input_datetime.cheapest_hour_1
input_datetime.cheapest_hour_2
input_datetime.cheapest_hour_3
Since it has to get information from two attributs it seem that an Automation could not do the trick. So I tried to create a Python-script to do the trick. But it simply doesn’t update the helper. I dont know much about Python, so I hope that some of you guys see an error in my script?
# Calculate and set the three cheapest hours between now and tomorrow at 06:00
# Get the current hour
now_hour = datetime.now().hour
logger.log("new_hour")
# Extract the price data
today_prices = hass.states.get("sensor.elpriser_inkl_afgifter").attributes["raw_today"]
tomorrow_prices = hass.states.get("sensor.elpriser_inkl_afgifter").attributes["raw_tomorrow"]
# Combine today and tomorrow's prices
combined_prices = today_prices + tomorrow_prices
# Debug: Log the combined price data
logger.log("Combined Price Data:")
for entry in combined_prices:
logger.log(f"Hour: {entry['hour']}, Price: {entry['price']}")
# Filter the prices for hours between now and tomorrow at 06:00
filtered_prices = [entry for entry in combined_prices if int(entry["hour"][11:13]) >= now_hour]
# Debug: Log the filtered price data
/config/logger.log("Filtered Price Data:")
for entry in filtered_prices:
/config/logger.log(f"Hour: {entry['hour']}, Price: {entry['price']}")
# Sort the filtered prices by price
sorted_prices = sorted(filtered_prices, key=lambda x: x["price"])
# Debug: Log the sorted price data
/config/logger.log("Sorted Price Data:")
for entry in sorted_prices:
/config/logger.log(f"Hour: {entry['hour']}, Price: {entry['price']}")
# Get the three cheapest hours
cheapest_hours = [entry["hour"] for entry in sorted_prices[:3]]
# Debug: Log the three cheapest hours
/config/logger.log("Three Cheapest Hours:")
for hour in cheapest_hours:
/config/logger.log(hour)
# Set the input_datetime helpers
hass.services.call("input_datetime", "set_datetime", {
"entity_id": "input_datetime.cheapest_hour_1",
"time": cheapest_hours[0],
})
hass.services.call("input_datetime", "set_datetime", {
"entity_id": "input_datetime.cheapest_hour_2",
"time": cheapest_hours[1],
})
hass.services.call("input_datetime", "set_datetime", {
"entity_id": "input_datetime.cheapest_hour_3",
"time": cheapest_hours[2],
})