Having trouble with my first template setup

I have a GE Z-wave plus motion sensor that I’ve been trying to configure so it works nicely with Homeassistant. Currently I get values of 0 (clear), 8 (motion detected), or 254 (right after a reboot of Homeassistant). Below is my template, and it seems to be working when I try it in the template editor, but doesn’t actually work when tested.

binary_sensor:
  - platform: template
    sensors:
      ge_motion:
        friendly_name: GE Motion Sensor
        entity_id: sensor.ge_zw6302_portable_smart_motion_sensor_burglar_2
        device_class: motion
        value_template: >-
          {% if is_state('sensor.ge_zw6302_portable_smart_motion_sensor_burglar_2', '8') %}
          on
            {% else %}
          off
            {% endif %}

I just now discovered there is a nice write up in the docs about this exact template. When I copied and pasted the example everything works. Not sure if it was formatting or what but here is the working config.

binary_sensor:
  - platform: template
    sensors:
      ge_motion:
        friendly_name: GE Motion Sensor
        device_class: motion
        value_template: >- 
          {%- if is_state('sensor.ge_zw6302_portable_smart_motion_sensor_burglar_2', '8') -%}
          true
          {%- else -%}
          false
          {%- endif -%}

Indentation matters. A lot. Not sure had you tried without the extra indents in your first example…

Also, the -%} and {%-
Adding the - removes the carriage returns from the result.

So your template WAS returning on and now it returns on

@treno - I still don’t understand this hyphen thing, as per…

https://community.home-assistant.io/t/question-about-hyphens-in-templates/

I never use them in mine and it always works exactly as I expect, and never get whitespace errors (which apparently I should).

Yeah, I’m aware of the importance of formatting, but this was my first go at templating so I was a bit out of my comfort zone. I think that was the solution, although it’s a little misleading that my template was giving accurate results in the front end template editor.

The way I use the template editor is to make sure the template gives the proper text, but in the .yaml files, I have to make sure I put the template in the proper “place” to make that text appear correctly.