Use templating in sensor

Is it possible to use templating in a sensor? I have the following

  - platform: google_travel_time
    name: Morning Commute
    scan_interval: >-
      {% if is_state("binary_sensor.workday_sensor", "on") 
        and now().hour > 7 and now().hour < 9 -%}
        480
      {% else %}
        10000
      {% endif %}
    api_key: !secret google_maps_api
    origin: zone.home
    destination: zone.work

but when I check the config I get

Invalid config for [sensor]: offset {% if is_state("binary_sensor.workday_sensor", "on") 
  and now().hour > 7 and now().hour < 9 -%}
  480
{% else %}
  10000
{% endif %} should be format 'HH:MM' or 'HH:MM:SS' for dictionary value @ data['scan_interval']. Got None. (See ?, line ?).

No, nein (sorry, I had to enter at least 10 characters! :wink:)

EDIT: Well, at least, you can’t use a template for the scan_interval parameter. The docs usually state explicitly where templates are accepted.

1 Like

Mmmm, is there another way to define the scan interval based on the time of day ( to lower the requests made to the Google api)

I looked at the documentation for this platform… I have not seen any “scan_interval” parameter…
But as mentioned, you can refresh the sensor with a defined frequency using automations…
See documentation for more details…

Aha, so when I use the automation, I can drop the scan_interval?

Actually, it does mention it, just not in the configuration variables section. Like most “entity based” integrations, scan_interval applies (and is documented in the link I provided above.)

Yes, if by “drop” you mean make much longer. The default for this integration is updating every 5 minutes. If you want to update less often (and/or on demand via an automation), you need to specify scan_interval with a very large time. E.g.:

scan_interval:
  days: 365

Otherwise it would still update every 5 minutes, in addition to updating on request.

1 Like

Thanks for clarifying… but there is no pain to put this parameter in the webpage of the documentation I mentioned, this will be more straight forward than looking to all possible parameters in various webpages for one platform… fortunately, there is a very good and active home assistant community !!! :wink: