Complex automation Not Firing - template help

Tags: #<Tag:0x00007f7391a63b30> #<Tag:0x00007f7391a636d0>

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 %}

As I write this my sensor.david_distance_to_home is outputting 0.03 but no trigger of automation. It should be populating the input_text with “Arrived Home’

Ok, not much luck so far. I have updated the code as follows, and it is now half working. Only the root “else” is ffiring in the actions. The root {% if… is still not sending comands to update the input_text. I will open up a seperate question for what I believe is the problem.

 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 %}