I am working on making a “better” or “more intelligent” device tracker. I am however having touble with my templates. This automation is one piece of the puzzle. It takes in distance from a sensor and should populate one of three input_text entities. Can someone reed through my templates and let me know if you see ang syntax or basic coding errors. I would really apreciate it.
- 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: template
value_template: >
{{ as_timestamp(states('sensor.date_time').replace(',', ''))|int - as_timestamp(states.sensor.david_distance_to_home.last_changed)|int >= 180
or as_timestamp(states('sensor.date_time').replace(',', ''))|int - as_timestamp(states.sensor.renata_distance_to_home.last_changed)|int >= 180
or as_timestamp(states('sensor.date_time').replace(',', ''))|int - as_timestamp(states.sensor.cynthia_distance_to_home.last_changed)|int >= 180 }}
condition:
condition: or
conditions:
- condition: template
value_template: >
{% if as_timestamp(states('sensor.date_time').replace(',', ''))|int - as_timestamp(trigger.to_state.last_changed)|int < 180
and trigger.from_state.state < trigger.to_state.state
and trigger.to_state.state > 0.25 %}
true
{% elif as_timestamp(states('sensor.date_time').replace(',', ''))|int - as_timestamp(trigger.to_state.last_changed)|int < 180
and trigger.from_state.state > trigger.to_state.state
and trigger.to_state.state > 0.25 %}
true
{% elif trigger.to_state.state <= 0.25 %}
true
{% else %}
false
{% endif %}
- condition: template
value_template: >
{% if trigger.to_state.entity_id == 'sensor.david_distance_to_home'
and as_timestamp(states('sensor.date_time').replace(',', ''))|int - as_timestamp(trigger.to_state.last_changed)|int >= 180
and trigger.to_state.state > 0.25
and state('input_text.david_motion_state') != 'Stationary' %}
true
{% elif trigger.to_state.entity_id == 'sensor.renata_distance_to_home'
and as_timestamp(states('sensor.date_time').replace(',', ''))|int - as_timestamp(trigger.to_state.last_changed)|int >= 180
and trigger.to_state.state > 0.25
and state('input_text.renata_motion_state') != 'Stationary' %}
true
{% elif trigger.to_state.entity_id == 'sensor.cynthia_distance_to_home'
and as_timestamp(states('sensor.date_time').replace(',', ''))|int - as_timestamp(trigger.to_state.last_changed)|int >= 180
and trigger.to_state.state > 0.25
and state('input_text.cynthia_motion_state') != 'Stationary' %}
true
{% else %}
false
{% endif %}
action:
- service: input_text.set_value
data_template:
entity_id: "input_text.{{ trigger.to_state.entity_id.split('.')[1].split('_')[0] }}_motion_state"
value: >-
{% if as_timestamp(states('sensor.date_time').replace(',', ''))|int - as_timestamp(trigger.to_state.last_changed)|int < 180
and trigger.from_state.state < trigger.to_state.state
and trigger.to_state.state > 0.25 %}
{{ "Away From" }}
{% elif as_timestamp(states('sensor.date_time').replace(',', ''))|int - as_timestamp(trigger.to_state.last_changed)|int < 180
and trigger.from_state.state > trigger.to_state.state
and trigger.to_state.state > 0.25 %}
{{ "Towards" }}
{% elif trigger.to_state.state <= 0.25 %}
{{ "Arrived Home" }}
{% else %}
{{ "Stationary" }}
{% endif %}