dave85stmn
(David Mehoves)
August 10, 2020, 2:04am
1
I need to use a version of the following code as the start of my action sequence. Is there a correct way to format this. See triggers (later) for reference.
Again my goal is to be able to ask if a certan entity_id is one of a named group of them, referenced by trigger.entity_id (refer to my value template)
action:
- service: input_text.set_value
data_template:
entity_id: "input_text.{{trigger.entity_id.split('.')[1].split('_')[0]}}_motion_state"
value: >
{% if trigger.entity_id in ['sensor.david_distance_to_home', 'sensor.renata_distance_to_home', 'sensor.cynthia_distance_to_home'] %}
- alias: Set distance input when state change
trigger:
- platform: state
entity_id:
- sensor.david_distance_to_home
- sensor.renata_distance_to_home
- sensor.cynthia_distance_to_home
- platform: numeric_state
entity_id: sensor.david_dist_update
above: 5
for:
seconds: 30
- platform: numeric_state
entity_id: sensor.renata_dist_update
above: 5
for:
seconds: 30
- platform: numeric_state
entity_id: sensor.cynthia_dist_update
above: 5
for:
seconds: 30
KTibow
(Kendell R)
August 10, 2020, 2:18am
2
action:
- service: input_text.set_value
data_template:
entity_id: "input_text.{{trigger.entity_id.split('.')[1].split('_')[0]}}_motion_state"
value: >
{{ trigger.entity_id in ['sensor.david_distance_to_home', 'sensor.renata_distance_to_home', 'sensor.cynthia_distance_to_home'] }}
dave85stmn
(David Mehoves)
August 10, 2020, 2:21am
3
Unless I am missing something, that is about the same as I had with the exception that this iss a {{ }} instead of the {% if… %}
here is the whole code for context.
- alias: Set distance input when state change
trigger:
- platform: state
entity_id:
- sensor.david_distance_to_home
- sensor.renata_distance_to_home
- sensor.cynthia_distance_to_home
- platform: numeric_state
entity_id: sensor.david_dist_update
above: 5
for:
seconds: 30
- platform: numeric_state
entity_id: sensor.renata_dist_update
above: 5
for:
seconds: 30
- platform: numeric_state
entity_id: sensor.cynthia_dist_update
above: 5
for:
seconds: 30
mode: queued
action:
# - service: notify.mobile_app_davids_phone
# data_template:
# title: "{{ trigger.entity_id }}"
# message: "{{ trigger.to_state.state }}"
- service: input_text.set_value
data_template:
entity_id: "input_text.{{trigger.entity_id.split('.')[1].split('_')[0]}}_motion_state"
value: >
{% if trigger.entity_id in ['sensor.david_distance_to_home', 'sensor.renata_distance_to_home', 'sensor.cynthia_distance_to_home'] %}
{% if trigger.entity_id == 'sensor.david_distance_to_home' %}
{% if states('sensor.david_distance_to_home') > 0.25 %}
{% if trigger.from_state.state > trigger.to_state.state %}
{{ 'Towards' }}
{% elif trigger.from_state.state < trigger.to_state.state %}
{{ 'Away From' }}
{% else %}
{{ 'Stationary 1' }}
{% endif %}
{% else %}
{{ 'Arrived Home 1' }}
{% endif %}
{% elif trigger.entity_id == 'sensor.renata_distance_to_home' %}
{% if states('sensor.renata_distance_to_home') > 0.25 %}
{% if trigger.from_state.state > trigger.to_state.state %}
{{ 'Towards' }}
{% elif trigger.from_state.state < trigger.to_state.state %}
{{ 'Away From' }}
{% else %}
{{ 'Stationary 1' }}
{% endif %}
{% else %}
{{ 'Arrived Home 1' }}
{% endif %}
{% else %}
{% if states('sensor.cynthia_distance_to_home') > 0.25 %}
{% if trigger.from_state.state > trigger.to_state.state %}
{{ 'Towards' }}
{% elif trigger.from_state.state < trigger.to_state.state %}
{{ 'Away From' }}
{% else %}
{{ 'Stationary 1' }}
{% endif %}
{% else %}
{{ 'Arrived Home 1' }}
{% endif %}
{% endif %}
{% else %}
{% if trigger.entity_id == 'sensor.david_dist_update' %}
{% if states('sensor.david_distance_to_home')|float <= 0.25 %}
{{ 'Arrived Home 2'}}
{% else %}
{{ 'Stationary 2' }}
{% endif %}
{% elif trigger.entity_id == 'sensor.renata_dist_update' %}
{% if states('sensor.renata_distance_to_home')|float <= 0.25 %}
{{ 'Arrived Home 2' }}
{% else %}
{{ 'Stationary 2' }}
{% endif %}
{% else %}
{% if states('sensor.cynthia_distance_to_home')|float <= 0.25 %}
{{ 'Arrived Home 2' }}
{% else %}
{{ 'Stationary 2' }}
{% endif %}
{% endif %}
{% endif %}