I’m using tibber as my electricity provider and it’s nicely integrated into home-assistant. The component have an attribute with price level that looks like this:
I have a spare heater that I want to run but only when the price is cheap or very_cheap and it’s plugged into a shelly switch.
Whats the best way to do an automation for this, make a trigger for every 1 minute past the hour and check the price level? If it’s cheap or very_cheap turn on the outlet?
If so I guess I need a new automation to turn it off for all the other levels or can I do that in the same automation somehow?
I think it would be better to use the state trigger to trigger the automation when your electricity cost sensor changes state instead of periodically checking it.
You can do that in a single automation using templates. (You could also use the choose action, but it is easier to do with a template.)
trigger:
- platform: state
entity_id: sensor.your_sensor
# trigger on all changes of the attribute
attribute: PriceLevel
action:
- service: >-
{% set is_cheap = is_state_attr('sensor.your_sensor', 'PriceLevel', 'CHEAP') or
is_state_attr('sensor.your_sensor', 'PriceLevel', 'VERY_CHEAP') %}
switch.turn_{{ 'on' if is_cheap else 'off' }}
target:
entity_id: switch.your_switch
So, now I’ve created my own template sensor doing the same calculation but with 1-day average and realized the values for price level can diverge a lot:
I will wait a few days to compare those values over time, but looks like I will have to rethink some of my automations.
Trying to understand these concepts as I have the same need.
So is this a sensor that goes into configuration.yaml? The configuration validator complains about required key not provided @ data[‘platform’]. What platform should be provided for this?
It have to be under Template > Sensor on your configuration.yaml.
Try this: (please remember to rename the entity_id of Tibber sensor for the name your integration is using)
Thanks a lot for sharing!
I am in need for a smarter control of my air-to-water heat pump and have just started to learn Home Assistant.
I first ran the code as is and it works fine.
I have modified the code a bit for my needs, but it does not work as intended… Maybe something with me not defining data types correctly?
My intention is to get rid of the enums (EXPENSIVE, NORMAL, etc) and instead use numbers (integer) so that I can print graphs for easier evaluation. Something like this:
The original Tibber API have this “Price Level” comparing the current value with the average value in the past 3 days. I like that, but for some devices I’d like the same levels, but compared only with the current day, so you will find the 2 sensors with “(1-day)” or “(3-days)” in the name.
The “Combined” will result the worse case between those 2 sensors. Let’s say the price now is “VERY_CHEAP” in the 3-days sensor, but it’s “NORMAL” in the 1-day (this will happen in a day where electricity is cheaper than the previous 2 days). The result of the combined sensor will be “NORMAL”, as this is the worse case between those two.
In the same way, it can be that today is way more expensive than the previous days, so it can happy that at certain time, the 1-day sensor will result in “CHEAP”, but when comparing to the 3-days, that would be “EXPENSIVE”, to the combine will return “EXPENSIVE” (the worse between the 2 other sensors). It will be rarer to have “VERY_CHEAP” in the combined sensor.
I have some devices where I don’t have to turn on every day… let’s say, I have a bench with lots of battery chargers where my electric screwdriver, a AAA-battery charger, my camera’s charger, etc. are all connected to a switch. I wanna those devices to be charging only when the price is really cheap, so I use the combined sensor to find when it was “VERY_CHEAP” on both the 1-day and the 3-day sensors.
Yes, I believe to have numbers as results you should have something like this (it’s ugly, maybe we should hve the values into a variable and then convert to int later):
Note that I’ve converted “unknown” to “-1”, just because I don’t know how the “unknown” will affect your plot.
Try it and let me know. I can play a bit with this later.
My next steps will be to adapt the code to my CTC heater, which only takes 4 levels as input. Also, I want to add variables so it’s easier to adjust each levels threshold. Something like this in dashboard:
Create a new automation in the UI. In the top right corner, click the overflow menu and select “Edit in YAML”. Now paste the example code I provided, replacing existing trigger: and action:. You should then be able to switch back to UI mode.