How to define my 'night time' from the interface?

Hey,

I’m trying to define different times for day and night, and I want to be able to change it from the interface. So i created 2 ‘helpers’ as input times, one for morning start time, and one for night start time (maybe i’ll add evening and more if it works).

I didn’t succeed to make it work from the TOD platform (binary sensor).

# Trying to create 'tod' with input datetime as 'after' and 'before'
# Not working (Configuration invalid)
- platform: tod
  name: Night Time
  after: "{{ states('input_datetime.night_start.state') }}"
  before: "{{ states('input_datetime.morning_start.state') }}"

so I tried to make it work like a regular template (without TOD), but it works only if night time is before 00:00 (23:59).

# Trying to create night time sensor as a template
# Configuration is valid, but getting wrong date if night time is after midnight
- platform: template
  sensors:
    day_time:
      value_template: "{{ states('input_datetime.night_start') [0:5] > states('sensor.time') > states('input_datetime.morning_start') [0:5] }}"    
      friendly_name: "Day Time"
      icon_template: mdi:white-balance-sunny

What is the correct way to do something like this?
Thanks.

Hey Arthur,

take a look at your tod template, you’re getting the state with states() and .state, that won’t work.
Remove the .state part and try again.
But I don’t see a reference in the documentation to templates, so I guess they aren’t supported with this integration.

Try out this solution, change the from and to parameters :slight_smile:

1 Like

seems like tod doesn’t work with templates.
but the second solution seems to be working, thanks!:slight_smile:

        {% set from = states.input_datetime.night_start.state %}
        {% set to = states.input_datetime.morning_start.state %}
        {% set cur = states.sensor.time.state ~ ':00' %}
        {{ (cur < to or from <= cur) if from > to else from <= cur < to }}  

for some reason this method stopped working, after a few months it worked well.
now the night time shows wrong values… really odd. something changed? what could cause it?

Edited: sorry, solved.