I set the scan interval for the sensor to something very high e.g.
scan_interval: 86400
Then I have these automations and scripts which you could adapt for your use. They just create a loop which updates the sensor. I also have a button in Lovelace that can request a manual update for those ‘urgent-I-must-have-the-most-current-information’ situations:
automation:
#===========================================================
#=== Set up timing of sensor calls for trains
#===========================================================
- alias: train_sensor
initial_state: on
trigger:
- platform: homeassistant
event: start
action:
- service: script.set_interval_for_train_sensor
script:
#=================================================================
#=== Set interval for the train sensor
#=== Reduce sensor calls when no train is scheduled
#=================================================================
set_interval_for_train_sensor:
sequence:
- service: homeassistant.turn_off
entity_id: script.update_train_sensor
- delay: "00:00:02"
- service_template: >
script.update_train_sensor
data_template:
interval: >
{% if states('sensor.next_train_to_xxx') == "No departures" %}
00:30:00
{% else %}
00:01:00
{% endif %}
#========================================================================
#=== Update the sensor and loop the script that sets the sensor interval
#========================================================================
update_train_sensor:
sequence:
- service: homeassistant.update_entity
entity_id: sensor.next_train_to_xxx
- delay: "{{ interval }}"
- service: script.set_interval_for_train_sensor
It was a while ago but think that for my sensor (UK) at the end of the daily schedule it would sometimes report that the next train was in a stupidly long time but then have no actual data for the next train. I got around this by adding a line to my script.
interval: >
{% if states('sensor.next_train_to_xxx') == "No departures" or
states('sensor.next_train_to_xxx') | int > 30 %}
00:30:00