Template condition automation not right

Hello there

I have been pulling my hair out to try and resolve a simple automation/ condition out but cant get it right.

Here is my code:

  - platform: state
    entity_id: binary_sensor.sm_s906b_is_charging
    to: 'on'
    condition: and
      - "{{ state_attr("sensor.ahmeds22_room_presence", "distance") == '0.1' }}"
      - condition: state
        entity_id: sensor.ahmeds22_room_presence
        state: 'Livingroom'
  - service: notify.alexa_media

but I get an error saying:

2022-06-17 11:16:24 ERROR (MainThread) [homeassistant.components.automation] while parsing a block collection
  in "/config/automations.yaml", line 17, column 7
expected <block end>, but found '<scalar>'
  in "/config/automations.yaml", line 17, column 25

Can anyone shade some light as to what I am doing wrong? Thanks.

you’re using " on the outside of your template and " inside. You can’t do that. You have to use ’ on the outside and " on the inside, or vice versa.

      - "{{ state_attr('sensor.ahmeds22_room_presence', 'distance') == '0.1' }}"

Also, I find it unlikely that an attribute has 0.1 as a string and not a float.

      - "{{ state_attr('sensor.ahmeds22_room_presence', 'distance') == 0.1 }}"

that did it thanks @petro. With regards to 0.1, you reckon I should use it as a float? so remove the ' ? Ideally I want to replace it with < 0.2 as the distance may change erratically and not always 0.1.

yes, just remove the quotes around the number, everything else is fine.

1 Like

perfect thanks :slight_smile:

Also, conditions are and by default, so you could reduce it a bit:

    - "{{ state_attr('sensor.ahmeds22_room_presence', 'distance')|float(0) < 0.2 }}"
    - condition: state
      entity_id: sensor.ahmeds22_room_presence
      state: 'Livingroom'
1 Like