Template trigger with conditions

Hello,
I’ve been using Home Assistant for a few weeks now and have already achieved a lot. Now I am trying to create a sensor in a template with a trigger that sets a timestamp depending on the previous numerical value. But I get the error

“Invalid config for ‘template’ at configuration.yaml, line 554: ‘conditions’ is an invalid option for ‘template’, check: conditions”.

I have seen here in the forum that there is a feature request to be able to use conditions here. Is there a way to create this configuration with the current Home Assistant version?
Many thanks in advance!

template:
  - trigger:
      - platform: numeric_state
        entity_id: sensor.wohnzimmer_regenmesser_niederschlag
    conditions:
      - condition: template
        value_template: "{{ trigger.from_state.state|float > trigger.to_state.state|float }}"
      - condition: template
        value_template: "{{ trigger.to_state.state|float = 0 }}"
    sensor:
      - name: Ende Niederschlag heute
        unique_id: sensor.wohnzimmer_regenmesser_ende_niederschlag_heute
        device_class: timestamp
        state: "{{ now() }}"

This is not a valid trigger. Numeric state triggers need a defined above and/or below.

An action block is allowed, but not a condition block. You have to build the conditions into either the trigger or the state template.

I think the following is what you were going for… but if not, clarify what the goal of the sensor is and its expected behavior, then we can probably set you right.

template:
  - trigger:
      - platform: state
        entity_id: sensor.wohnzimmer_regenmesser_niederschlag
        to: 
          - '0'
          - '0.0'
    sensor:
      - name: Ende Niederschlag heute
        unique_id: sensor.wohnzimmer_regenmesser_ende_niederschlag_heute
        device_class: timestamp
        state: |
          {{ now() if trigger.from_state.state | float(0) > 0 else this.state | default(now(),true) }}
1 Like

Thank you for this code, this looks good to me. I implemented it and now I´m waiting for rain.
Purpose of the sensor: I want to record the timestamp (last occurance on a day), when my rain sensor stops reporting rain (so it was raining, and now there is no rain anymore).
addendum: worked well, thanks again!