How to set te scan interval in configuration.yaml

This is driving me crazy! HOW do you pass a setting of the scan interval of a resful sensor to the configuration settings?

I’ve tried everything, but the only thing that work is:

scan_interval: 7200 #seconds
scan_interval: "02:00:00" # HH:MM:SS format as requested in error code

In the Template Editor I made this template:

{{states('input_select.xau_api_update')}}

This renders exactly the same value as the hard-coded “02:00:00”; I put it behind the scan_interval in the YAML in the following forms:

scan_interval: {{states('input_select.xau_api_update')}}
scan_interval: "{{states('input_select.xau_api_update')}}"

…but to no avail.

I’ve also tried numerical inputs with the seconds value; that didn’t work either although the Tempate Editor rendered perfectly good numerical values.

Any help on how I can debug this is welcome.

You can’t. scan_interval expects an integer, and a template isn’t an integer (docs).

image

You could set a long interval, and use an automation to force an update at whatever frequency you want:

  action:
    - service: homeassistant.update_entity
      target:
        entity_id: sensor.your_sensor_id

Thanks!
I more or less feared that answer, because the probably the configuration.yaml doesn’t update as a value changes and apparently doesn’t pass a template to the service to do so, either…
The strange thing is however that it also accepts a string, formatted “HH:MM:SS”; this was indicated in the debug info.

I know the service update “trick” via an automation works, but I thought I’d try to dynamically change the service parms, because then there’s no need for extra automations. Alas, we’ll have to do with that solution.

If the option does not explicitly state that it accepts templates then it doesn’t.

Where can be found what options accept as value types? I’ve been looking for that but couldn’t find it.

Any of the documentation pages. For example, in the link I provided above:

image

1 Like

Thanks! I used the long scan_interval solution with an automation requesting updates when appropriate. But then I ran into the problem that the service is called at least once after a restart. Of course, this is fine if it weren’t for the limited number of free API-calls: 500/month. For that, I found the following solution:

In the secrets.yaml I coded this:

gold_api_key: >
    {% if now().isoweekday() < 5 %}
      {{'goldapi-xxxVALID KEYxxx-io'}}
    {%else%}
      {{'goldapi-xxxxxxxxxxxxxxx-io'}}
    {%endif%}

This prevents access to the API in the weekend, when I work on the HA coding and the exchange is closed anyway :slight_smile: