Update a template sensor and attributes on demand

I’m trying to create a template sensor that only updates when I use the update entity service. The issue is that the attributes for the sensor are changing frequently and these changes are making the sensor update.

What i need is for the sensor to only update on demand and at this point update the attributes from the source. See below from template.yaml. Any ideas appreciated.

I think I need to take a different approach to setting the attributes from within the script (or probably there is a much better way)…

- sensor:
    - name: "Jago iCloud GPS"
      unique_id: jago_icloud_gps
      icon: "mdi:map-marker-radius"
      state: "{{ states('device_tracker.jago_comp') }}"            
      attributes:
        latitude: "{{ state_attr('device_tracker.jago_iphone', 'latitude') }}"
        longitude: "{{ state_attr('device_tracker.jago_iphone', 'longitude') }}"
        gps_accuracy: "{{ state_attr('device_tracker.jago_iphone', 'gps_accuracy') }}"
        last_timestamp: "{{ state_attr('device_tracker.jago_iphone', 'last_timestamp') }}"

For context - below is a chart that shows the issue. Source GPS data updates frequently and screws up the speed calculations - I need to fix this - but I can’t control the source sensor update frequency. It automatically starts high frequency GPS updates when I get within 1km of my house.

Create a triggered template sensor with a trigger that never occurs, or triggers when something you cause happens, like your current automation frequency triggers (whatever they are), then you would need one less automation.

e.g. configuration.yaml:

template:
  - trigger:
      - platform: template
        value_template: "{{ false }}"
    sensor:
      - name: "Jago iCloud GPS"
        unique_id: jago_icloud_gps
        icon: "mdi:map-marker-radius"
        state: "{{ states('device_tracker.jago_comp') }}"            
        attributes:
          latitude: "{{ state_attr('device_tracker.jago_iphone', 'latitude') }}"
          longitude: "{{ state_attr('device_tracker.jago_iphone', 'longitude') }}"
          gps_accuracy: "{{ state_attr('device_tracker.jago_iphone', 'gps_accuracy') }}"
          last_timestamp: "{{ state_attr('device_tracker.jago_iphone', 'last_timestamp') }}"

Thanks @tom_l. I will give this a try. I’m think I’m so close to solving this problem.

With the above trigger example, is it correct that the sensor will not update even if the attributes change?

And I can still force it to update using the update entity service?

Thats right. It will only update when triggered.

As I said you can use the always false trigger and the entity update service or replace that trigger with whatever trigger(s) you are using in the automation now that calls the entity update service.

1 Like

Got it. I will trigger on state change to ‘off’ on the script and then remove the entity update from the script as it will no longer be needed

Looks like the trigger is working correctly. One last question. I am trying to add the last time the script ran - see below. It is working but in wrong timezone. Is there a better way?

- trigger:
  - platform: state
    entity_id: script.jago_icloud_gps_update
    from:
      - 'on'
    to: 
      - 'off'
  sensor:
    - name: "Jago iCloud GPS"
      unique_id: jago_icloud_gps
      icon: "mdi:map-marker-radius"
      state: "{{ states('device_tracker.jago_comp') }}"            
      attributes:
        latitude: "{{ state_attr('device_tracker.jago_iphone', 'latitude') }}"
        longitude: "{{ state_attr('device_tracker.jago_iphone', 'longitude') }}"
        gps_accuracy: "{{ state_attr('device_tracker.jago_iphone', 'gps_accuracy') }}"
        last_timestamp: "{{ state_attr('device_tracker.jago_iphone', 'last_timestamp') }}"
        last_ran: "{{ states.sensor.jago_icloud_gps.last_updated }}"
last_ran: "{{ states.sensor.jago_icloud_gps.last_updated|as_local }}"
1 Like

Thanks @tom_l Really appreciate the help.

Next time I drive i can see if this solves the problem that i have.

1 Like