Can't get condition value_template right

I’m trying to do this:

condition: template
value_template: >-
    "{{ states('counter.greet_{{ states('sensor.time_of_day') }}_{{ state_attr(' {{
    trigger.entity_id }} ','matches') }}' < 1) }}"

But i keep getting:

Message malformed: invalid template (TemplateSyntaxError: expected token ',', got 'sensor') for dictionary value @ data['condition'][0]['value_template']

If i remove the “{{ and }}” at the beginning and end i get no errors, but then it keeps saying the condition failed and it shouldn’t (these counters are all set to 0). I guess that’s a good thing, but i cannot see where this fails.

For clarity :
I am saying good morning/noon/evening using a timer day sensor and double_take_sensors.

I have counters set for every recognised person so that they don’t get more then 1 good morning/noon/evening per time of day e.g.:
counter.greet_morning_

These are all set to 0 and are incremented when the automation has ran at the end.

Full code for clarity’s sake:

alias: Greet person based on time of day
description: >-
  Good morning that uses face recognition (Compreface and Doubletake) and the
  time of day sensor to recognise the person who it sees.


  It uses double_take_camera and checks if there is a match, then says good
  timeofday using the appropriate alexa using a trigger.id then delays by the
  defined time in time of day sensor so it only runs once.
trigger:
  - platform: state
    entity_id:
      - sensor.double_take_kitchen
    attribute: matches
    id: garage
  - platform: state
    entity_id:
      - sensor.double_take_diningroom
    attribute: matches
    id: dining_room
condition:
  - condition: template
    value_template: >-
      "{{ states('counter.greet_{{ states('sensor.time_of_day') }}_{{
      state_attr(' {{ trigger.entity_id }} ','matches') }}' < 1) }}"
action:
  - service: notify.alexa_media_{{ trigger.id }}
    data:
      message: >-
        Good {{ states('sensor.time_of_day') }} {% set f = state_attr('{{
        trigger.entity_id', 'matches') %} {{ f[0].name if f is iterable and
        f|count > 0 and f[0].name is defined else "Unrecognized" }}.
      data:
        type: tts
        method: all
  - service: counter.increment
    data: {}
    target:
      entity_id: >-
        counter.greet_{{ states('sensor.time_of_day') }}_{{ state_attr('{{
        trigger.entity_id }}','matches') }}
mode: single

Appreciate the help.

Notice how you make correct syntax at message: >- and entity_id: >-?

Then at value_template: >- you by some reason added " ".
That makes the template a string. If you remove the " " then it’s a template.

1 Like

Thanks, that makes sense, i removed the quotes but i still get the error though.

This seems to work without error but still condition fails.

if (counter.greet_{{ states('sensor.time_of_day')}}_{{
      state_attr(' {{ trigger.entity_id }} ','matches') }} }}) < 1

Developer tools says it’s a string, that’s probably why.

Whatever is between paired double-braces {{ }} is understood to be a Jinja2 template. What you can’t do is nest one template inside another one, like what you did here:

condition: template
value_template: >-
    "{{ states('counter.greet_{{ states('sensor.time_of_day') }}_{{ state_attr(' {{
    trigger.entity_id }} ','matches') }}' < 1) }}"

Try this version:

condition: template
value_template: >-
    {{ states('counter.greet_' ~ states('sensor.time_of_day') ~ '_' ~ state_attr(trigger.entity_id, 'matches')) | int(0) < 1 }}
1 Like

Much better, thanks,

I had to amend it to :

      {{ states('counter.greet_' ~ states('sensor.time_of_day') ~ '_' ~ state_attr(trigger.entity_id, 'matches')[0].name) | int(0) < 1 }}

To get the actual name which matches with the counter name. Weird thing is, even though when i run it in developer tools, it tells me true or false correctly when using < 1 (without the trigger.entity_id obviously).

But the actual automation still fails on a condition. I wish it would tell me what, or i wish i could test with the actual trigger id. Or even if it could just tell me what the value of the template did hold. I guess i can create a counter for that.

Thanks again!

Check the automation’s trace for clues.