Template sensor triggers configuration file errors

Hi,

Many thanks to anyone taking interest in this query. I hope I can get some helpful pointers in solving this issue. I’ve been scratching my head/googling for some time now but I’m not very experienced with YAML and I am at a dead end.

The problem:
Ultimately I want to use the different levels to drive an automation for some LEDs which I believe requires a sensor trigger in order to interpret the values from the sensor.

I can’t seem to add the sensor trigger template for a Zwift Heart Rate sensor (via the Zwift Sensor HACS integration which appears to be recording values correctly).

Observations
The COVER template has been working without issue for a long time on ny HA system but the error occurs when I add the code for the Zwift sensor trigger to the templates.yaml file.
It’s my feeling that there is some incorrect indenting within either (or both) cover and triggers sections of the templates.yaml file but I’ve tried several different variants that validate in HA and external YAML validators so I’m a bit stumped!

The error I receive via notifications when I restart HA (quick reload) is as follows:

Logger: homeassistant.config
Source: config.py:354
First occurred: 11:32:59 (16 occurrences)
Last logged: 12:32:24

Invalid config for 'template' at templates.yaml, line 57: 'triggers' is an invalid option for 'template', check: cover->1->triggers Invalid config for 'template' at templates.yaml, line 60: 'sensor' is an invalid option for 'template', check: cover->1->sensor
Invalid config for 'template' at templates.yaml, line 58: 'sensor' is an invalid option for 'template', check: sensor
Invalid config for 'template' at templates.yaml, line 79: expected a dictionary for dictionary value 'sensor->0->attributes', got None Invalid config for 'template' at templates.yaml, line 80: 'heart_rate' is an invalid option for 'template', check: sensor->0->heart_rate Invalid config for 'template' at templates.yaml, line 81: 'zone' is an invalid option for 'template', check: sensor->0->zone
Invalid config for 'template' at templates.yaml, line 59: expected a dictionary 'triggers->0', got 's' Invalid config for 'template' at templates.yaml, line 60: 'entity_id' is an invalid option for 'template', check: entity_id Invalid config for 'template' at templates.yaml, line 80: expected a dictionary for dictionary value 'sensor->0->attributes', got None Invalid config for 'template' at templates.yaml, line 81: 'heart_rate' is an invalid option for 'template', check: sensor->0->heart_rate Invalid config for 'template' at templates.yaml, line 82: 'zone' is an invalid option for 'template', check: sensor->0->zone
Invalid config for 'template' at templates.yaml, line 1: expected a dictionary 'triggers->0', got 's' Invalid config for 'template' at templates.yaml, line 22: expected a dictionary for dictionary value 'sensor->0->attributes', got None Invalid config for 'template' at templates.yaml, line 23: 'heart_rate' is an invalid option for 'template', check: sensor->0->heart_rate Invalid config for 'template' at templates.yaml, line 24: 'zone' is an invalid option for 'template', check: sensor->0->zone Invalid config for 'template' at templates.yaml, line 2: 'entity_id' is an invalid option for 'template', check: entity_id

My templates.yaml file is as follows:

- cover:
    - device_class: GARAGE
      open_cover:
        - data:
            entity_id: switch.sonoff3
          action: switch.turn_on
      close_cover:
        - data:
            entity_id: switch.sonoff3
          action: switch.turn_on
      stop_cover:
        - data:
            entity_id: switch.sonoff3
          action: switch.turn_on
      default_entity_id: cover.garage_door_front
      icon: |-
        {% if is_state('switch.sonoffpir1','on') %}
          mdi:garage-open
        {% else %}
          mdi:garage
        {% endif %}
      name: Garage Door Front
      state: |-
        {% if is_state('switch.sonoffpir1','on') %}
          Open
        {% else %}
          Closed
        {% endif %}
- cover:
    - device_class: GARAGE
      open_cover:
        - data:
            entity_id: switch.sonoff4
          action: switch.turn_on
      close_cover:
        - data:
            entity_id: switch.sonoff4
          action: switch.turn_on
      stop_cover:
        - data:
            entity_id: switch.sonoff4
          action: switch.turn_on
      default_entity_id: cover.garage_door_rear
      icon: |-
        {% if is_state('switch.sonoffpir1_2','on') %}
          mdi:garage
        {% else %}
          mdi:garage-open
        {% endif %}
      name: Garage Door Rear
      state: |-
        {% if is_state('switch.sonoffpir1_2','on') %}
          Closed
        {% else %}
          Open
        {% endif %}
  
- triggers:
    - trigger: state
      entity_id: sensor.zwift_heart_rate_8107321
     
      sensor:
        - name: Zwift Heart Rate Status
          unique_id: zwift_heart_rate_status_8107321
          state: >
                {% set hr = states('sensor.zwift_heart_rate_8107321') | int(0) %} {%
                if hr == 0 %}
                  Resting
                {% elif hr > 0 and hr < 100 %}
                  Light
                {% elif hr >= 100 and hr < 130 %}
                  Moderate
                {% elif hr >= 130 and hr < 150 %}
                  Vigorous
                {% elif hr >= 160 %}
                  Maximum
                {% else %}
                  Unknown
                {% endif %}
          attributes:
          heart_rate: '{{ states(''sensor.zwift_heart_rate_8107321'') }}'
          zone: >
                  {% set hr = states('sensor.zwift_heart_rate_8107321') | int(0) %}
                  {% if hr == 0 %}
                    0
                  {% elif hr < 100 %}
                    1
                  {% elif hr < 130 %}
                    2
                  {% elif hr < 150 %}
                    3
                  {% else %}
                    4
                  {% endif %}

Any help is very gratefully received! Please let me know if I’m missing any useful/important information and I’ll try to provide it.

Many thanks,

Jason

Sensor needs to be at the same indentation as triggers and both of your covers

Also, looking at your template, it doesn’t even look like you need a trigger based template sensor. You could just do a state based template sensor

1 Like

Thanks for kindly replying! I’ve tried the indentation you suggested as below. That seems to have changed the nature of the error message, which is definitely positive! However, I don’t know how to resolve this issue. The error message indicates that the error is now around the 'heart_rate" attribute under the sensor section. Is this an indentation problem again, or just that “heart_rate” as an attribute is unrecognised or undefined?

Also, thanks for the suggestion wrt ‘State’. I take your point but I don’t know how to implement that to get the same outcome.

- cover:
    - device_class: GARAGE
      open_cover:
        - data:
            entity_id: switch.sonoff3
          action: switch.turn_on
      close_cover:
        - data:
            entity_id: switch.sonoff3
          action: switch.turn_on
      stop_cover:
        - data:
            entity_id: switch.sonoff3
          action: switch.turn_on
      default_entity_id: cover.garage_door_front
      icon: |-
        {% if is_state('switch.sonoffpir1','on') %}
          mdi:garage-open
        {% else %}
          mdi:garage
        {% endif %}
      name: Garage Door Front
      state: |-
        {% if is_state('switch.sonoffpir1','on') %}
          Open
        {% else %}
          Closed
        {% endif %}
- cover:
    - device_class: GARAGE
      open_cover:
        - data:
            entity_id: switch.sonoff4
          action: switch.turn_on
      close_cover:
        - data:
            entity_id: switch.sonoff4
          action: switch.turn_on
      stop_cover:
        - data:
            entity_id: switch.sonoff4
          action: switch.turn_on
      default_entity_id: cover.garage_door_rear
      icon: |-
        {% if is_state('switch.sonoffpir1_2','on') %}
          mdi:garage
        {% else %}
          mdi:garage-open
        {% endif %}
      name: Garage Door Rear
      state: |-
        {% if is_state('switch.sonoffpir1_2','on') %}
          Closed
        {% else %}
          Open
        {% endif %}
  
- triggers:
    - trigger: state
      entity_id: sensor.zwift_heart_rate_xxxxxx
     
  sensor:
    - name: Zwift Heart Rate Status
      unique_id: zwift_heart_rate_xxxxxx
      state: >
            {% set hr = states('sensor.zwift_heart_rate_xxxxxx') | int(0) %} {%
            if hr == 0 %}
              Resting
            {% elif hr > 0 and hr < 100 %}
              Light
            {% elif hr >= 100 and hr < 130 %}
              Moderate
            {% elif hr >= 130 and hr < 150 %}
              Vigorous
            {% elif hr >= 160 %}
              Maximum
            {% else %}
              Unknown
            {% endif %}
      attributes:
      heart_rate: '{{ states(''sensor.zwift_heart_rate_xxxxxx'') }}'
      zone: >
              {% set hr = states('sensor.zwift_heart_rate_xxxxxx') | int(0) %}
              {% if hr == 0 %}
                0
              {% elif hr < 100 %}
                1
              {% elif hr < 130 %}
                2
              {% elif hr < 150 %}
                3
              {% else %}
                4
              {% endif %}

The error I receive is:

Logger: homeassistant.config
Source: config.py:354
First occurred: 12:45:47 (2 occurrences)
Last logged: 12:45:48

Invalid config for 'template' at templates.yaml, line 80: expected a dictionary for dictionary value 'sensor->0->attributes', got None Invalid config for 'template' at templates.yaml, line 81: 'heart_rate' is an invalid option for 'template', check: sensor->0->heart_rate Invalid config for 'template' at templates.yaml, line 82: 'zone' is an invalid option for 'template', check: sensor->0->zone

So, maybe I’m a little close but still a bit lost!

Many thanks,

Jason

Indent these two lines by at least two spaces:

heart_rate: '{{ states(''sensor.zwift_heart_rate_xxxxxx'') }}' 
zone: >
1 Like

Ah, yes. Thank you. That’s done the trick! No error notifications now!

Many thanks,

Jason