Use Distance on Change

I’m trying to write an automation to calculate the distance when the distance changes.

I have the distance integration which is fine, and I know its not 100% accurate, and thats fine, I just need a rough guide.
However, on the tile tracker, it is the attribute that changes, not the state, so running into an error with my automation.
Here it is:

- alias: 'Car Change Distance'
  id: 046ffbf2-737b-44fd-8d03-253ebd418b05
  mode: single
  max_exceeded: silent
  trigger: 
    - platform: state
      entity_id: device_tracker.car
      attribute:
        - latitude
        - longitude
  action:
    - service: input_number.set_value
      entity_id: input_number.car_distance_charge
      data:
        value: "{{ distance(('trigger.from_state.attributes.longitude', 'trigger.from_state.attributes.latitude'), 'trigger.to_state.entity_id') }}"

Any help with this one would be appreciated.

Why not just use the proximity integration?

That could be the other solution afterward if I can’t get this working.

I suspect the inner quotes (') are your issue here

I’ve removed those, and this is the error I get:

Error while processing state change for device_tracker.car
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/event.py", line 288, in _async_state_change_dispatcher
    hass.async_run_hass_job(job, event)
  File "/usr/src/homeassistant/homeassistant/core.py", line 574, in async_run_hass_job
    hassjob.target(*args)
  File "/usr/src/homeassistant/homeassistant/components/homeassistant/triggers/state.py", line 143, in state_automation_listener
    old_value = from_s.attributes.get(attribute)
TypeError: unhashable type: 'NodeListClass'

Have you tried using the to and from lat and longs instead of the entity id? Also, I think the () and ' ' mentioned by @Tinkerer are both issues.

action:
  - service: input_number.set_value
    entity_id: input_number.car_distance_charge
    data:
      value: "{{ distance(trigger.from_state.attributes.latitude, trigger.from_state.attributes.longitude, trigger.to_state.attributes.latitude, trigger.to_state.attributes.longitude }}"

Still getting the same error.

Are you sure that using a list of attributes is supported in state triggers? There are no examples of that in the docs… it also seems gratuitous, what is the likelihood that the GPS is reliable enough, accurate enough, and you travel in exactly a cardinal direction? Try it with just one attribute.

The docs say you can provide a longitude, latitude and then an entity with those attributes.

I’m not looking hugely reliable, and I know its straight-line, but just looking to find out roughly the distance so to know the likelyhood of needing to charge the car.

Looking at the message, I think it’s because the the from_state doesn’t give the previous attributes