Where is the problem? "invalid template (TemplateSyntaxError: unexpected '}')"

I’m trying to figure out where this problem is. Unfortunately, the logs doesn’t help much, since it can’t say which automation that has the problem (could it not just say the id/alias of the automation?)

The full log error is:

Invalid config for [automation]: invalid template (TemplateSyntaxError: unexpected '}') for dictionary value @ data['condition'][0]['value_template']. Got None. (See ?, line ?).
Invalid config for [automation]: invalid template (TemplateSyntaxError: unexpected '}') for dictionary value @ data['condition'][0]['value_template']. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 0).

If I grep for value_template in my automations.yaml I get:

(homeassistant) homeassistant@mistik:~/_homeassistant$ grep value_template automations.yaml 
    value_template: '{{ state.attributes.elevation }}'
    value_template: '{{ state.attributes.elevation }}'
      value_template: '{{ state.attributes.elevation }}'
    value_template: '{{ state.attributes.elevation }}'
        value_template: '{{ not is_state(''device_tracker.gte_position'', ''home'') }}'
        value_template: '{{ not is_state(''device_tracker.gte_position'', ''home'') }}'
    value_template: '{{ state.attributes.elevation }}'
      value_template: '{{ states.media_player.sony_x77 != "playing" }}'
      value_template: '{{ states.media_player.dining_room_jbl != "playing" }}'

Can someone see a problem here?

check your quotes…?

No that’s actually how you escape single quotes inside single quotes in yaml. Make an automation in the UI editor and use a template and you’ll see that a lot since it only uses single quotes.

That being said, @Konstigt those ones might be the problem. Is there no closing }} on those lines?
EDIT: Whoops, had to scroll I see it now.

Yea idk, nothing is jumping out at me. Btw this log line is giving you some positioning information:

It’s telling you that the error is in the condition section of an automation and the problem is in the first condition. Do you have many automations that use the condition section? Maybe that narrows it down some?

remove ‘’ around device_tracker.get_position … no ‘’ around the entity
forget about it :slight_smile:

It’s probably a combination of ignoring this warning:

https://www.home-assistant.io/docs/configuration/templating/#states

And missing the entity in these:

'{{ state.attributes.elevation }}'

States of which entity has an attribute called elevation?

Sun?

If so, try this instead:

value_template: "{{ state_attr('sun.sun', 'elevation' }}"

Though it is probably not needed at all. Use a numeric state condition instead. Show your actual automation.

For these:

value_template: '{{ states.media_player.sony_x77 != "playing" }}'

Use this format instead:

value_template: "{{ not is_state('media_player.sony_x77', 'playing' }}"

Although again, you could use a state and not condition rather than a template condition.

1 Like

Okey, here are the fuller versions instead of just grep’ing for template:

  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: -5.9

      - condition: template
        value_template: '{{ not is_state(''device_tracker.gte_position'', ''home'')
          }}'

      - condition: template
        value_template: '{{ not is_state(''device_tracker.gte_position'', ''home'')
          }}'

    - condition: template
      value_template: '{{ not is_state(''media_player.sony_x77'', ''playing'' }}'
    - condition: template
      value_template: '{{ not is_state(''media_player.dining_room_jbl'', ''playing'' }}'

Quoting is not how I did them originally but how HA has changed them since I moved them from manual-only-file to editable-from-gui-file.

Ok, change this:

  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: -5.9

To this:

  trigger:
    platform: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: -5.9

I’d need to see the others in context.

1 Like

Changed according to your suggestions but the startup error remains. But my code is a bit nicer now so thanks for that :slight_smile:

The error doesn’t look exactly the same now:

Logger: homeassistant.config
Source: config.py:454
First occurred: 08:14:09 (2 occurrences)
Last logged: 08:14:09

Invalid config for [automation]: invalid template (TemplateSyntaxError: unexpected '}', expected ')') for dictionary value @ data['condition'][0]['conditions'][1]['value_template']. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 0).
Invalid config for [automation]: invalid template (TemplateSyntaxError: unexpected '}') for dictionary value @ data['condition'][0]['value_template']. Got None. (See /home/homeassistant/.homeassistant/configuration.yaml, line 0).

Since it’s quite big automations I post them one by one.

- alias: Larm Aktivera och Notify om ej larmat och ingen hemma (rörelse)
  initial_state: true
  trigger:
  - platform: state
    entity_id: input_boolean.motion_hall_30
    from: 'off'
    to: 'on'
  - platform: state
    entity_id: input_boolean.motion_dining_30
    from: 'off'
    to: 'on'
  action:
  - service: notify.notify
    data:
      message: Glömt att larma? (rörelse)
      title: Home Assistant
  - service: alarm_control_panel.alarm_arm_home
    entity_id: alarm_control_panel.home_alarm
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: alarm_control_panel.home_alarm
      state: disarmed
    - condition: state
      entity_id: input_boolean.motion_hall_30
      state: 'on'
    - condition: state
      entity_id: input_boolean.motion_dining_30
      state: 'on'
    - condition: or
      conditions:
      - condition: state
        entity_id: binary_sensor.thomas_s20_presence
        state: 'off'
      - condition: template
        value_template: '{{ not is_state(''device_tracker.gte_position'', ''home'')
          }}'
  id: 48a32841b2164ae0818d46a1d8b47013
- alias: Larm Notify om ej larmat och ingen hemma (Thomas mobil och bilen)
  initial_state: true
  trigger:
  - platform: state
    entity_id: binary_sensor.thomas_s20_presence
    from: 'on'
    for: 00:31:00
  - platform: state
    entity_id: device_tracker.gte_position
    from: home
    for: 00:31:00
  action:
  - service: notify.notify
    data:
      message: Glömt att larma? (mobil)
      title: Home Assistant
  - service: alarm_control_panel.alarm_arm_home
    entity_id: alarm_control_panel.home_alarm
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: alarm_control_panel.home_alarm
      state: disarmed
    - condition: or
      conditions:
      - condition: state
        entity_id: binary_sensor.thomas_s20_presence
        state: 'off'
      - condition: template
        value_template: '{{ not is_state(''device_tracker.gte_position'', ''home'')
          }}'
    - condition: and
      conditions:
      - condition: state
        entity_id: input_boolean.motion_hall_30
        state: 'on'
      - condition: state
        entity_id: input_boolean.motion_dining_30
        state: 'on'
  id: 1f8a2d6d71384772963c5ca7f7d87fad

- alias: Play Radio when motion is detected in the morning
  initial_state: true
  trigger:
    platform: state
    entity_id: light.hall_inne
    from: 'off'
    to: 'on'
  action:
  - service: media_player.play_media
    data:
      entity_id: media_player.sony_and_dining_room
      media_content_id: http://sverigesradio.se/topsy/direkt/207-hi.mp3
      media_content_type: audio/mp3
  - delay: 0:0:1
  - service: media_player.volume_set
    data:
      entity_id: media_player.sony_x77
      volume_level: '0.25'
  - service: media_player.volume_set
    data:
      entity_id: media_player.dining_room_jbl
      volume_level: '0.50'
  condition:
    condition: and
    conditions:
    - condition: time
      after: '6:00:00'
      before: '9:30:00'
    - condition: template
      value_template: '{{ not is_state(''media_player.sony_x77'', ''playing'' }}'
    - condition: template
      value_template: '{{ not is_state(''media_player.dining_room_jbl'', ''playing''
        }}'
    - condition: state
      entity_id: input_boolean.motion_hall_inne
      state: 'on'
  id: 606d5bdb571e456a8c4dab54a9f18d37

They look pretty good. You could write them like this, though I’m not sure if this shorthand is available in the automation editor:

- alias: Larm Notify om ej larmat och ingen hemma (Thomas mobil och bilen)
  initial_state: true
  trigger:
  - platform: state
    entity_id: binary_sensor.thomas_s20_presence
    from: 'on'
    for: 00:31:00
  - platform: state
    entity_id: device_tracker.gte_position
    from: home
    for: 00:31:00
  action:
  - service: notify.notify
    data:
      message: Glömt att larma? (mobil)
      title: Home Assistant
  - service: alarm_control_panel.alarm_arm_home
    entity_id: alarm_control_panel.home_alarm
  condition:
    - condition: state
      entity_id: alarm_control_panel.home_alarm
      state: disarmed
    - or:
      - condition: state
        entity_id: binary_sensor.thomas_s20_presence
        state: 'off'
      - not:
          condition: state
          entity_id: device_tracker.gte_position'
          state: 'home'
    - and:
      - condition: state
        entity_id: input_boolean.motion_hall_30
        state: 'on'
      - condition: state
        entity_id: input_boolean.motion_dining_30
        state: 'on'
  id: 1f8a2d6d71384772963c5ca7f7d87fad
- alias: Play Radio when motion is detected in the morning
  initial_state: true
  trigger:
    platform: state
    entity_id: light.hall_inne
    from: 'off'
    to: 'on'
  action:
  - service: media_player.play_media
    data:
      entity_id: media_player.sony_and_dining_room
      media_content_id: http://sverigesradio.se/topsy/direkt/207-hi.mp3
      media_content_type: audio/mp3
  - delay: 0:0:1
  - service: media_player.volume_set
    data:
      entity_id: media_player.sony_x77
      volume_level: '0.25'
  - service: media_player.volume_set
    data:
      entity_id: media_player.dining_room_jbl
      volume_level: '0.50'
  condition:
    - condition: time
      after: '6:00:00'
      before: '9:30:00'
    - not:
        condition: state
        entity_id: media_player.sony_x77
        state: playing
    - not:
        condition: state
        entity_id: media_player.dining_room_jbl
        state: 'playing'
    - condition: state
      entity_id: input_boolean.motion_hall_inne
      state: 'on'
  id: 606d5bdb571e456a8c4dab54a9f18d37
1 Like

Thanks again! The problem was probably fixed somewhere along the line… in my automations.yaml-file. BUT! The problematic old automations/x.yaml-files that I had moved into directory automations/old-directory was still read! So that’s why I got the error again and again… even after fixing the templates. I didn’t count on that “dir_list_merge” parameter also including subdirectories. Now with these files removed, the problem is gone. Just wish the error could have pointed to these files! :slight_smile: