How often will this expression be evaluated (now())

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) }}        
#
# 

From the Templating docs

image

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.

1 Like

No need. Trigger based template sensors are restored on start-up.

1 Like

I will give that a shot. Thanks, seems obvious now

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.

Thank you. I think my errors had to do with the merged file sets.
In configuration.yaml I have:

##################################
####            FILE INCLUDES
##################################
####
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
multiscrape: !include multiscrape.yaml
sensor: !include_dir_merge_list includes/included_sensors/
notify: !include includes/notify.yaml
template: !include_dir_merge_list includes/included_templates/
powercalc: !include includes/powercalc.yaml
utility_meter: !include includes/utility_meter.yaml
camera: !include includes/camera.yaml
alert2: !include includes/alert2.yaml

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 :slight_smile: