How to use a datetime helper in an automation that needs the date and time

Hi,
I am still learning my way round HA, but loving its capability.

I want to be able to set a date and time in the future to automate an action (inhibiting the hot water heater when away on holiday).

I have created a datetime helper, which collects the date and time fine.

The problem is how to include the date in an automation. I can use the ‘time’ condition in the automation UI, but that just looks at the time component of the datetime helper, and doesnt check whether the date has passed. I cant see how to get the date checked a a condition. Does it need to be done in YAML, or do I need to use a template?

I have dug around in the documentation and other questions on this forum, and havent found an answer.

Thanks for your help
Jon

Can’t you have the datetime as the trigger?

From what you have described, you could use two Input datetime helpers (start & end) and then create a Template binary sensor:

template:
  - binary_sensor:
      - name: "On Vacation"
        state: >
          {{ states('input_datetime.vacation_start') | as_datetime | as_local <= now() 
          < states('input_datetime.vacation_end') | as_datetime | as_local }}

This will give you a sensor called binary_sensor.on_vacation that will have a state of “on” when the current time and date is between your start and end datetimes, otherwise it will be “off”. Then, in you automation(s), you add a State condition:

condition:
  - condition: state
    entity_id: binary_sensor.on_vacation
    state: 'off'

EDIT: Corrected typo in template

1 Like

Drew, hi,
Thanks. I think this will do what I’m looking for. Really useful.
Cheers
Jon

Im trying to use this code above but get a TemplateSyntaxError: Encountered unknown tag ‘states’.
My code

template:
  - binary_sensor:
      - name: "Its Easter"
        state: >
          {% states('input_datetime.easter_start') | as_datetime | as_local <= now() 
          < states('input_datetime.easter_end') | as_datetime | as_local %}

I also don’t understand why when using this in automations it only pulls the time and not the date from he input_datetime entity


ui automation code

alias: turn holiday switches on and off
description: ""
trigger:
  - platform: time
    at: input_datetime.easter_start
    id: easter starts
  - platform: time
    at: input_datetime.easter_end
    id: easter ends
  - platform: state
    entity_id:
      - input_select.house_mode
    to: Home
condition: []
action:
  - if:
      - condition: and
        conditions:
          - condition: time
            after: input_datetime.easter_start
            before: input_datetime.easter_end
    then:
      - service: switch.turn_on
        data: {}
        target:
          entity_id: switch.easter
    else:
      - if:
          - condition: and
            conditions:
              - condition: not
                conditions:
                  - condition: time
                    after: input_datetime.easter_start
                    before: input_datetime.easter_end
        then:
          - service: switch.turn_off
            data: {}
            target:
              entity_id: switch.easter
mode: single

There’s a typo in the posted Solution.

In the template, replace:

{% with {{

%} with }}

1 Like

Awesome I will try that, thank you!

I am trying to do similar to this. I cannot get it to work, I am using various calculations and it feels like input_datetime (only using time element) is not a properly formatted time? athough the developer tools show it as 08:39:09 for example…

binary_sensor:
  - platform: template
    sensors:
      actual_charging_window_active:
        friendly_name: "Actual Charging Window Active"
        value_template: >
          {{ states('input_datetime.charge_window_start') | as_datetime | as_local <= now() 
          and now() <= states('input_datetime.charge_window_end') | as_datetime | as_local }}

      calculated_charging_window_active:
        friendly_name: "Calculated Charging Window Active"
        value_template: >
          {{ states('sensor.calculated_charge_start_time') | as_datetime | as_local <= now() 
          and now() <= states('input_datetime.charge_window_end') | as_datetime | as_local }}

I’ve tried various ways of doing this then hit a brick wall with errors like TypeError: ‘<=’ not supported between instances of ‘str’ and ‘datetime.datetime’

I think I got it working…

What a strange arrangement with the time

  charge_window_active:
    friendly_name: "Charge Window Active"
    value_template: >-
      {{ now().time() >= strptime(states('input_datetime.charge_window_start'), '%H:%M:%S').time() and 
        now().time() <= strptime(states('input_datetime.charge_window_end'), '%H:%M:%S').time() }}

  calc_charge_window_active:
    friendly_name: "Calc Charge Window Active"
    value_template: >-
      {{ now().time() >= strptime(states('sensor.calculated_charge_start_time'), '%H:%M:%S').time() and 
        now().time() <= strptime(states('input_datetime.charge_window_end'), '%H:%M:%S').time() }}