Dynamically change update_interval

I know this is old but, I found a solution and I’d like to share:

I created an input boolean in Home Assistant to enable “high speed” update intervals for a power meter:

binary_sensor:
  - platform: homeassistant
    id: high_speed_update
    entity_id: input_boolean.high_speed_update
    on_state:
      - lambda: !lambda |-
          // This is to revert back to the default update interval when needed
          static uint32_t default_update_interval = id(my_sensor).get_update_interval();
          // 3000 = 3 secs = New "high speed" interval
          int update_interval = x ? 3000 : default_update_interval;

          id(my_sensor).set_update_interval(update_interval);
          id(my_sensor).call_setup();

I need to call call_setup because it will update the interval internally, depending on the sensor, this may have a side effect.

Changing the switch on the home assistant, it’ll configure the new interval and start updating faster, turning off, will revert.

7 Likes