Issue with template in binary sensor

I’m trying to build a binary sensor that will be an alarm for me if the temp in the house gets too low. However, I am unable to get the alarm to trigger (it is always off.) Here is my configuration.yaml:

binary_sensor:

  • platform: template
    sensors:
    temp_alarm:
    friendly_name: ‘Temp Alarm’
    sensor_class: cold
    value_template: ‘{{ states.sensor[“2gig_technologies_ct30_thermostat_temperature_7_1”].state < 70}}’

{{ states.sensor[“2gig_technologies_ct30_thermostat_temperature_7_1”].state}} returns 68.0 in the templating tool.

What am I getting wrong in the template?

Shouldn’t this be:
{{ states.sensor.2gig_technologies_ct30_thermostat_temperature_7_1.state }}

assuming your sensor shows up in the dev states panel as:

sensor.2gig_technologies_ct30_thermostat_temperature_7_1

I had tried it that way at first. However, Jinja does not seem to like the use of the number “2” in “2gig”. Using

{{ states.sensor.2gig_technologies_ct30_thermostat_temperature_7_1.state }}

results in the following error:

Error rendering template: TemplateSyntaxError: expected token ‘end of print statement’, got ‘gig_technologies_ct30_thermostat_temperature_7_1’

I figured out my problem though. The template expects the value to be in double quotes. So:

{{ states.sensor[“2gig_technologies_ct30_thermostat_temperature_7_1”].state < “70”}}
works

Maybe I was too quick to declare my issue solved. HA is still throwing errors on startup. If I use:

value_template: ‘{{ states.sensor[‘2gig_technologies_ct30_thermostat_temperature_7_1’].state < “48”}}’

I get this error in the log:

ERROR (Thread-1) [homeassistant.util.yaml] while parsing a block mapping
in “/home/hass/.homeassistant/binary_sensor.yaml”, line 4, column 7
expected , but found ‘’
in “/home/hass/.homeassistant/binary_sensor.yaml”, line 4, column 42

And if I change it to this:

value_template: ‘{{ states.sensor.2gig_technologies_ct30_thermostat_temperature_7_1.state < “48”}}’

I get this error:

ERROR (MainThread) [homeassistant.bootstrap] Invalid config for [binary_sensor.template]: invalid template (TemplateSyntaxError: expected token ‘end of print statement’, got ‘gig_technologies_ct30_thermostat_temperature_7_1’) for dictionary value @ data[‘sensors’][‘temp_alarm’][‘value_template’]. Got ‘{{ states.sensor.2gig_technologies_ct30_thermostat_temperature_7_1.state < “48”}}’. (See ?, line ?). Please check the docs at Template - Home Assistant

I don’t understand what the system wants…

Go to YOUR_IP:8123/dev-template to validate and debug your template.

I used the template developer tool to confirm that my template works.

{{ states.sensor[‘2gig_technologies_ct30_thermostat_temperature_7_1’].state < “48”}}

returns “False” (or “True”, depending on the value in the template) as expected. However, when I use that template in the configuration.yaml, I get the error described in my last post:

ERROR (Thread-1) [homeassistant.util.yaml] while parsing a block mapping
in “/home/hass/.homeassistant/binary_sensor.yaml”, line 4, column 7
expected , but found ‘’
in “/home/hass/.homeassistant/binary_sensor.yaml”, line 4, column 42

Here is the exact contents of binary_sensor.yaml:

- platform: template
  sensors:
    temp_alarm:
      value_template: '{{ states.sensor['2gig_technologies_ct30_thermostat_temperature_7_1'].state < "48"}}'
      friendly_name: Temp Alarm
      sensor_class: cold

Try:

value_template: ‘{{ states.sensor[‘2gig_technologies_ct30_thermostat_temperature_7_1’].state < ‘48’}}’

That did not fix it, but it did get me going and I figured it out (for now). I needed double quotes everywhere inside the template.

value_template: ‘{{ states.sensor[“2gig_technologies_ct30_thermostat_temperature_7_1”].state < “48”}}’

Thanks

2 Likes

If your temperature is always a float or integer, you could use the int or float filters so you don’t need quotes around the numbers and use the states() function so you don’t need the brackets:

value_template: "{{ states('sensor.2gig_technologies_ct30_thermostat_temperature_7_1') | float < 48 }}"

The key is to not use the same type of quotes within the string, otherwise the parser can’t tell where the real end of the string is. If you use double quotes around the string, use single quotes everywhere within and vice versa.

You can sometimes also use a backslash to escape the quote within the string to tell the parser to treat it as a normal character and not the end of the string:

value_template: "{{ states(\"sensor.2gig_technologies_ct30_thermostat_temperature_7_1\") | float < 48 }}"

I’m not 100% sure if the backslash works in templates though.

3 Likes

I spent nearly two hours trying to figure what was it, no other solution has worked except yours! Thanks! :smiling_face_with_three_hearts:

I just hit this issue myself. There appears to be a “problem” in the template engine if you have an entity of the form foo.x_y_z where either of x or y are only digits. For example, I had an entity named weather.1234_main_st (not my real address) and it would throw the error:

TemplateSyntaxError: expected token ‘end of print statement’, got ‘main’

Using this workaround, weather[“1234_main_st”] gets around the issue and the template works. This took a LOT of googling to figure out, but once I plugged in the error I got this thread. So THANK YOU!