I believe that now() in a template normally gets evaluated every second (so a practice to be avoided) , but I wondered about when I used it as below (in my binary sensor template definition)
I suspect it is still evaluated every second, in which case I will make it an automation so I can control that it only gets evaluated once a day.
#
############################################################
# CHRISTMAS LIGHT SEASON
############################################################
#
- platform: template
sensors:
christmas_lights_season:
friendly_name: "Christmas Lights Season"
device_class: light
value_template: >
{% set n = (now().month, now().day) %}
{{ n >= (10, 13) or n <= (1, 10) }}
#
#
If you want to reduce that you can use a trigger-based template sensor that fires daily by using a single Time trigger, but you may also want to include a trigger for restart.
Are you sure I can do a trigger based template for a binary sensor? Struggling a bit with this. Wouldn’t the trigger have a platform of “time” and the sensor a platform of “template”?
Trigger-based template sensors and binary sensors are only available using the current format, not the legacy format, so there is no sensor “platform”…
template:
- trigger:
- platform: time
at: "00:01:00"
binary_sensor:
- name: Christmas Lights Season
device_class: light
state: >
{% set n = (now().month, now().day) %}
{{ n >= (10, 13) or n <= (1, 10) }}
Note that the current format uses the top-level key template, not sensor like the legacy format, so it should be placed either in directly in configuration.yaml or in a properly merged file set to use template.
I had added the binary sensor template to one of the files in the "included_templates folder. I got errors pointing to the line in the time trigger “platform: time” - saying “required key: platform is missing”. This of course seemed really strange since is was pointing to the platform key.
I then created a new file in that folder called “seasons.yaml” and move this template configuration into it (as the only template) - and removed the “template:” line. After that it worked fine.
Many thanks.
Just when I think I am beginning to grasp templates (after creating about 75 of them) - I realize that I really have so so much yet to learn