Trigger based sensors in templates

Hello,

I’ve been successfully using the following setup in configuration.yaml:

template: !include_dir_list template/

With template sensors like these:

sensor:
  - name: "Defro - Bieg Zadany Max"
    unique_id: "defro_bieg_zadany_max"
    state: >-
      {{ [
        states('sensor.defro_bieg_nawiewu') | int(0),
        states('sensor.defro_bieg_wywiewu') | int(0)
      ] | max }}

binary_sensor:
  - name: "Defro - Status"
    unique_id: "binary_sensor.defro_status"
    state: >-
      {% set val = states('sensor.defro_bieg_zadany') | int(0) %} 
      {{ 'off' if val == 0 else 'on' }}

I’m now trying to add this trigger based sensor into my template.yaml:

- triggers:
    - trigger: state
      entity_id:
        - sensor.defro_bieg_nawiewu
        - sensor.defro_bieg_wywiewu
      not_to:
        - unavailable
        - unknown
  sensor:
    - name: "Defro - Bieg Zadany Max"
      unique_id: "defro_bieg_zadany_max"
      state: >-
        {{ [ states('sensor.defro_bieg_nawiewu') | int(0), states('sensor.defro_bieg_wywiewu') | int(0) ] | max }}

However, it doesn’t work. I’ve checked community posts and tried various approaches without success. ChatGPT failed too. I’m sure the fix is simple… Can someone help me, please?

It’s a bit hard to tell how you’ve formatted it (specific to the indenting) since you split it into multiple examples. But here’s what it should look like:

  - switch:
      - name: my template switch
        ...
      - name: template switch 2
        ...
  - sensor:
      - name: my template sensor
        ...
  - triggers:
      - trigger: some trigger
        ...
    sensor:
      - name: trigger-based template sensor
        ...
    binary_sensor:
      - name: binary sensor based off same trigger
        ...
  - triggers:
      - trigger: some other trigger
        ...
    binary_sensor:
      - name: trigger-based binary template sensor
        ...

With include_dir_list, each file is implictely a list entry, so remove the - in front of triggers:, the same way you did with the others.

like that? this doesn’t work - I’ve just tried it again

sensor:
  - name: "Defro - Bieg Zadany Max"
    unique_id: "defro_bieg_zadany_max"
    state: >-
      {{ [
        states('sensor.defro_bieg_nawiewu') | int(0),
        states('sensor.defro_bieg_wywiewu') | int(0)
      ] | max }}

binary_sensor:
  - name: "Defro - Status"
    unique_id: "binary_sensor.defro_status"
    state: >-
      {% set val = states('sensor.defro_bieg_zadany') | int(0) %} 
      {{ 'off' if val == 0 else 'on' }}

triggers:
  - trigger: state
    entity_id:
      - sensor.defro_bieg_nawiewu
      - sensor.defro_bieg_wywiewu
    not_to:
      - unavailable
      - unknown
sensor:
  - name: "Defro - Bieg Zadany Max"
    unique_id: "defro_bieg_zadany_max_test"
    state: >-
      {{ [ states('sensor.defro_bieg_nawiewu') | int(0), states('sensor.defro_bieg_wywiewu') | int(0) ] | max }}

it throws a warning and some sensors don’t work (I have other sensors in the same file following the same structure and indenting)

if I do it like that, then I’m getting the following error:

Cannot quick reload all YAML configurations because the configuration is not valid: Unexpected error calling config validator: ‘NodeListClass’ object has no attribute ‘keys’

here’s the code:

  - sensor:
      - name: "Defro - Bieg Zadany Max"
        unique_id: "defro_bieg_zadany_max"
        state: >-
          {{ [
            states('sensor.defro_bieg_nawiewu') | int(0),
            states('sensor.defro_bieg_wywiewu') | int(0)
          ] | max }}

  - binary_sensor:
      - name: "Defro - Status"
        unique_id: "binary_sensor.defro_status"
        state: >-
          {% set val = states('sensor.defro_bieg_zadany') | int(0) %}
          {{ 'off' if val == 0 else 'on' }}

  - triggers:
      - trigger: state
        entity_id:
          - sensor.defro_bieg_nawiewu
          - sensor.defro_bieg_wywiewu
        not_to:
          - unavailable
          - unknown
    sensor:
      - name: "Defro - Bieg Zadany Max Test"
        unique_id: "defro_bieg_zadany_max_test"
        state: >-
          {{ [
            states('sensor.defro_bieg_nawiewu') | int(0),
            states('sensor.defro_bieg_wywiewu') | int(0)
          ] | max }}

it looks like I managed to get it to work :slight_smile: thank you for all your help!

here’s the working solution:

1. update configuration.yaml:

template: !include_dir_merge_list template

2. divide sensors into separate yaml files for better control - in my case like that:

A. defro_fan_sensor
B. defro_trigger_sensors
C. defro_template_sensors

3. structure each yaml in the following way:

B. defro_trigger_sensors

- triggers:
    - trigger: state
      entity_id:
        - sensor.defro_bieg_nawiewu
        - sensor.defro_bieg_wywiewu
      not_to:
        - unavailable
        - unknown
  sensor:
    - name: "Defro - Bieg Zadany Max 2"
      unique_id: "defro_bieg_zadany_max_2"
      state: >-
(...)

C. defro_template_sensors

- sensor:
    - name: "Defro - Bieg Zadany Max"
      unique_id: "defro_bieg_zadany_max"
      state: >-
        {{ [
          states('sensor.defro_bieg_nawiewu') | int(0),
          states('sensor.defro_bieg_wywiewu') | int(0)
        ] | max }}

- binary_sensor:
    - name: "Defro - Status"
      unique_id: "binary_sensor.defro_status"
      state: >-
        {% set val = states('sensor.defro_bieg_zadany') | int(0) %}
        {{ 'off' if val == 0 else 'on' }}

        {{ [
          states('sensor.defro_bieg_nawiewu') | int(0),
          states('sensor.defro_bieg_wywiewu') | int(0)
        ] | max }}