Helper in tod template

I am fairly new to HA and struggling with the YAML syntax to create a binary_sensor using tod and helpers for the after and before properties. This is the content of my config/binary_sensor.yaml file:

- platform: tod
  name: Energy Savings
  after:  '{{ states("input_datetime.energy_savings_start_time") }}'
  before: '{{ states("input_datetime.energy_savings_end_time"  ) }}'

Both input_datetime.energy_savings_start_time and input_datetime.energy_savings_end_time are Date and/or time (time only) helpers with start_time set to 4:00 PM and end_time set to 9:00 PM.

I have tried various combinations of single and doulbe quotes but have not come up with the correct combination. Everything seems to be OK in File editor but errors when I Check Configuration. the current error is:

Invalid config for [binary_sensor.tod]: Invalid time specified: {{ states("input_datetime.energy_savings_end_time" ) }} for dictionary value @ data['before']. Got '{{ states("input_datetime.energy_savings_end_time" ) }}'
Invalid time specified: {{ states("input_datetime.energy_savings_start_time") }} for dictionary value @ data['after']. Got '{{ states("input_datetime.energy_savings_start_time") }}'. (See ?, line ?).

What am I doing wrong? Is there a good reference or tutorial for writing advanced YAML for HA?

Thanks in advance.

The after and before options don’t support templates. The documentation indicates they only support time strings.

Thank you @123. I misinterpret the docs. I assumed that string | time included templates that resolve to sting or time.

if you’re not against templates, just make a template sensor

template:
- binary_sensor:
  - name: My TOD
    state: >
      {{ today_at(states(("input_datetime.energy_savings_start_time")) < now() < today_at(states(("input_datetime.energy_savings_end_time")) }}

Thank you, @petro. Other than the error in your YAML – it should be states(... rather than states((... – this worked perfectly. And I learnt a bit more Jinja2 with today_at() :grinning:

When it supports templates, it will state that it supports templates. Here’s an example.

Thank you, @123. I am fairly new to HA so still learning how to interpret the docs. This helps.