Need recommendation for YAML Automation trigger using input_timedate value

I’m trying to create an automation(s) based on the value of input_timedate.future_datetime value. I believe this can be accomplished with an automation template trigger; however, I am failing to determine the YAML syntax. (Another automation successfully sets a critical future date and time into the helper.) using now() as the consistent comparison variable… I believe my quest will be preceded by either…

trigger:
- platform: template
    value_template: {{now().strftime("%Y-%m-%d %H:%M:%S")}} > {{ (state_attr('input_datetime.g6_sensor_timer', 'timestamp')) | timestamp_custom(%Y-%m-%d %H:%M:%S) }}

but I am sure there is some sort of syntax or template error.
My goal is to have three trigger(s)

  1. When current date/time is > input_timedate.future_datetime
    Modified for one day earlier at 09:00.
  2. When current date/time is > input_timedate.future_datetime
    Modified for one day of but 1 hour earlier.
  3. When current date/time is = or > input_timedate.future_datetime

I have been using the DeveloperTools/Template for the successful modification of input_timedate.future_datetime or now() but
nothing seems to work, other than

{{now().strftime("%Y-%m-%d %H:%M:%S")}}

which changes the result of now() precisely matches the result of

{{ (state_attr('input_timedate.future_datetime', 'timestamp')) | timestamp_custom() }}

Does anyone have any ideas or clarification questions?

just use a time trigger

- platform: time
  at: input_datetime.g6_sensor_timer

If you’re dead set on offsetting that time, I recommend making timestamp sensors and using them in your triggers.

template:
- sensor:
  - name: g6_sensor_timer_1_hour_before
    device_class: timestamp
    state: "{{ states('input_datetime.g6_sensor_timer') | as_datetime | as_local - timedelta(hours=1) }}" 
  - name: g6_sensor_timer_night_before
    device_class: timestamp
    state: "{{ states('input_datetime.g6_sensor_timer') | as_datetime | as_local - timedelta(hours=24) }}" 

Convert the Input Datetime’s value to a datetime object and then you can use timedelta to easily offset it and compare it directly with the value of now().

Copy-paste the following examples into the Template Editor and experiment with them.

{% set dt = states('input_datetime.future_datetime') | as_datetime | as_local %}
{{ now() >= dt }}
{{ now() >= dt - timedelta(days=1) }}
{{ now() >= dt - timedelta(hours=1) }}

EDIT

Correction. As per finity’s post below, it’s input_datetime not input_timedate.

you’ve fallen into a trap that the OP set for you. :grinning_face_with_smiling_eyes:

that needs to be:

{% set dt = states('input_datetime.future_datetime') | as_datetime | as_local %}
1 Like

@123 your example works great in the Template Editor! Now, my question would be how would I put that into a YAML automation trigger?

Also, I want to say special thanks to @petro @finity. I can not express enough for your assistance!!

@petro
What am I doing wrong. I don’t get any errors; however, the automation trigger does ant activate.

trigger:
- platform: sensor
  template:
  - sensor:
    - name: g6_sensor_timer_1_hour_before
      device_class: timestamp
      state: "{{ states('input_datetime.g6_sensor_timer') | as_datetime | as_local - timedelta(hours=24) }}" 

what I posted creates a sensor. It’s not a trigger for an automation. You place the snippit into your configuration.yaml file and restart. YOu’ll have 2 additional sensors on your system. Then you simply list out those time triggers pointing at those entities. Your trigger will not contain a template.

This one triggers 1 hour before the time specified by input_datetime.future_datetime.

trigger:
  - platform: template
    value_template: >
      {% set dt = states('input_datetime.future_datetime') | as_datetime | as_local %}
      {{ now() >= dt - timedelta(hours=1) }}

For your consideration:

I use input_datetime to control irrigation. I set up an irrigation page and have 6 times in a day to water the lawn(short to minimize runoff).

- id: '1627491913964'
  alias: Water Lawn with repeat
  description: Water all zones with repeat
  trigger:
  - platform: time
    at:
    - input_datetime.cycle1
    - input_datetime.cycle2
    - input_datetime.cycle3
    - input_datetime.cycle4
    - input_datetime.cycle5
    - input_datetime.cycle6
  condition:
  - condition: state
    entity_id: sensor.watering_days
    state: '0'
  - condition: state
    entity_id: input_boolean.run_irrigation
    state: 'on'
  action:
  - repeat:
      count: '11'
      sequence:
      - variables:
          zone: switch.zone_{{ repeat.index }}
      - service: switch.turn_on
        target:
          entity_id: '{{ zone }}'
      - delay:
          minutes: '{{ states(''input_number.zone'' ~ repeat.index) | int }}'
      - service: switch.turn_off
        target:
          entity_id: '{{ zone }}'
  mode: single

My irrigation page looks like this:

Instead of offsets everything is explicit and can be modified easily.

1 Like

I’m in the process of using template sensors.
Can you tell me if it possible to test the results of a template,
if the result is negative, add 60 to it, if it is positive keep it.
Funny how doing math with time, direct answers may need to be modified.