Dynamically varying sensor scan interval

Hi I want a variable delay ping

I am using the [ping binary_sensor(https://www.home-assistant.io/integrations/ping/) but would like to be able to vary the interval. Most of the time it’s fine to check every 5 minutes (300) but when I perform certain actions I want to check as frequently as every second. I tried using a value_template in the scan interval

binary_sensor:
  - platform: ping
    name: variabletimedping
    host: 192.168.8.81
    scan_interval: 
      value_template: 60

but this gives the error

ERROR (MainThread) [homeassistant.config] 
Invalid config for [binary_sensor]: 
[value_template] is an invalid option for [binary_sensor]. 
Check: binary_sensor->scan_interval->value_template.

A similar question was asked here with no answer. This post seems to suggest that scan interval can have a value template under the command line platform. So I tried

sensor
  - platform: command_line
    name: "ping test 1"
    command: ping -c 5 192.168.8.81 &> /dev/null && echo 0 || echo 1
    scan_interval: 
      value_template: 60

But this also gives the error

[homeassistant.config] 
Invalid config for [sensor]: 
[value_template] is an invalid option for [sensor]. 
Check: sensor->scan_interval->value_template.

Could anybody suggest a way to have a ping run every second but just for a very limited time, to stop them swamping network traffic. How could I turn this on and off / enable or disable the sensor? Thanks

No you can’t template the scan interval. What you can do is set it to a very long time (years) and update it at varying rates using the update entity service in an automation.

1 Like

Oh that sounds like it will fit my needs very well, @tom_l - do you have any examples to hand?

1 Like

That’s a good and workable solution @tom_l and gives me exactly what I was hoping for, I was going to be using it in automations, so it’s ideal. If you want to see the correct formatting in the solution check the link at the top of it.

See also the config files for a working solution using automations in this way in a UX for remote reboots

I’m still too little experienced in this stuff and also have my challenges in this field.

I see a problem: Automations need updated sensor values. But they start with a trigger followed by conditions followed by actions. How do you locate/execute an entity update between the trigger and the condition? I can add it as a part of the actions, but this is too late.

By the way: what happens if you set the scan interval to 0/zero? :wink:

That is precisely where you do it.

You have one automation triggered by a variable time interval that updates the sensor in its actions. You can have another automation triggered by this sensor update.

I believe it falls back to the default polling interval of the sensor.

This appears to be rather complicated - two separate but timely dependent automations.

I performed a brief test and the following appears to work: I added the update to the action part, then opened automation.yaml and moved the action from the action code part between the trigger and the condition code part:

alias: Switch boiler to anti-legionella-temp
  description: ''
  trigger:
  - minutes: '18'
    platform: time_pattern
  action:
  - data: {}
    entity_id: sensor.single_cheapest
    service: homeassistant.update_entity
  - data:
      message: VVB automatikken er triggered
      title: An automation says
    service: notify.notify
  condition:
  - after: sunset
    before: sunrise
    condition: sun
  - condition: state
    entity_id: sensor.single_cheapest
    state: 'true'
  action:
  - data: {}
    entity_id: sensor.single_cheapest
    service: homeassistant.update_entity
  - data: {}
    entity_id: switch.vv_bereder_z_tz68
    service: switch.turn_on
  - data:
      message: ...
      title: An automation says
    service: notify.notify

Sorry for some Norwegian-based text

I’m surprised that passed a config check and let you reload automations / restart. It is certainly invalid.

This whole second action block should never execute:

  action:
  - data: {}
    entity_id: sensor.single_cheapest
    service: homeassistant.update_entity
  - data: {}
    entity_id: switch.vv_bereder_z_tz68
    service: switch.turn_on
  - data:
      message: ...
      title: An automation says
    service: notify.notify

As I said initially: I am very new to HASS, YAML, PYTHON.

My assumption was that the YAML sequence is processed sequentially without any semantic check of the sequence as such, and the UI for editing automations only is something on top of it which does not cover such special cases as inserting actions before conditions.

By the way, what specifically is invalid with the second action block?

You can only have one action block per automation.

I had the same issue. I ended up solving it by using an input_boolean and an automation.

alias: Logs - Fast polling
description: ''
trigger:
  - platform: state
    entity_id: input_boolean.minigun_log_poller
    to: 'on'
condition: []
action:
  - repeat:
      while:
        - condition: state
          entity_id: input_boolean.minigun_log_poller
          state: 'on'
      sequence:
        - service: homeassistant.update_entity
          target:
            entity_id: sensor.rest_minigun_logs
        - wait_template: ''
          timeout: '00:00:05'
          continue_on_timeout: true
mode: single

With this I can change from a 5 minute polling period to once a second. The sequence/while loop made it pretty easy.

Using the input_boolean I can trigger the quick polling a few different ways which is nice.

Interesting alternative, thanks for sharing it