Allow dynamically changing scan_interval

It would be nice to be able to use templates against scan_interval or a service call to change it.

For example, I want to poll something every 120 seconds in the morning (07:00-10:00) but outside of that 600 seconds.

Considering all of the people using HASS it would probably reduce the load of all the IoT and API providers we all use… or maybe increase it, who knows :slight_smile: But at least we’d have more control.

I think I read somewhere that scan_interval is slowly being phased out in favour of the homeassistant.update_entity service. Assuming that your entity supports this, you can implement an automation that is triggered through a time_pattern at specific intervals depending on the time of the day, or by adding other conditions like state of the sun.

1 Like

Do all entities support homeassistant.update_entity?

I don’t see how it would make sense to get rid of scan_interval. That would mean everything you want to update would require an automation.

+1 for this.

In case it never happens I do it this way

#================
#=== Automations
#================
automation:
  #===========================================================
  #=== Set up timing of sensor calls for trains to XXX
  #===========================================================
  - alias: xxx_trains_sensor
    initial_state: on
    trigger:
      - platform: homeassistant
        event: start

    action:
      - service: script.set_interval_for_xxx_train_sensor


#============
#=== Scripts
#============
script:
  #=================================================================
  #=== Set interval for the XXX train sensor
  #=== Reduce sensor calls when no train to XXX is scheduled
  #===   1000 api calls allowed per day
  #===   equal to once every 87 seconds
  #=================================================================
  set_interval_for_xxx_train_sensor:
    sequence:
      - service: homeassistant.turn_off
        entity_id: script.update_xxx_train_sensor

      - delay: "00:00:02"

      - service_template: >
          script.update_xxx_train_sensor
        data_template:
          interval: >
            {% if states('sensor.next_train_to_xxx') == "No departures" or
                  states('sensor.next_train_to_xxx') | int > 30 %}
              00:30:00
            {% else %}
              00:01:00
            {% endif %}

  #========================================================================
  #=== Update the sensor and loop the script that sets the sensor interval 
  #========================================================================
  update_xxx_train_sensor:
    sequence:
      - service: homeassistant.update_entity
        entity_id: sensor.next_train_to_XXX

      - delay: "{{ interval }}"

      - service: script.set_interval_for_xxx_train_sensor


Eventually this service calls the entity’s update or async_update method, so I would expect that all entities support this.

Sorry, I can’t find the source anymore; may have been a comment in a PR; it may have been specific to certain types of entities, like the ones calling an external API.