Problem with codition templating

Hello everyone.
I’m stuck with automation, that turns light in entrance hall on arriving. It does simple thing, motion sensor looks for motion, device tracker watches for presence of family group, and if motion detected and nobody at home, it turns the light on.

- alias: Entrance light
  trigger:
  - entity_id: binary_sensor.motion_sensor
    platform: state
    to: 'on'
  condition:
  - condition: template
    value_template: '{{ states.binary_sensor.motion_sensor.attributes['No motion since']|int > 600 }}'
  action:
  - data:
      entity_id: switch.wall_switch
    service: light.turn_on

Value template works perfect in dev_tools, but when i’m trying to check config, i get error

“while parsing a block mapping
in “/home/homeassistant/.homeassistant/automations.yaml”, line 167, column 5
expected , but found ‘’
in “/home/homeassistant/.homeassistant/automations.yaml”, line 168, column 87”

Column 87 is beginning of [‘No motion since’], beetween single quote and “No”.
I think this is a bug, because jinja syntax allows this construction.

The quoting is wrong. Try:

    value_template: "{{ states.binary_sensor.motion_sensor.attributes['No motion since']|int > 600 }}"

Also you’re action is wrong. You can’t use the light turn on service with a switch. It should be:

  action:
  - data:
      entity_id: switch.wall_switch
    service: switch.turn_on

Thank you for reply, but double quotes are also wrong. I solved this problem with other construction:

value_template: '{{ (state_attr(''binary_sensor.motion_sensor_158d000271d664'', ''No motion since'')|int) > 600 }}'

I can guarantee you they are not wrong (if used as I suggested. Maybe you replaced all the single quote characters with double quote characters??? If you did then, yes, that would be wrong.) But whatever works for you.

Yeah, that’s not correct. @pnbruckner’s method is correct. The only reason that’s working for you is because of another issue. When that issue is fixed, this will error for you. I suggest moving to @pnbruckner’s method.

What another issue? What are you talking about? Automation is working now.

your value template is flat out wrong. There is an issue in value templates where strings are concatenated by default inside yaml. Your code is incorrect in the fact that you have 5 strings together, the strings being:

1 '{{ (state_attr('
2 'binary_sensor.motion_sensor_158d000271d664'
3 ', '
4 'No motion since'
5 ')|int) > 600 }}'

When this bug is fixed, your template will error because of this. Those double single quotes that you have are not correct.

You should replace the double single quotes with double quotes. So that the entire string is encapsolated inside single quotes and the template itself uses double quotes. As @pnbruckner said and you blissfully ignored.

    value_template: '{{ states.binary_sensor.motion_sensor.attributes["No motion since"]|int > 600 }}'

I’m confused. In YAML, two single-quote characters in a row, in a string this is quoted with single-quote characters, is just an escaped single-quote character. What bug are you referring to?

EDIT:

I.e., the following are equivalent:

'abc''def'
"abc'def"

EDIT 2:

Any of these should work:

    value_template: "{{ states.binary_sensor.motion_sensor.attributes['No motion since']|int > 600 }}"
    value_template: '{{ states.binary_sensor.motion_sensor.attributes["No motion since"]|int > 600 }}'
    value_template: '{{ states.binary_sensor.motion_sensor.attributes[''No motion since'']|int > 600 }}'
    value_template: "{{ states.binary_sensor.motion_sensor.attributes[\"No motion since\"]|int > 600 }}"
    value_template: >
      {{ states.binary_sensor.motion_sensor.attributes['No motion since']|int > 600 }}
    value_template: >
      {{ states.binary_sensor.motion_sensor.attributes["No motion since"]|int > 600 }}

Well, you learn something new every day. Of course yaml is the code that adds different ways to escape single quotes. I was under the impression that it was a bug and it was concatenating the strings. Which in turn just happened to work. My mistake.

1 Like

My mistake. :slightly_smiling_face:
It work now with double quotes.