Best way to check time of day?

I want to use the current time of day as conditions for bayesian sensors. I’m wondering what the best way to format the template for this would be. This is what I’ve come up with so far:

    observations:
      # between 1030pm and 12am
      - platform: template
        value_template: >
          {{ (now().strftime('%H%M') | int >= 2230 and now().strftime('%H%M') | int < 2400) }}
        prob_given_true: 0.5
        prob_given_false: 0.5        
      # between 12am and 7am
      - platform: template
        value_template: >
          {{ (now().strftime('%H%M') | int >= 0 and now().strftime('%H%M') | int < 700) }}
        prob_given_true: 0.928571429
        prob_given_false: 0.071428571
      # between 7am and 9am
      - platform: template
        value_template: >
          {{ (now().strftime('%H%M') | int >= 700 and now().strftime('%H%M') | int < 900) }}
        prob_given_true: 0.5
        prob_given_false: 0.5 
      # between 9am and 10pm
      - platform: template
        value_template: >
          {{ (now().strftime('%H%M') | int >= 900 and now().strftime('%H%M') | int < 2200) }}
        prob_given_true: 0.000769231
        prob_given_false: 0.999230769 

Not sure about ‘the best’. But this is the way I do it.

        {{ now() >
        now().replace(hour=6).replace(minute=20).replace(second=0).replace(microsecond=0) 
           and now() < now().replace(hour=9).replace(minute=30).replace(second=0).replace(microsecond=0) }}

The way you’re doing it works though. I bet there’s really short ways to do this. Let’s ask @123 :slight_smile:

1 Like
observations:
      # between 10pm and 12am
      - platform: template
        value_template: '{{ now().hour >= 22 }}'
        prob_given_true: 0.5
        prob_given_false: 0.5        
      # between 12am and 7am
      - platform: template
        value_template: '{{ now().hour < 7 }}'
        prob_given_true: 0.928571429
        prob_given_false: 0.071428571
      # between 7am and 9am
      - platform: template
        value_template: '{{ 7 <= now().hour < 9 }}'
        prob_given_true: 0.5
        prob_given_false: 0.5 
      # between 9am and 10pm
      - platform: template
        value_template: '{{ 9 <= now().hour < 22 }}'
        prob_given_true: 0.000769231
        prob_given_false: 0.999230769
1 Like

This is what I used to use but it doesn’t work if you want to consider 30 or even 15 minute increments.

You could create some time of day binary sensors:

2 Likes

Nice! That’s pretty much exactly what I’m looking for.

Curious though. Anything similarly simple to this that lets you do minutes?

{{ 9 <= now().hour < 22 }}

{{ today_at("9:15") <= now() < today_at("22:30") }}

Time of Day Sensor using mapper
template:
  - sensor:
      - name: Time of Day
        state: >
          {% set e_morn = today_at("05:00")%}
          {% set morn = today_at("07:00")%}
          {% set noon = today_at("12:00")%}
          {% set evening = today_at("18:00") %}
          {% set night = today_at("20:00")%}
          {% set l_night = today_at("01:00")%}
          {% set map = {
          e_morn < now() <= morn: 'Early Morning',
          morn < now() <= noon: 'Morning',
          noon < now() <= evening : 'Afternoon',
          evening < now() <= night: 'Evening',
          night < now() < l_night + timedelta(days=1): 'Night',
          today_at() < now() <= l_night: 'Night',
          l_night < now() <= e_morn : 'Late Night'
          } %}
          {{ map.get(True) }}
6 Likes

Oh wow, this is a really clean solution. I think I like it better than the Time of Day sensor.

Much better thanks!!

Perhaps you have forgotten that I showed you how, nine days ago, when I reduced your multi-line template to a single line using today_at.

However, my preference is to use now().hour if the scheduled time is on the hour (because it’s shorter and neater). To demonstrate that, here’s the equivalent of didgeridrew’s Time of Day Sensor.

TOD when times are on the hour
template:
 - sensor:
     - name: Time of Day
       state: >
         {% set h = now().hour %}
         {{ {0 <= h < 5: 'Late Night',
             5 <= h < 7: 'Early Morning',
             7 <= h < 12: 'Morning',
             12 <= h < 18: 'Afternoon',
             18 <= h < 20: 'Evening',
             20 <= h <= 23: 'Night'}.get(true) }}

Tip:
If you leave the argument blank in today_at() it’s equivalent to 00:00.

actually yes I couldn’t find it. Now I added it to my yaml notes though. :slight_smile:

As an added bonus I’ve also learned that x < now() <= y syntax is valid. I thought I had to do x < now() and now() <= y.

For future reference: Python comparisons