[SOLVED] Limit restful API only in certain day/time to avoid unnecessary request

I’ve used restful API get a device address location, and then call another API to get the lat & lon of the device with locationiq. Then it will create a custom device and plot on map.
The code is added in my configuration.yaml and everything works fine.

However, since i have limit on the calling on locationiq. Is there a way to only set to do API call only on Monday, Tuesday, Thursday, Friday, Saturday, and between 6:30PM ~ 7:50? That’s the time i will actually get value, so this will avoid unnecessary call on locationiq.
Or is there a way to only trigger the request when state of a entity (custom sensor ‘CurrentAddress’) change?

rest:
  - resource: https://crd-rubbish.epd.ntpc.gov.tw/dispProject/api/line-status.ashx?lineid=244025
    scan_interval: 60
    method: GET
    sensor:
      - name: "CurrentAddress"
        value_template: "{{ value_json['data']['place'] }}"
  - resource_template: https://us1.locationiq.com/v1/search?key=API_key&q={{ states('sensor.CurrentAddress') }}&format=json
    scan_interval: 60 
    method: GET
    sensor:
      - name: "Lat"
        value_template: "{{ value_json[0]['lat'] }}"
      - name: "Lon"
        value_template: "{{ value_json[0]['lon'] }}"

template:
  - binary_sensor:
      - name: CustomDevice
        state: >
          {{ is_state('device_tracker.my_device_nmap', 'home') or is_state('device_tracker.my_device_gps', 'home') }} #This doesn't do anything.
        attributes:
          latitude: "{{ states('sensor.Lat') }}"
          longitude: "{{ states('sensor.Lon') }}"

Set a really long scan interval (years).

Then update the entities using an automation that calls the homeassistant.update_entity service.

You can use any combination of triggers and conditions you want to achieve your schedule. Or even use the schedule helper.

e.g. time pattern trigger every 60 seconds, condition that the shcedule helper must be on.

2 Likes