Templates in automations?

hi.
i’m trying to trigger actions based on the humidity level. for that purpose i have created this template:

{{states("input_number.humidity_setpoint") | float + 1 }}

the thing is, in the template section of the developer tools, it works as expected, but when i try to use it in an automation, like this:

if:
  - condition: numeric_state
    entity_id: sensor.bedroom_humidity
    above: {{states("input_number.humidity_setpoint") | float + 1 }}
then:
...

i get this error:

Message malformed: expected float for dictionary
value @ data['action'][0]['if'][0]['above']

i don’t understand what i’m doing wrong. is there a way to make this work? or do i have to use 2 input numbers, one for the upper limit and one for the lower limit?

  1. Template Editor
    You assumed that the Template Editor is able to test YAML and Jinja but in fact it only tests Jinja. Any YAML is simply treated as literal strings and is not tested for valid syntax or functionality. Even if you don’t include any YAML in the Template Editor, the Jinja template you test is subject to YAML rules wherever you copy-paste it.

  2. Numeric State Condition
    The Numeric State Condition’s above option doesn’t support a template. Even if it did, your example would still fail because it doesn’t wrap the single-line template in quotes.

I suggest you use a Template Condition.

if:
  - condition: template 
    value_template: >
      {{ states('sensor.bedroom_humidity') | float(0) > 
        states('input_number.humidity_setpoint') | float(0) + 1 }}
then:
...

I’d set the sensor default value much smaller than the input number could ever be to prevent accidental activations if it is unavailable.

if:
  - condition: template 
    value_template: >
      {{ states('sensor.bedroom_humidity') | float(-10000) > 
        states('input_number.humidity_setpoint') | float(0) + 1 }}
then:
...
1 Like

i copied that into my automation, and now it seems to work. thanks.

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.