Pass numeric value of trigger template to action condition template

Hi All - Been scratching my head over this one for a couple of days now so any help gratefully appreciated:

I have the following automation trigger template which works:

{{ states('sensor.temperature')|float(0) > 35 or
   states('sensor.temperature')|float(0) < 1 }}

I then have a condition to determine if the value is above or below the trigger value. This always returns false irrespective of trigger value.

{{states('trigger.to_state.state') | float(0) > 35 }}

For some reason the trigger value is not being evaluated by the condition template.

{{ trigger.to_state.state | float(0) > 35 }}

There’s no need for templates by the way. All of this can be done with numeric state triggers and conditions.

Isn’t that just a matter of taste or has hardcoding advantages my tunnelvisioned mind cannot see? (making frequent use of templates out of lazyness).

I don’t complicate things if I don’t have to.

Thanks - fully aware there’s many ways to cut the cake with this however in true HA style I’m trying to overcomplicate as far as possible. For my wider learning I would be interested to understand why my proposed approach is failing. Furthermore, what would be ‘best practice’ with regards to achieving this? Is template usage more resource intensive as the system scales?

states() needs to be supplied an an entity id, not a value. You were not only supplying it with a state (trigger.to_state.state) but you quoted the variable ('trigger.to_state.state')so it became a string.

Assume your trigger to state was 35, that would give this in your template:

{{states('trigger.to_state.state') | float(0) > 35 }}

If you had not quoted the trigger variable it would have given this (still wrong):

{{states(35) | float(0) > 35 }}

Where as this:

{{ trigger.to_state.state | float(0) > 35 }}

gives:

{{ 35 > 35 }}

Post the complete YAML for your automation.

Thank you - understood and appreciate the clear explanation.

Full automation as per below. Note this has been updated as per the solution response.

alias: _temperature test
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.temperature1
    below: 1
    id: External Temperature1
  - platform: numeric_state
    entity_id:
      - sensor.temperature2
    above: 30
    id: External Temperature2
  - alias: Template Temperature Trigger
    platform: template
    value_template: |-
      {{ states('sensor.temperature')|float(0) > 35 or
         states('sensor.temperature')|float(0) < 1 }}
    id: External Temperature3
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - External Temperature1
          - condition: state
            entity_id: binary_sensor.external_temperature1_threshold_rising
            state: "off"
        sequence:
          - service: notify.all
            data:
              message: >-
                Home {{ state_attr(trigger.entity_id, 'friendly_name') }} is {{
                trigger.to_state.state }}°c and decreasing.
      - conditions:
          - condition: trigger
            id:
              - External Temperature2
          - condition: state
            entity_id: binary_sensor.external_temperature2_threshold_rising
            state: "on"
        sequence:
          - service: notify.all
            data:
              message: >-
                ALERT! {{ state_attr(trigger.entity_id, 'friendly_name') }} is
                {{ trigger.to_state.state }}°c and rising.
          - if:
              - condition: state
                entity_id: binary_sensor.tablet
                state: "on"
            then:
              - service: notify.tablet
                data:
                  message: TTS
                  data:
                    tts_text: >-
                      ALERT! {{ state_attr(trigger.entity_id, 'friendly_name')
                      }} is {{ trigger.to_state.state }}°c and rising.
      - conditions:
          - condition: trigger
            id:
              - External Temperature3
        sequence:
          - if:
              - condition: template
                value_template: "{{ trigger.to_state.state | float(0) > 35 }}"
              - condition: state
                entity_id: binary_sensor.external_temperature3_threshold_rising
                state: "on"
            then:
              - service: notify.all
                data:
                  message: >-
                    {{ state_attr(trigger.entity_id, 'friendly_name') }} is {{
                    trigger.to_state.state }}°c and rising.
            else:
              - condition: template
                value_template: "{{ trigger.to_state.state | float(0) < 1 }}"
              - condition: state
                entity_id: binary_sensor.external_temperature3_threshold_rising
                state: "off"
              - service: notify.all
                data:
                  message: >-
                    {{ state_attr(trigger.entity_id, 'friendly_name') }} is {{
                    trigger.to_state.state }}°c and decreasing.
mode: single

If it works, it’s fine. Personally, I’d replace:

  - alias: Template Temperature Trigger
    platform: template
    value_template: |-
      {{ states('sensor.temperature')|float(0) > 35 or
         states('sensor.temperature')|float(0) < 1 }}
    id: External Temperature3

with

  - platform: numeric_state
    entity_id: sensor.temperature
    above: 35
    id: External Temperature3
  - platform: numeric_state
    entity_id: sensor.temperature
    below: 1
    id: External Temperature3

and it’ll work exactly the same but in theory need less evaluation each minute. If you give the second one a new id you can shorten some of the logic further down.

You’re aware that you won’t get a notification if the temperature is rising but your binary sensor is off? and likewise for falling / on?

So it’s more resource intensive to evaluate value templates as triggers?

Yes - aware of this, thanks.

I cannot definitively answer that, nor would it matter unless you had a lot on a very low-power system… It’s more about elegance and clarity of the coding: template triggers feel like a last resort.