Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automation trigger lowest price or highest price of the day #18

Open
jayaarts opened this issue Aug 2, 2022 · 11 comments
Open

Automation trigger lowest price or highest price of the day #18

jayaarts opened this issue Aug 2, 2022 · 11 comments

Comments

@jayaarts
Copy link

jayaarts commented Aug 2, 2022

Is it possible to create an automation to, for example, receive a notification if the price becomes the lowest (or highest) of the day?

@Tazzios
Copy link

Tazzios commented Aug 7, 2022

You can do that with the build HA automation. You can compare current price with lowest and highest. As an action you can then send a notification. (sorry no example)

@jayaarts
Copy link
Author

jayaarts commented Aug 9, 2022

When I try to do this:
{{ states('sensor.current_electricity_price_all_in') | float == states('sensor.lowest_energy_price_today') | float }}

I get an error:
template value should be a string for dictionary value @ data['value_template']. Got None

Sorry, I'm new and helpless knowledge; trying to learn.

@bajansen
Copy link
Owner

bajansen commented Oct 9, 2022

The described template should work and it should also work without the casts to float. The lowest_energy_price_today value is retrieved from the same array as the current all-in price so the values should also be an exact match.

For me, the template {{ states('sensor.current_electricity_price_all_in') == states('sensor.lowest_energy_price_today') }} returns true or false as expected. Maybe the error occurs when the values have not yet been instantiated, such as when Home Assistant has just started.

@HiDiHo01
Copy link
Contributor

HiDiHo01 commented Oct 18, 2022

I have created a binary sensor for this in configuraton.yaml

  - binary_sensor:
      - name: Happy hour
        device_class: running
        state: >
          {{ (states('sensor.current_electricity_price_all_in') | round(4,default=0) == states('sensor.lowest_energy_price_today') | round(4,default=1)) | default('off') }}

For my plugs (eg charging phone) I use this automation. It also plays a message on all my speakers

- id: '1651974863090'
  alias: Philips plug aan bij laagste tarief
  trigger:
  - platform: state
    entity_id:
    - binary_sensor.happy_hour
    from: 'off'
    to: 'on'
  condition: []
  action:
  - type: turn_on
    device_id: d7ea19752753e08adbc9baa5128b75fa
    entity_id: switch.philips
    domain: switch
  - service: tts.zeg
    data:
      entity_id: media_player.huis
      message: De elektriciteitprijs is nu het laagst
      cache: true
  mode: single

@TFhdKi95ae8L
Copy link

TFhdKi95ae8L commented Dec 23, 2022

For me the binary sensor mentioned above did not work in my configuration.yaml.
This one did:

- platform: template
  sensors:
    happy_hour:
      unique_id: happyhour
      value_template: "{{ (states('sensor.current_electricity_price_all_in') | round(4,default=0) == states('sensor.lowest_energy_price_today') | round(4,default=1)) | default('off') }}"

@TFhdKi95ae8L
Copy link

The code mentioned above works, but only for today.
Now is the cheapest hour for today (2300), but in a few hours it'll be tomorrow and the prices are even lower (0300).
Any tips on how to account for that?

@corneyl
Copy link
Collaborator

corneyl commented Dec 28, 2022

I think it's better to do something with the prices attributes:

{% set endtime = (state_attr('sensor.current_electricity_price_all_in', 'prices') | 
    selectattr('till', 'gt', now()) | 
    min(attribute='price')
  ).get('till') %}
{{ timedelta(hours=0) < endtime - now() < timedelta(hours=1)  }}

This should be true when the end time (till-datetime) of the hour with the lowest prices is within one hour, so we're currently at the lowest price.

@HiDiHo01
Copy link
Contributor

I think it's better to do something with the prices attributes:

{% set endtime = (state_attr('sensor.current_electricity_price_all_in', 'prices') | 
    selectattr('till', 'gt', now()) | 
    min(attribute='price')
  ).get('till') %}
{{ timedelta(hours=0) < endtime - now() < timedelta(hours=1)  }}

This should be true when the end time (till-datetime) of the hour with the lowest prices is within one hour, so we're currently at the lowest price.

Dit werkt niet bij mij....geeft altijd True aan
Zo werkt ie wel

{% set endtime = (state_attr('sensor.current_electricity_price_all_in', 'prices') |
min(attribute='price')
).get('till') %}
{{ timedelta(hours=0) < endtime - now() < timedelta(hours=1) }}

@macdivernl
Copy link

macdivernl commented Jun 29, 2024

For me the binary sensor mentioned above did not work in my configuration.yaml. This one did:

- platform: template
  sensors:
    happy_hour:
      unique_id: happyhour
      value_template: "{{ (states('sensor.current_electricity_price_all_in') | round(4,default=0) == states('sensor.lowest_energy_price_today') | round(4,default=1)) | default('off') }}"

Works for me Tnx! State is “False” or “True”. (Spelling incl. caps is crucial for proper functioning)

  • CONFIG -
    Core v2024.6.4
    Supervisor v2024.06.2
    Operating System v12.4
    Frontend v20240610.1
    Frank Energie v3.0.0

@macdivernl
Copy link

How would you create an extra binary sensor called e.g. “below zero” to have a longer period available in which interesting rates can be used? In my condig “Happy Hour” is one hour only

@DCSBL
Copy link
Collaborator

DCSBL commented Jul 4, 2024

Maybe you can use a 'Threshold sensor': https://www.home-assistant.io/integrations/threshold/. Create one in 'helpers'

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants