Blueprint trigger template

blueprint:
  name: Gas Sensor1
  description: Gas sensor with Warning and Alarm levels
  domain: automation

  input:
  
    Gas_Sensor:
      name:  Gas Sensor 
      description: Select Gas Sensor entity.            
      selector:
        entity:
#          domain: sensor
          multiple: true            

#    Alarm_level:
#      name: Alarm level
#      description: Select Alarm level entity.
#      selector:
#        entity:
#          domain: input_number
            
    Warning_level:
      name: Warning Level
      description: Select Warning Level entity.
      selector:
        entity:
          domain: input_number 
  
    target_Script:
      name: Script
      description: Select Script entity.
      selector:
        target:
          entity:
            domain: script
            

mode: restart


variables:
  GasSensor: !input Gas_Sensor
#  Alarmlevel: !input Alarm_level
  Warninglevel: !input Warning_level

  
trigger:

#  - platform: template
#    id: Alarm
#    value_template: >-
#      {{states(GasSensor) >= states(Alarmlevel)}}
      
  - platform: template
    id: Warning
    value_template: >-
      {{ states(GasSensor) >= states(Warninglevel) }}
      

action:
   - service: script.turn_on
     target: !input target_Script 

log

1-
Logger: homeassistant.components.template.trigger
Source: components/template/trigger.py:68
Integration: Template (documentation, issues)
First occurred: 7:50:22 pm (1 occurrences)
Last logged: 7:50:22 pm

Error evaluating 'template' trigger for 'ttttt': UndefinedError: 'GasSensor' is undefined

2- 
Logger: homeassistant.helpers.event
Source: helpers/template.py:460
First occurred: 7:50:22 pm (1 occurrences)
Last logged: 7:50:22 pm

Error while processing template: Template("{{ states(GasSensor) >= states(Warninglevel) }}")
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 458, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2007, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.10/site-packages/jinja2/sandbox.py", line 393, in call
    return __context.call(__obj, *args, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 744, in __call__
    state = _get_state(self._hass, entity_id)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 990, in _get_state
    return _get_template_state_from_state(hass, entity_id, hass.states.get(entity_id))
  File "/usr/src/homeassistant/homeassistant/core.py", line 1406, in get
    return self._states.get(entity_id.lower())
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2033, in _fail_with_undefined_error
    raise ex
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2024, in _fail_with_undefined_error
    return super()._fail_with_undefined_error(*args, **kwargs)
jinja2.exceptions.UndefinedError: 'GasSensor' is undefined

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 576, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 460, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: UndefinedError: 'GasSensor' is undefined

how can I fix code?

You can use a numeric state trigger in this case and make your life much easierā€¦

trigger:
  - platform: numeric_state
    entity_id: !input gas_sensor
    above: !input warning_level

Hi,

I think the error is caused because you use

variables:

Instead of

trigger_variables:

to define your variables. Try it and see if it works.

1 Like

Dear @EdwardTFN thanks for your repay I donā€™t use platform: numeric_state because I need state to be >= warning_level not above

Hi @DarkWarden thanks for your reply

please can you give me an example Iā€™m not good at this type of variables

Just replace the word

variables:

with

trigger_variables:

and leave the rest as is.

@DarkWarden
log:

1-

Logger: homeassistant.components.template.trigger
Source: components/template/trigger.py:68
Integration: Template (documentation, issues)
First occurred: 9:10:31 pm (1 occurrences)
Last logged: 9:10:31 pm

Error evaluating 'template' trigger for '13435': AttributeError: 'list' object has no attribute 'lower'

2-

Logger: homeassistant.helpers.event
Source: helpers/template.py:460
First occurred: 9:10:30 pm (1 occurrences)
Last logged: 9:10:30 pm

Error while processing template: Template("{{ states(GasSensor) >= states(Warninglevel) }}")
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 458, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2007, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.10/site-packages/jinja2/sandbox.py", line 393, in call
    return __context.call(__obj, *args, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 744, in __call__
    state = _get_state(self._hass, entity_id)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 990, in _get_state
    return _get_template_state_from_state(hass, entity_id, hass.states.get(entity_id))
  File "/usr/src/homeassistant/homeassistant/core.py", line 1406, in get
    return self._states.get(entity_id.lower())
AttributeError: 'list' object has no attribute 'lower'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 576, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 460, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: AttributeError: 'list' object has no attribute 'lower'

Ok, at least the error of the variables not being defined has gone away, so you are a step further.
The next problem is that you are trying to compare the states of gas_sensor and warning_level like comparing numbers. The template however does not automatically convert the states to numbers for you, you have to do this yourself by using the float filter.

Try if this works:

value_template: >-
      {{ states(GasSensor)|float >= states(Warninglevel)|float }}

@DarkWarden

Log Details (ERROR)
Logger: homeassistant.helpers.event
Source: helpers/template.py:460
First occurred: 9:33:17 pm (1 occurrences)
Last logged: 9:33:17 pm

Error while processing template: Template("{{ states(GasSensor)|float >= states(Warninglevel)|float }}")
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 458, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2007, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.10/site-packages/jinja2/sandbox.py", line 393, in call
    return __context.call(__obj, *args, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 744, in __call__
    state = _get_state(self._hass, entity_id)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 990, in _get_state
    return _get_template_state_from_state(hass, entity_id, hass.states.get(entity_id))
  File "/usr/src/homeassistant/homeassistant/core.py", line 1406, in get
    return self._states.get(entity_id.lower())
AttributeError: 'list' object has no attribute 'lower'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 576, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 460, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: AttributeError: 'list' object has no attribute 'lower'

You should provide a default value for the float conversionā€¦

Try this:

value_template: >-
      {{ states(GasSensor) | float(-999) >= states(Warninglevel) | float(999) }}

Or this:

value_template: >-
      {{ 
        is_number(GasSensor) and
        is_number(Warninglevel) and
        states(GasSensor) | float(-999) >= states(Warninglevel) | float(999)
      }}

By the way, this template trigger will fail with rror if you have multiple entities selected, as you will be passing a list to states, which is not supported.
We can find an alternative, but before that, how you want the system to behave when multiple entities are selected? Trigger if all entities are above the threshold or if any of the entities are above the threshold?

Could you please share the yaml of the automation using this Blueprint in your tests?

Yeah ok,

I see that I was a bit quick when saying that you should just replace variables with trigger_variables, my bad.
You have a list of gas sensor entities because you use

multiple: true

in its definition. Handling lists in templates goes above my knowledge level :sweat:
Sorry I canā€™t help you any further

ŁA : Any of the entities are >= the threshold

Try this:

value_template: >
  {{ 
    ( 
      states.sensor 
      | selectattr('entity_id', 'in', GasSensor) 
      | map(attribute='state') 
      | map('float') 
      | list 
      | count > 0
    ) and
    is_number(Warninglevel) and
    ( 
      states.sensor 
      | selectattr('entity_id', 'in', GasSensor) 
      | map(attribute='state') 
      | map('float') 
      | list 
      | max
    ) >= states(Warninglevel) | float 
  }}

@EdwardTFN

trigger:

#  - platform: template
#    id: Alarm
#    value_template: >-
#      {{states(GasSensor) >= states(Alarmlevel)}}
      
  - platform: template
    id: Warning
    value_template: >-
      {{ 
        ( 
         states.sensor 
         | selectattr('entity_id', 'in', GasSensor) 
         | map(attribute='state') 
         | map('float') 
         | list 
         | count > 0
        ) and
        is_number(Warninglevel) and
        ( 
         states.sensor 
         | selectattr('entity_id', 'in', GasSensor) 
         | map(attribute='state') 
         | map('float') 
         | list 
         | max
        )  >= states(Warninglevel) | float 
      }}

Never triggered and no Err in log

Could you please share the yaml of the automation using this blueprint in your tests?
If you can capture the screen of the latest trace (whit the step related to that trigger selected) it might help.

Also, could you please go to Developer Tools and select the tab State, then double check that at least one entity selected to GasSensor and also the input number select for Warninglevel have some value?

@EdwardTFN

the Idea is I have more than 12 smoke and gas DIY sensor in the house and we are 7 users
So I decided to make a blueprint for notifying users if any of the sensors have been triggered with two levels of notification Warning and Alarm by using two input_number for each type of sensor

if any sensor level is >= Warning or Alarm send a notification

for that I use

    Gas_Sensor:
      name:  Gas Sensor 
      description: Select Gas Sensor entity.            
      selector:
        entity:
          domain: sensor
          multiple: true

multiple: true
help me to add more sensors in one blueprint

Note: for testing purposes, I use the other two input_number as sensors to simulate the gas sensor

input_number Warninglevels = 5
input_number Alarmlevels = 10

input_number as gas sensor1 range = 0-20
input_number as gas sensor2 range = 0-20

I got the point, Iā€™m just trying to understand where the problem is that it is not triggering the automation, so I want to see the result of the traceā€¦

But based oj what you just said, Iā€™m thinkingā€¦ If you are using the input number entity itself, then your template will result true from the beginning and will never be false, preventing your automation to run.
An automation runs when the trigger change its value from false to true. If it is always true or always false, it will never run.

@EdwardTFN for the trace is ā€œNo traces foundā€ because it Never triggered

but if I do it by press Run

mode: restart
trigger_variables:
  GasSensor:
    - input_number.test_1
    - input_number.test_2
  Warninglevel: input_number.lpg_gas_warning_level
trigger:
  - platform: template
    id: Warning
    value_template: |-
      {{ 
        ( 
         states.sensor 
         | selectattr('entity_id', 'in', GasSensor) 
         | map(attribute='state') 
         | map('float') 
         | list 
         | count > 0
        ) and
        is_number(Warninglevel) and
        ( 
         states.sensor 
         | selectattr('entity_id', 'in', GasSensor) 
         | map(attribute='state') 
         | map('float') 
         | list 
         | max
        )  >= states(Warninglevel) | float 
      }}
action:
  - service: script.turn_on
    target:
      entity_id: script.light_toggle
id: '1678823213206'
alias: '12345'
description: ''
id: '1678823213206'
alias: '12345'
description: ''
use_blueprint:
  path: my/Gas Sensor - Copy.yaml
  input:
    Gas_Sensor:
      - input_number.test_1
      - input_number.test_2
    Warning_level: input_number.lpg_gas_warning_level
    target_Script:
      entity_id: script.light_toggle

Ok, the problem is that my template will look for entities only on the sensor domain, and you are using entities in the input_number domain.
I will change that, but probably will be a bit more CPU consumption compared to how it is now.

Try this:

trigger:

#  - platform: template
#    id: Alarm
#    value_template: >-
#      {{states(GasSensor) >= states(Alarmlevel)}}
      
  - platform: template
    id: Warning
    value_template: >-
      {{ 
        ( 
         states
         | selectattr('entity_id', 'in', GasSensor) 
         | map(attribute='state') 
         | map('float') 
         | list 
         | count > 0
        ) and
        is_number(Warninglevel) and
        ( 
         states
         | selectattr('entity_id', 'in', GasSensor) 
         | map(attribute='state') 
         | map('float') 
         | list 
         | max
        )  >= states(Warninglevel) | float 
      }}