How to create trigger based sensor? How to avoid having "duplicate key" red messages, or other syntax error messages?

I have a list of sensors and binary sensors in my template.yaml file.
Now I want to start playing with trigger-based sensors. But there is clearly something wrong on my file organization.

I already have a list of sensors below the sensor key, starting with the activite_tracker_phone, etc.
Now, no matter what I try, there is always a red message when I try to create a trigger based sensor.

Can somebody please tell me how I should put it? I tried my best looking in the docs but with no success.

Which method are you using to include your new templates.yaml file into your configuration?

This is in my configuration.yaml file:

template: !include templates.yaml

With that method you should only need to add hyphens so that the YAML interpreter knows it’s looking at a list. You will need a new hyphen for every new trigger-sensor grouping and one for your state-based sensors:

- trigger:
    - platform: event
      event_type: "MY_CUSTOM_EVENT"
  sensor:
    - name: test_example
      state: hello

- trigger:
    - platform: state
      entity_id: sensor.test_example
  sensor:
    - name: test_example_2
      state: "{{ iif(trigger.to_state.state == 'hello', 'up', 'down') }}"

- sensor:
## All your state-based sensors can go here ##
2 Likes