Trigger-based template sensor: Trigger not triggering

I have the following trigger-based sensor defined in YAML. It uses two triggers, one is state-based the other template-based. This defines two sensors, one for daily high and one for daily low.

(I used details found in this post to create this sensor: Statistic sensor reset/clear at midnight for daily min & max temperature - #29 by droidgren)

The state-based trigger works as expected. I cannot get the template-based trigger to fire.

NOTE: the YAML in my configuration is slightly different from the example based on what I read in the current documents for the template integration.

YAML Configuration
template:
  - trigger:
      - trigger: template
        value_template: "{{ now().hour == 0 }}"
      - trigger: state
        not_to:
          - unknown
          - unavailable
        entity_id: sensor.outdoor_temp
    action:
      - variables:
          source: "{{ states('sensor.outdoor_temp') | float }}"
          now: "{{ now() }}"
    sensor:
      - name: "Outdoor Daily High Temperature"
        unique_id: outdoor-daily-high
        unit_of_measurement: "°F"
        device_class: temperature
        state_class: measurement
        icon: mdi:arrow-up-circle-outline
        state: |
          {% set current = (this.state or trigger.to_state.state) | float(source) %}
          {{ source if trigger.trigger == 'template' else [source, current] | max }}
        attributes:
          temperature_updated: |
            {% set current = (this.state or trigger.to_state.state) | float(source) %}
            {{ now if (trigger.trigger == 'template' or source > current) else this.attributes.temperature_updated | default(now) }}
          sensor_last_reset: |
            {{ now if trigger.trigger == 'template' else this.attributes.sensor_last_reset | default(now) }}
      - name: "Outdoor Daily Low Temperature"
        unique_id: outdoor-daily-low
        unit_of_measurement: "°F"
        device_class: temperature
        state_class: measurement
        icon: mdi:arrow-down-circle-outline
        state: |
          {% set current = (this.state or trigger.to_state.state) | float(source) %}
          {{ source if trigger.trigger == 'template' else [source, current] | min }}
        attributes:
          temperature_updated: |
            {% set current = (this.state or trigger.to_state.state) | float(source) %}
            {{ now if (trigger.trigger == 'template' or source < current) else this.attributes.temperature_updated | default(now) }}
          sensor_last_reset: |
            {{ now if trigger.trigger == 'template' else this.attributes.sensor_last_reset | default(now) }}

I have tried changing the value_template: several times today trying to catch an error message of some sort when the time passes and have seen nothing. I have even tried being more precise with the template adding both hour and minute definitions to the template.

According to the documents for the template trigger, the template simply needs to evaluate to true to fire the trigger. I have tested my templates in the developer tools and when the time matches the template does evaluate to true.

Any suggestions on what I’m missing here?

Thanks in advance for your assistance!

That not exactly right, the template’s rendered value needs to change from false to true… after the integration has been loaded.

FWIW, there is a custom integration available for this kind of sensor:

Periodic Min/Max

Well, that’s what was in my head, just not what I typed. You are correct!

If the trigger is set to fire when the clock hits midnight, the template now().hour == 0 will evaluate as false up to 11:59:59. It will then evaluate as true at 00:00:00 and will stay true until 01:00:00. The trigger should only fire once when the clock hits midnight because that’s when the template goes from false to true. Does that sound correct?

This just means that HA is restarted or the Template Entity reload has been run from developer tools, correct?

I’ll take a look at the custom integration you linked. It’s not just daily statistics I’m looking for though. I have some other sensors I will be keeping monthly and annual details for. The outdoor temperature sensor was just one I had that is online now to test this with.

The overall project is for a new water well that is going in. I want to keep track of head pressure at the pump (calculated), water flow rate and volume going into a storage tank, and maybe others. The Utility Meter integration will probably work nicely for the water volume details. I’ll have to figure out the others.

Well, it looks like it’s something other than the trigger definition. I just created this very simple trigger-based sensor:

- trigger:
    - trigger: template
      value_template: "{{ now().hour == 19 and now().minute == 10 }}"
  action:
    - variables:
        now: "{{ now() }}"
  sensor:
    - name: Trigger Sensor Test
      unique_id: trigger-sensor-test
      icon: mdi:test-tube
      state: |
        Last Updated {{ now }}

The trigger fired perfectly on time and updated the state of the sensor.

Time to rebuild the original sensor section by section and see which part is not working. I’ll update this post with my findings.

That’s not real… I think you mean trigger.platform

I just tested that. Changing to trigger.platform fixed the issue.

I noticed in the docs that the definition of the trigger changed from - platform to - trigger and I assumed the property changed as well. Had I read further in the automation trigger docs I would have seen the numerous property tables that still show trigger.platform as one of the trigger properties.

Ugg!!!

Thanks for proofreading, @Didgeridrew!!