it’s ok
To test if it works we will back to states.sensor
So, if I would create this blueprint, I will try to reduce CPU a bit reducing the number of queries to the states, and will prevent the case where the value was already above the threshold when HA starts or the automation is loaded.
Something like this (please change states.sensor to states only for your tests as this increases a lot the CPU consumption):
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
trigger_variables:
GasSensor: !input Gas_Sensor
GasSensor_val_list: "{{ states.sensor | selectattr('entity_id', 'in', GasSensor) | map(attribute='state') | map('float') | list }}"
GasSensor_val_max: "{{ GasSensor_val_list | max if GasSensor_val_list | count > 0 else 'unknown' }}"
#Alarmlevel: !input Alarm_level
Warninglevel: !input Warning_level
trigger:
#- platform: template
# id: Alarm
# value_template: >-
# {{states(GasSensor) >= states(Alarmlevel)}}
- id: Warning
platform: template
value_template: "{{ is_number(GasSensor_val_max) and is_number(states(Warninglevel)) and GasSensor_val_max >= states(Warninglevel) | float }}"
- id: HA Start
platform: homeassistant
event: start
- id: Automations reloaded
platform: event
event_type: automation_reloaded
condition:
- condition: template
value_template: "{{ is_number(GasSensor_val_max) and is_number(states(Warninglevel)) and GasSensor_val_max >= states(Warninglevel) | float }}"
action:
- service: script.turn_on
target: !input target_Script
Unfortunately, it didn’t work
Logs? Trace?
Log Details (ERROR)
Logger: homeassistant.components.automation.1111new_automation
Source: components/automation/__init__.py:603
Integration: Automation (documentation, issues)
First occurred: 1:48:56 am (1 occurrences)
Last logged: 1:48:56 am
Error rendering trigger variables: TypeError: 'function' object is not iterable
mode: restart
trigger_variables:
GasSensor:
- input_number.test_1
- input_number.test_2
GasSensor_val_list: >-
{{ states | selectattr('entity_id', 'in', GasSensor) |
map(attribute='state') | map('float') | list }}
GasSensor_val_max: >-
{{ GasSensor_val_list | max if GasSensor_val_list | count > 0 else 'unknown'
}}
Warninglevel: input_number.lpg_gas_warning_level
trigger:
- id: Warning
platform: template
value_template: >-
{{ is_number(GasSensor_val_max) and is_number(states(Warninglevel)) and
GasSensor_val_max >= states(Warninglevel) | float }}
- id: HA Start
platform: homeassistant
event: start
- id: Automations reloaded
platform: event
event_type: automation_reloaded
condition:
- condition: template
value_template: >-
{{ is_number(GasSensor_val_max) and is_number(states(Warninglevel)) and
GasSensor_val_max >= states(Warninglevel) | float }}
action:
- service: script.turn_on
target:
entity_id: script.light_toggle
id: '1678832259257'
alias: 1111New Automation
description: ''
id: '1678832259257'
alias: 1111New Automation
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
I think if you have time you can do the same setup by add 4 input_number and test code
I will do that tomorrow.
Please one more try before I go to sleep:
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
trigger_variables:
GasSensor: !input Gas_Sensor
GasSensor_val_list: "{{ states.sensor | selectattr('entity_id', 'in', GasSensor | default([])) | map(attribute='state') | map('float') | list }}"
GasSensor_val_max: "{{ GasSensor_val_list | max if GasSensor_val_list | count > 0 else 'unknown' }}"
#Alarmlevel: !input Alarm_level
Warninglevel: !input Warning_level
trigger:
#- platform: template
# id: Alarm
# value_template: >-
# {{states(GasSensor) >= states(Alarmlevel)}}
- id: Warning
platform: template
value_template: "{{ is_number(GasSensor_val_max) and is_number(states(Warninglevel)) and GasSensor_val_max >= states(Warninglevel) | float }}"
- id: HA Start
platform: homeassistant
event: start
- id: Automations reloaded
platform: event
event_type: automation_reloaded
condition:
- condition: template
value_template: "{{ is_number(GasSensor_val_max) and is_number(states(Warninglevel)) and GasSensor_val_max >= states(Warninglevel) | float }}"
action:
- service: script.turn_on
target: !input target_Script
Logger: homeassistant.components.automation.547new_automation
Source: components/automation/__init__.py:442
Integration: Automation (documentation, issues)
First occurred: 3:19:36 am (1 occurrences)
Last logged: 3:19:36 am
Error rendering trigger variables: TypeError: 'function' object is not iterable
mode: restart
trigger_variables:
GasSensor:
- input_number.test_1
- input_number.test_2
GasSensor_val_list: >-
{{ states | selectattr('entity_id', 'in', GasSensor | default([])) |
map(attribute='state') | map('float') | list }}
GasSensor_val_max: >-
{{ GasSensor_val_list | max if GasSensor_val_list | count > 0 else 'unknown'
}}
Warninglevel: input_number.lpg_gas_warning_level
trigger:
- id: Warning
platform: template
value_template: >-
{{ is_number(GasSensor_val_max) and is_number(states(Warninglevel)) and
GasSensor_val_max >= states(Warninglevel) | float }}
- id: HA Start
platform: homeassistant
event: start
- id: Automations reloaded
platform: event
event_type: automation_reloaded
condition:
- condition: template
value_template: >-
{{ is_number(GasSensor_val_max) and is_number(states(Warninglevel)) and
GasSensor_val_max >= states(Warninglevel) | float }}
action:
- service: script.turn_on
target:
entity_id: script.light_toggle
id: '1678839571055'
alias: 547New Automation
description: ''
This worked for me:
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
trigger_variables:
GasSensor: !input Gas_Sensor
#Alarmlevel: !input Alarm_level
Warninglevel: !input Warning_level
trigger:
#- platform: template
# id: Alarm
# value_template: >-
# {{states(GasSensor) >= states(Alarmlevel)}}
- id: Warning
platform: template
value_template: "{{ states.sensor | selectattr('entity_id', 'in', GasSensor) | map(attribute='state') | map('float') | list | max >= states(Warninglevel) | float }}"
- id: HA Start
platform: homeassistant
event: start
- id: Automations reloaded
platform: event
event_type: automation_reloaded
condition:
- condition: template
value_template: "{{ states.sensor | selectattr('entity_id', 'in', GasSensor | default([])) | map(attribute='state') | map('float') | list | max >= states(Warninglevel) | float }}"
action:
- service: script.turn_on
target: !input target_Script
It’s interesting that states.sensor isn’t working on trigger variables. I’m pretty sure this worked before.
thanks @EdwardTFN its works but I face a new problem
let’s say I have 3 sensors attached to
Gas_Sensor:
name: Gas Sensor
description: Select Gas Sensor entity.
selector:
entity:
domain: sensor
multiple: true
according to temple
value_template: "{{ states.sensor | selectattr('entity_id', 'in', GasSensor) | map(attribute='state') | map('float') | list | max >= states(Warninglevel) | float }}"
if sensor 1 state >= states(Warninglevel) = true -----> push notification
if sensor 2 state after 1s >= states(Warninglevel) = true ------ > that means trigger not change at all and will not push the notification
the only way to push notifications once again is for all sensors < states(Warninglevel) = false
and start over
How to fix that ?
I still thinking you are over complicating this by choosing to not use a numeric state trigger, which will manage this new problem in a very easy way and do all the work for you.
I understand that numeric state is a “>
” logic, while you are looking for a “>=
” logic, but if we are talking about real sensors that shouldn’t be an issue, as any 0.00001 above the threshold will be sufficient to trigger your automation.
But, if you still wanna try to find a way to do >=
, then a template trigger might not work well, as this is in the end checking if the result of the template is false
or true
(or 0 vs non-zero).
If you don’t change the sensors all the time and are OK about leaving the list of sensors hard-coded, I would suggest to create a template sensor to return the number of sensors above the threshold, so you use this new sensor in a state based trigger in your automation.
In order to make this a bit more user friendly, you can, instead of hard coding the sensor list, to create a new sensor group helper with all the sensors you have and then use this helper as the source for this same Template sensor I’ve mentioned before. The advantage here is that you can manage the sensors in the group using only the UI. This definitely would be my way to go (if I really couldn’t use the numeric state trigger).
I will use temperature sensors in my example, to make my life easier, as I don’t have gas sensors at home, but your solution could be something like this:
For the group sensor (under Settings > Devices & Settings, tab Helpers):
Then you create a template sensor like this:
template:
- sensor:
- name: Temperature sensors above threshold
unique_id: b843fc05-b242-4179-9199-53208179e077
availability: "{{ is_number(states('input_number.temperature_threshold')) and is_number(states('sensor.max_temperature')) }}"
state: >
{{
expand('sensor.max_temperature')
| map(attribute='state')
| map('float')
| selectattr(None, '>=', states('input_number.temperature_threshold') | float )
| list
| count
}}