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
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().hourif 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.