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

Hi jata,

I’ve been searching for ways to update a service when needed, rather than constantly - such as using the home assistant entity update service - and I noticed you had some luck with that on this thread.

In my case I’m using the UK Transport API - looks like I’m the only one using it :rofl: - it works well for me. But I obviously only want it to update and provide the times when I need them, not constantly, otherwise it would cost a fortune every month.

I’m trying to figure out how to me the yaml script only update and call on the API when I need the times, rather than several times an hour, and wondered if I could adapt your example for this.

I was considering having a button on my timetable tab to press when I want to call on the API to update the entities with the next bus/train times.

I’m trying to figure out where in the yaml I would need to enter this, and what I would need to put in there. Do you have any tips by any chance?

I’m quite new to HA and Yaml esp. No worries if not

Sorry @CymruDad78 I missed this reply/question.

Have you solved this or do you still need help (I am a learner too but will help if I can).

You can use a button with an action to call a service (or an automation) to force an update

Or another way would be to use a trigger in a template. This works well if there are a number of different scenarios that you want to force the update.