Ultrasonic Sensor (HC-SR04) change update interval via Home Assistant?

Ok one variant that works is to create an input_boolean toggle in Home assistant:

And link it to a binary sensor in the ESP Home yaml:

sensor:
  - platform: ultrasonic
    trigger_pin: 10
    echo_pin: 1
    name: "Ultrasonic Sensor"
    id: ultrasonic_sensor
    update_interval: 60s

binary_sensor: 
  - platform: homeassistant
    id: high_speed_update
    entity_id: input_boolean.high_speed_update
    on_state:
      - lambda: !lambda |-
          // revert back to the default update interval
          static uint32_t default_update_interval = id(ultrasonic_sensor).get_update_interval();
          // 100 = 100 ms = New "high speed" interval
          int update_interval = x ? 100 : default_update_interval;
          id(ultrasonic_sensor).set_update_interval(update_interval);
          id(ultrasonic_sensor).call_setup();

When the HA toggle is on the ultrasonic sensor updates every 100ms and when it is off only every 60s.

If there is an even more elegant way I would be happy about further suggestions. I try to work with this for now and see if there are any unintended consequences (maybe because of the call_setup()).

1 Like