Trigger an Action on Specific Date & Time

What about this? And why not this if the previous string conversion example also works?

{{ now().strftime("%Y-%m-%d") == "2017-11-01" }}

One seems to be a filter and the other a function? I don’t know this stuff much…

2 Likes

I like this idea - will hopefully allow me to do something like this:

{{ now().strftime("%m-%d") == "10-03" }}

So I can run an action on the same time, same day every year :smile:

3 Likes

I came across this post when looking for a solution to my requirement.

I want to setup a list of month/days on which to trigger an event, in this case it is the refuse disposal schedule for a 1 year period. The dates are NOT always previous date + 7 days, so I was thinking to use an array something like this:

[[1,7,22],[1,14,22],[1,20,22],[1,28,22],[2,5,22],[2,12,22],.........[12,27,22]

Is this the best way of doing this, and if so how would I go about coding it, given that this is a ‘pseudo’ array ?

Where do you get the dates from to begin with.
Perhaps there is a way to skip the manual work completely?

But that could work, only thing I would add is an empty array at the beginning to make january [1] not [0].
Another option which will be easier to code in HA would be if the format is [“2023-01-01”, “2023-01-07”, … “2023-03-20”…]

I wanted to have an Automation that ran just once to update my Electricity tariffs. From the advice above, this is what I ended up with.

alias: Electricity Rate Change
description: ""
trigger:
  - platform: time
    at: "00:01:00"
condition:
  - condition: template
    value_template: "{{ now().date() | string == \"2024-04-01\" }}"
action:
  - service: input_number.set_value
    target:
      entity_id: input_number.electricity_day_rate
    data:
      value: 0.62726
  - service: input_number.set_value
    target:
      entity_id: input_number.electricity_kwh_rate
    data:
      value: 0.23838
  - delay:
      hours: 0
      minutes: 0
      seconds: 10
      milliseconds: 0
  - service: automation.turn_off
    target:
      entity_id: automation.electricity_rate_change
    data:
      stop_actions: false
mode: single

The Automation will trigger every day at 1 minute past midnight but then not get any further until the date in the Condition Template is reached. It then updates the rates, waits 10 seconds and then disables itself. I can reactivate it when I get notice of the next rate change.
Thanks

Your condition looks fine syntactically, but you can use single quotes within double quotes and avoid the escaping. Of course it’s not 1st April, so what was that string the last time it was supposed to run and what did the trace show? As is, it shouldn’t run.

You will need to update the date when you get a tarrif change, so an easier method would be to use an input date time helper instead of the hard-coded date in the condition.

That’s how it came out when I went from the GUI editor to YAML.

Presumably, you mean it shouldn’t run until 1 Apr 24. I’ve never run it so am waiting till 1 Apr when I will check. If it doesn’t work I’ll come back.

Don’t know what you’re getting at here. This seems pretty simple to me, I mean, I can understand it! Just edit the date and values in the automation and re-enable it.

If the goal is only allow it to execute its actions on April 1st every year, you can do it like this (without the need to manually modify the year annually).

condition:
  - condition: template
    value_template: "{{ (now().month, now().day) == (4, 1) }}"

No, the tarrifs can change anytime and several times a year. I just wanted a way so I could ensure they got updated at the right time without me having to remember. With the Automation, I can just go in and edit it (re-enable, date and new tarrifs) as soon as I get a notification.

In that case I agree with parautenbach that exposing this date in the UI, as an Input Datetime, can improve visibility and convenience. FWIW, I created a “configuration page” in the UI where several Input Datetimes and Input Booleans are used to control the behavior of various automations. I find it easier to manage centralized controls.

Nonetheless, it’s your choice as to what you find more efficient for your needs.

1 Like

I think I see what you both are getting at. I could create a panel on a dashboard for entering the new tariffs and the date they start. I like a challenge so will give it a go.
At the end of the day though, I’m normally sitting at my computer with the email telling me of an impending rate change rather than standing in front of my wall mounted panel in the hall.

1 Like

Sounds like you need a tab open to your HA in your web browser :wink: I have a pinned tab in my “main” browser so it’s easier to do stuff in my HA.

And when you’re up for it, you can even automate this (probably), but first steps first.

OK I bowed to the pressure and created 3 new input number helpers to hold the date and the 2 new values. Then added an Entities card to my Dashboard


followed by an update to the Automation.

alias: Electricity Rate Change
description: ""
trigger:
  - platform: time
    at: "00:01:00"
condition:
  - condition: template
    value_template: >-
      {{ states('input_datetime.electricity_rate_change_date') ==
      now().strftime('%Y-%m-%d') }}
action:
  - service: input_number.set_value
    target:
      entity_id: input_number.electricity_day_rate
    data:
      value: "{{ states('input_number.electricity_upcoming_day_rate') }}"
  - service: input_number.set_value
    target:
      entity_id: input_number.electricity_kwh_rate
    data:
      value: "{{ states('input_number.electricity_upcoming_kwh_rate') }}"
mode: single

In light of what you posted in another topic (ability to programmatically disable a trigger), if you want it to trigger at a specific time on a specific date then I suggest you simply use Local Calendar, to store the date and time, and a Calendar Trigger to trigger the automation.

Yes you’re right, that would overcome my minor worry. I don’t think I’ve got that integration. Before I go down that route would I still be able use the Entities GUI Card to enter the date?

Edit:
I had a look and installed Local Calendar, but found I had to create an ‘update’ event to trigger the automation in the Calendar panel, then enter the new rates separately on my GUI card. I’d prefer to keep everything in one place ie the GUI card I showed above.

If, for testing purposes, you scheduled an Calendar event occuring within 15 minutes, you would need to force a refresh otherwise it shouldn’t be necessary.

A Calendar event’s description field can be used to store related data (such as the new rates). No separate helpers are needed.

Anyway, you now have at least two different ways to achieve your goal and can choose whichever one suits you best.

I’ve no idea how i would extract the new rates from the description field of the event. I tried googling it but no success.

Easiest is to simply use a space to separate the two values.

0.62726 0.23838

The Calendar Trigger will generate a trigger variable and the data you put in the event’s description field will be in trigger.calendar_event.summary. (No idea why it’s called summary instead of description but there it is).

You can extract the two values like this:

{% set day_rate, kwh_rate = trigger.calendar_event.summary.split() | map('float', 0) %}

OK. I’ve probably gone overboard but it’s been a learning experience. I’ll post here in case it helps others.

I wanted to create something that would update my Electricity Day and kWh rates on a particular date as soon I get notification of a change from my supplier; then I can forget about it! A Calendar entry seemed to work and I started out with an automation that run every day checking if the event had been reached. I didn’t like that as I wanted to trigger the automation just the once. 123 Taras suggested a Calendar entry with the data in it and a Calendar trigger for the Automation. I wanted a nice Front-End for entering the data via a dashboard.
This is what I’ve now got (it may still not be perfect).
Three Helpers -

input_datetime:electricity_rate_change_date
input_number.electricity_upcoming_day_rate
input_number.electricity_upcoming_kwh_rate

A Script that reads these 3 helper and creates a calendar event, writing the new rates into the description field in the format 0.12345 0.98765.

alias: Commit Electricity Tariff Change
sequence:
  - service: calendar.create_event
    data:
      summary: Electricity Rate Change
      description: >-
        {{states('input_number.electricity_upcoming_day_rate')}}
        {{states('input_number.electricity_upcoming_kwh_rate')}}
      start_date_time: "{{states('input_datetime.electricity_rate_change_date')}} 00:01:00"
      end_date_time: "{{states('input_datetime.electricity_rate_change_date')}} 00:02:00"
    target:
      entity_id: calendar.calendar_triggers
mode: single

A GUI Dashboard Card where I can enter the details and commit the Event.

And an Automation that gets triggered by the event. It reads the new rates from the description field (ie not the helpers).

alias: Calendar Trigger
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "0:0:0"
    entity_id: calendar.calendar_triggers
condition:
  - condition: template
    value_template: "{{ trigger.calendar_event.summary == 'Electricity Rate Change' }}"
action:
  - service: input_number.set_value
    target:
      entity_id: input_number.electricity_day_rate
    data:
      value: "{{ trigger.calendar_event.description.split()[0] | float }}"
  - service: input_number.set_value
    target:
      entity_id: input_number.electricity_kwh_rate
    data:
      value: "{{ trigger.calendar_event.description.split()[1] | float }}"
mode: single

It’s not perfect; you can create multiple events if you press the RUN button over and over. I might update the script to clear the form (the helper entities) and I would have like to have the form in a pop-up dialogue box, but that’s another rabbit hole.
Hope that’s some help.