Hi, need some help here!
I have a sensor (sensor.elia_sdac_current_price) which contains 96 prices (as attribulte) for the current 24 hours (4 per hour). In the morning I want to know the minimum value in this list, to determine whether I should empty the battery to the grid or keep the power for later use. I have tried a statistics helper but that only works for values in the past and the minimum value most of the time is in the future. In the apexcharts-card that I am using to display the prices I can add a "min" calculation and that works fine, but I can see no way to use that value in an automation.
When does that get updated? Could a template sensor trigger off of that?
Does the data in the prices attribute look like this? (well, hopefully the price isn't this high...)
{% set attr = [
dict(time=(now()+timedelta(seconds=10)).isoformat(),price='123.34'),
dict(time=(now()+timedelta(seconds=20)).isoformat(),price='110.13'),
dict(time=(now()+timedelta(seconds=10)).isoformat(),price='140.56'),
]
%}
Then in a template sensor trigger when that sensor is updated -- or trigger at a time of day:
{% set ns = namespace(entry=none, min=none) %}
{% for item in attr %}
{% set p = item.price | float %}
{% if ns.min is none or p < ns.min %}
{% set ns.min = p %}
{% set ns.entry = item %}
{% endif %}
{% endfor %}
{{ ns.entry }}
Maybe someone has a one-liner, but you have to convert to a float.
I am not sure how often the entity it is updated, but at least twice a day. Around 15:00 the prices for the next day are added and I think just after midnight the prices for the previous day are deleted.
I have no idea how the price attribute looks like and also no idea how to check it.
I am fairly new to HA and still learning.
Hello, thank you for your suggestion. I examined the macro and it is not what I am looking for.
Instead of knowing when the cheap hours are, I want to know the lowest price of the whole range of 96 prices and I am not interested when it occurs. An automation takes care of charging or unloading the battery with respect to the current price. When the lowest price of the current day is above a certain amount, there is no profit in emptying the battery to the grid. Thanks anyway.
So, two things from that image. I was wondering if the state was a timestamp, but it's not, so you cannot trigger on that state change. Anyway, sounds like you want to trigger at some point in time.
Next, as you know, there's an attribute "prices" that is an array/list of dictionaries with a time and price keys.
If you go into Dev Tools -> Template and add this:
You should see that array/list of dictionaries, and one of the keys is price as in your screenshot.
So, set that array to a variable and try the above code:
{% set attr = state_attr('sensor.elia_sdac_current_price', 'prices') %}
{% set ns = namespace(entry=none, min=none) %}
{% for item in attr %}
{% set p = item.price | float %}
{% if ns.min is none or p < ns.min %}
{% set ns.min = p %}
{% set ns.entry = item %}
{% endif %}
{% endfor %}
{{ ns.entry.price }}
And see if that finds the one you are looking for.
Next you paste that into the state part of a triggered template sensor.
Look at the second sensor example here and change the trigger to be a time of day trigger.
That will create a sensor that has what the low price was in that sensor when the template sensor triggered. You will have to add that to your templates.yaml file or to the template: section you your configuration.yaml file. Lots of examples online.
Hopefully that will be enough. If you get stuck show where you got stuck.
{{ state_attr('sensor.elia_sdac_current_price', 'prices') | map(attribute='price') | list | min }}
```gives me the correct (minimum) price, so I made a template helper with that and use the helper in the automation that decides wether to dump the battery to the grid or not.
I am not sure when the template helper is updated, but I hope is it every quarter hour when the current price changes. Will follow up. Thanks everyone!
Oh, btw, you can test these templates in the tools.
And this you will find under settings/developer tools/ templates.
This will also inform you about the update rate: This template updates at the start of each minute. This template listens for the following state changed events: Entity: sensor.frank_energie_prices_current_electricity_price_all_in
{{ state_attr('sensor.elia_sdac_current_price', 'prices') | map(attribute='price') | list | min }}
```gives me the correct (minimum) price, so I made a template helper with that and use the helper in the automation that decides wether to dump the battery to the grid or not.
I am not sure when the template helper is updated, but I hope is it every quarter hour when the current price changes. Will follow up. Thanks everyone!
Update: tested it for some days and it does just what I need it for! Marked "Hellis81'" post as solution. Thank you.