Testing for current time between input_datetime as time only

I’ve been reading all night and can’t find what I need, or, am totally confused.

It could be both.

I have two input_datetime helpers that are configured as ‘time only’ and hold the start and end time of an event.

I would like to create a on/off template sensor that tells me if the current time is between the two helpers.

There is an attribute of timestamp to the input_datetime helpers, but without a date I guess it’s from the beginning of time so wouldn’t work properly. Is there a way to get a timestamp of now(), but only the time, not the date?

{% set n = (now()) %}
{% set open = (today_at(states('input_datetime.battery_charge_start'))) %}
{% set closed = (today_at(states('input_datetime.battery_charge_finish'))) %}

Open: {{ open }}
now: {{ n }}
close: {{ closed }}

{% if now() > open %}
true
{%- else -%}
false
{%- endif %}
Open: 2023-08-21 23:30:00+01:00
now: 2023-08-21 00:48:18.629932+01:00
close: 2023-08-21 05:30:00+01:00

false

The basic option is to use today_at():

{{ today_at(states('input_datetime.example_1')) > now() > today_at(states('input_datetime.example_2')) }}

Thanks,

I think I need to store the date in the input_datetimes as I have the ‘over midnight’ issue to contend with.

Does the time span always cross midnight?

No, it doesn’t. I should have started with DateTime, but the kit I’m sending the data to only needs time, ill just format for the hours and mins before I send

If the Input datetimes are being updated the same date you need them , you might be able to pull the date from the last_changed property of the entity i.e.

{{ (states.input_datetime.battery_charge_start.last_changed.date() | string 
~' '~states('input_datetime.battery_charge_start') )|as_datetime }}

I think I need to go back to the drawing board on this one…

I’m trying to sync my solar inverter charging window with my energy providers off peak windows, which are dynamic.

The rules I have are:

  • Everyday the off-peak window is 2330hrs - 0530hrs.
  • There may be additional off-peak windows though and I want to take advantage of these by remotely updating the inverters charging window.
  • Additional off-peak windows are received as datetime stamps.
  • The inverter only accepts hh:mm for start and finish.

1 - If the dynamic window is between 2330hrs - 0530hrs, ignore and do not update the inverter. Ensure the inverter continues with 2330 - 0530.
2 - If the dynamic window is outside of 2330 - 0530 then update the inverter with the new window (say, 2000hrs - 2200hrs)
3 - When the dynamic window ends, restore the 2230hrs - 0530hrs window.

You may be right… it seems like there is something overly convoluted. If you want, post the automation so we can see how you have the logic structured.

Appreciated, so this automations “works” in that the inverter gets updated, but I need to make it smarter:

It is triggered when the dynamic charging windows are made available/updated.
I am ignoring the fact that there may be multiple windows, and just working with the earliest one, which is acceptable to me

alias: Sync Inverter with Intelligent Octopus Tariff
description: ""
trigger:
  - platform: state
    entity_id:
      - binary_sensor.octopus_energy_intelligent_dispatching
    attribute: planned_dispatches
condition: []
action:
  - service: mqtt.publish
    data:
      topic: solis2mqtt/write_charging_window_start_hours/set
      payload_template: >-
        {{
        (as_timestamp(state_attr('binary_sensor.octopus_energy_intelligent_dispatching',
        'planned_dispatches')[0].start)|timestamp_custom ('%H')) }}
  - service: mqtt.publish
    data:
      topic: solis2mqtt/write_charging_window_start_mins/set
      payload_template: >-
        {{
        (as_timestamp(state_attr('binary_sensor.octopus_energy_intelligent_dispatching',
        'planned_dispatches')[0].start)|timestamp_custom ('%M')) }}
  - service: mqtt.publish
    data:
      topic: solis2mqtt/write_charging_window_close_hours/set
      payload_template: >-
        {{
        (as_timestamp(state_attr('binary_sensor.octopus_energy_intelligent_dispatching',
        'planned_dispatches')[0].end)|timestamp_custom ('%H')) }}
  - service: mqtt.publish
    data:
      topic: solis2mqtt/write_charging_window_close_mins/set
      payload_template: >-
        {{
        (as_timestamp(state_attr('binary_sensor.octopus_energy_intelligent_dispatching',
        'planned_dispatches')[0].end)|timestamp_custom ('%M')) }}
  - service: notify.mobile_app_nick_iphone_13
    data:
      title: Solis Charge Synced with Octopus
      message: >-
        Smart Charge window is {{
        (as_timestamp(state_attr('binary_sensor.octopus_energy_intelligent_dispatching',
        'planned_dispatches')[0].start)|timestamp_custom ('%H:%M')) }} - {{
        (as_timestamp(state_attr('binary_sensor.octopus_energy_intelligent_dispatching',
        'planned_dispatches')[0].end)|timestamp_custom ('%H:%M')) }}
  - service: input_datetime.set_datetime
    data:
      time: >-
        {{
        (as_timestamp(state_attr('binary_sensor.octopus_energy_intelligent_dispatching',
        'planned_dispatches')[0].start)|timestamp_custom ('%H:%M:00')) }}
    target:
      entity_id: input_datetime.battery_charge_start
  - service: input_datetime.set_datetime
    data:
      time: >-
        {{
        (as_timestamp(state_attr('binary_sensor.octopus_energy_intelligent_dispatching',
        'planned_dispatches')[0].end)|timestamp_custom ('%H:%M:00')) }}
    target:
      entity_id: input_datetime.battery_charge_finish
mode: single

Some templating:

Smart charging windows:

Fixed OffPeak Start (a): {{ states('input_datetime.intelligent_octopus_offpeak_window_start')}}
Fixed OffPeak Ends (b): {{ states('input_datetime.intelligent_octopus_offpeak_window_end')}}

Next Dynamic Window Start (c): {{ states('sensor.smart_charge_start') }}
Next Dynamic Window Ends (d): {{ states('sensor.smart_charge_end') }}

Time now: {{ now() }}

Trigger: Dynamic window updated.

1) Is Dynamic window within Fixed OffPeak?  // Ignore
2) Is the dynamic window completely outside the Fixed OffPeak? // Overwrite Fixed OffPeak
3) Does the dynamic window overlap part of the Fixed OffPeak? // Extend Fixed OffPeak

// In the above case we would ignore the update`

Smart charging windows:

Fixed OffPeak Start (a): 23:30:00
Fixed OffPeak Ends (b): 05:30:00

Next Dynamic Window Start (c): 02:00
Next Dynamic Window Ends (d): 02:30

Time now: 2023-08-21 02:00:00.431472+01:00