Newb template questions

Hi, I copied some code from an example, but I’m struggling to understand some things.

  1. Are the trigger IDs I put in configuration.yaml template section global? If so, I obviously need to name them better. Trigger IDs created in automations seem local to the automation, so I’m confused about the scope.
  2. Why is there no hyphen before sensor:? I spent hours trying to find my bug. Does that relate to 1, somehow grouping the trigger with the sensor? Should I somehow separate the second, unrelated sensor? Basically, if I had two unrelated sensors with unrelated trigger IDs, I’m not sure what the format would be. Ideally I would encapsulate them in separate objects, but I can’t tell if that’s possible.
  3. Why when setting the state during reset do you use 0 instead of {{ 0 }}?
template:
  - trigger:
      - id: count # are these names global?
        platform: state
        entity_id: binary_sensor.rain_gauge_contact
      - id: reset
        platform: time
        at: '00:00:00'
      - id: restart
        platform: homeassistant
        event: start
      - id: reload
        platform: event
        event_type: event_template_reloaded
    sensor:  # why is there no hyphen before sensor?
      - name: 'Rainfall (day)'
        unique_id: 'rainfall_day'
        state_class: measurement  
        device_class: precipitation
        unit_of_measurement: 'in' 
        state: >
          {% set in_per_flip = (1 / 24) | round(2) %}
          {% if trigger.id == 'reset' or trigger.id == 'restart' or trigger.id == 'reload' %}
          0  # why not {{ 0 }}?
          {% else %}
            {{ this.state | float(0) + in_per_flip }}
          {% endif %}
      - name: 'garage fan current on minutes'
        unique_id: 'garage_fan_current_on_minutes'
        state_class: measurement
        device_class: duration
        unit_of_measurement: min
        state: >
          {% if states('switch.garage_fan') == 'on' %} 
            {{ ((as_timestamp(now()) - as_timestamp(states['switch.garage_fan'].last_changed| default(0)) | int) / 60) | round(0, 'floor') }}
          {% else %}
          0
          {% endif %}

They are local to the list item that contains them. This can include one or more template entities ( such as sensors or binary sensors) and the action block if one is being used.

The hyphen starts a new list item. Any new state-based sensor or trigger-based sensor which is using a different trigger or group of triggers needs a hyphen. You likely want a new list item for the garage sensor. As it stands, that sensor is also only updating based on the 4 triggers that trigger the rain sensor to update.

If you are new to YAML you may want to take a look at YAML for Non-programmers for an introduction to lists and dictionaries and how they are handled in YAML.

Pseudo configuration
template:
  - unique_id: template_item_A
    trigger:
      - id: A1
      - id: A2
    sensor:
      - state: Refreshes whenever A1 or A2 fire.
        attributes:
          a: Refreshes whenever A1 or A2 fire.

  - unique_id: template_item_B
    trigger:
      - id: B1
    sensor:
      - state: Refreshes whenever B1 fires.
        attributes:
          b: Refreshes whenever B1 fires.

  - unique_id: template_item_C
    sensor:
      - state: Refreshes based on changes to entities in state template.
        attributes:
          c: Refreshes based on changes to entities in this attribute's template. 

You can use either the plain value 0 or the expression {{ 0 }}. If you use the expression make sure that it evaluates to a usable type. For example {{ 'hello' }} will return the string “hello”, but {{ hello }} will cause an error because the template engine sees that as a variable without a defined value.

1 Like

Awesome! That’s a perfect explanation. I know quite a few programming languages, but I’m finding YAML (and Jinja2 to some extent) quite unintuitive. This definitely helps.