How do trigger-based template sensors work?

What is wrong with this code? What I want to achieve is that the sensor “Fuellstand_Zisterne_in Prozent” only updates every 5 minutes and not by state change of the shellyuni-adc output.

template:
  - trigger:
    - platform: time_pattern
      hours: "/5"
  
  - sensor:
    - name: "Fuellstand_Zisterne_in Prozent"
      unique_id: fuell_103
      unit_of_measurement: "%"
      state: >
          {{ ( states('sensor.shellyuni_xyzxyz_adc') | float | round(3) / 3 * 100) | float | round(0) }}
    - name: "Fuellstand_Zisterne_in_Liter"
      unique_id: fuell_102
      unit_of_measurement: "l"
      state: >
          {{ ( states('sensor.shellyuni_xyzxyz_adc') | float | round(3) / 3 * 5200) | float | round(-1) }}

The sensor updates by state change and ignores the time pattern.

I guess I have misunderstood the funtionality of the time pattern trigger.
According to the ha docs (template integration) a trigger based template sensor gives you more control “over when an entity updates”. The docs says also “Trigger-based entities do not automatically update when states referenced in the templates change”.
Can anybody help?

Ok couple of issue I can see straight away.

You have defined a time trigger and separate sensors but the two are not linked, look at the sample from the documents:

template:
  - trigger:
      - platform: time_pattern
        # This will update every night
        hours: 0
        minutes: 0
    sensor:
      # Keep track how many days have past since a date
      - name: "Not smoking"
        state: 

Then look at yours:

Also you state in your post you want every 5 mins but then have put in your yaml every 5 hours, which is it you want ?

3 Likes

Thank you, @rossk for having this close look at my configuration! I corrected it and restartet ha. Under development tools ha says that the sensor was last updated at 8:40 (the time I restartet it after having corrected the configuration (please see picture; I took the picture at 9:17) Should’nt there having been more updates?

Can you post up your corrected yaml please?

Thats the section:

# Berechnung des Zisternen-Füllstands in Prozent; time_pattern sorgt dafür, dass nur alle 5 Minuten eine Messung erfolgt (alle durch 5 teilbaren Minuten)
# die unique_id der beiden Sensoren habe ich selbst vergeben, da es ohne diese Angabe zu Fehlermeldungen kam
template:
  - trigger:
    - platform: time_pattern
      minutes: "/5"
  
    sensor:
    - name: "Fuellstand_Zisterne_in Prozent"
      unique_id: fuell_103
      unit_of_measurement: "%"
      state: >
          {{ ( states('sensor.shellyuni_xyzxyz_adc') | float | round(3) / 3 * 100) | float | round(0) }}
    - name: "Fuellstand_Zisterne_in_Liter"
      unique_id: fuell_102
      unit_of_measurement: "l"
      state: >
          {{ ( states('sensor.shellyuni_xyzxyz_adc') | float | round(3) / 3 * 5200) | float | round(-1) }}
    ```

Do you need the complete configuration yaml?

The last changed / updated will only change if the sensor state has changed as well.

For example if I create a sensor that references a temp sensor and make it update every 1 min, the initial last changed / updated will be the first (whole) min after reloading templates or starting HA. The last change / updated will stay at the same value unless the actual temp sensor I am referencing physically changes value.

So the above will not necessarily update every 5 mins as you expected, only if the values have changed.

Thank you for your explanation and your kind help!

1 Like

How can I do the same thing, but for the attribute of an entity, not the state? Currently trying to do something like this, but since the state doesn’t actually change, just the attributes, it doesn’t seem to trigger.

  - trigger:
    - platform: state
      entity_id: sensor.battery_monitor
      attribute: packs
      variables:
        module: 1
        <<: &pack_variables
          main: "{{ trigger.to_state.attributes.packs | selectattr('module', 'eq', module|str) | first | default({}) }}"
          items: >
            {%- set keys = 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', '+', '-', 'module' %}
            {%- set ns = namespace(items=[]) %}
            {%- for key in keys %}
              {%- set mk = key.replace('+','pos_temp').replace('-','neg_temp') %}
              {%- set name = 'P' ~ module ~ mk %}
              {%- set ns.items = ns.items + [(key, {'name': name.upper(), 'value': main.get(key) })] %}
            {%- endfor %}
            {{ dict.from_keys(ns.items) }}
    sensors: &pack_sensors
    - name: "{{ items.c1.name }}"
      state: "{{ items.c1.value or this.state if this.state is defined else none }}"
    - name: "{{ items.c2.name }}"
      state: "{{ items.c2.value or this.state if this.state is defined else none }}"
    - name: "{{ items.c3.name }}"
      state: "{{ items.c3.value or this.state if this.state is defined else none }}"
    - name: "{{ items.c4.name }}"
      state: "{{ items.c4.value or this.state if this.state is defined else none }}"
    - name: "{{ items.c5.name }}"
      state: "{{ items.c5.value or this.state if this.state is defined else none }}"
    - name: "{{ items.c6.name }}"
      state: "{{ items.c6.value or this.state if this.state is defined else none }}"
    - name: "{{ items.pos_temp.name }}"
      state: "{{ items.pos_temp.value or this.state if this.state is defined else none }}"
    - name: "{{ items.neg_temp.name }}"
      state: "{{ items.neg_temp.value or this.state if this.state is defined else none }}"
    - name: "{{ items.module.name }}"
      state: "{{ items.module.value or this.state if this.state is defined else none }}"

Hi,

I try to add these two examples from

but I don’t know how to solve these errors.

the template.yaml is added in my configuration.yaml like this

I don’t know as well how I add the second example from that webpage?

The message is trying to tell you that the old syntax using “platform” has been discontinued. Use the syntax as shown on the manual or this post above: Link