Template error "expected '<document start>', but found '<block sequence start>"

I am trying to add the following “sensor” to my sensors.yaml file. What am I doing wrong?

- platform: template
  sensors:
    BedRoomLightWorkColor:
      friendly_name: "BedRoom light color based on weather"
        value_template: >-
            {%- if  sensor.dark_sky_apparent_temperature <= '14' -%}
                Blue
            {%- elif  sensor.dark_sky_apparent_temperature > '14' and sensor.dark_sky_apparent_temperature <= '18' -%}
                Green
            {%- elif  sensor.dark_sky_apparent_temperature > '18' and sensor.dark_sky_apparent_temperature <= '20' -%}
                Yellow
            {%- elif  sensor.dark_sky_apparent_temperature > '20' -%}
                Red
            {%- endif -%}

I have some comparisons like this:

{% if states.sensor.dark_sky_temperature.state | float > 50.5 %}
HOT
{% endif %}
1 Like

Thanks a lot!

I wasnt putting “states.” at the beginning of the sensor name or converting the value to a float " | float"

could you share a bit more of your code so I can see you template formatting?

You will find comparisons in Alexa.yaml

1 Like

Thanks speekquietly very useful examples and interesting you monitor your chickens with it :slight_smile:

I nearly have it working like this:

 - platform: template
   sensors: 
     worklight: 
       friendly_name: 'Work Light'
       value_template: >-
           {%- if  sensor.dark_sky_apparent_temperature <= '14' -%}
               Blue
           {%- elif  sensor.dark_sky_apparent_temperature > '14' and sensor.dark_sky_apparent_temperature <= '18' -%}
               Green
           {%- elif  sensor.dark_sky_apparent_temperature > '18' and sensor.dark_sky_apparent_temperature <= '20' -%}
               Yellow
           {%- elif  sensor.dark_sky_apparent_temperature > '20' -%}
               Red
           {%- endif -%}

but it gets a value of “unknown” and I see lots of errors in my error log like:

17-01-25 22:32:16 homeassistant.components.sensor.template: UndefinedError: 'sensor' is undefined

Are the errors at startup? To avoid the “undefined sensor” errors at startup, wrap the template in “if sensor.dark_sky_apparent_temperature” to check that the sensor exists

Go to YOUR_IP:8123/dev-template to validate the template.
Try:
states.sensor.dark_sky_apparent_temperature.state | float <= 14

This should test correctly

{{ states.sensor.dark_sky_apparent_temperature.state | float <= 14 }}

Thanks Daniel, I was using the dev-template tester, then I somehow went backwards and broke my template and assumed it was ok as I had tested it earlier! I can see I am going to use the dev-template page a lot.

Thanks you are correct.

Just in case anyone else finds this thread I will post my working outcome:

 - platform: template
   sensors: 
     worklight: 
       friendly_name: 'Work Light'
       value_template: >-
           {%- if  states.sensor.dark_sky_temperature.state <= '14' -%}
               Blue
           {%- elif  states.sensor.dark_sky_temperature.state > '14' and states.sensor.dark_sky_temperature.state <= '18' -%}
               Green
           {%- elif  states.sensor.dark_sky_temperature.state > '18' and states.sensor.dark_sky_temperature.state <= '20' -%}
               Yellow
           {%- elif  states.sensor.dark_sky_temperature.state > '20' -%}
               Red
           {%- endif -%}
1 Like