Cannot figure out what is eating my API call count

Hi everyone

I have a free tier account with AccuWeather which allows for 50 API call per day. I have written what I think is a nice feature in determining if my irrigation should run based on the rain probability + volume. I currently have the following set up:

In configuration.yml:

rest:
  # Retrieve rain probabilities and volumes from 5 day forecast on AccuWeather
  - resource: "http://dataservice.accuweather.com/forecasts/v1/daily/5day/LOCATION?apikey=MyKeY&details=true&metric=true"
    scan_interval: 360

    sensor:
      - name: "AccuWRainProbabilityDay1"
        value_template: "{{ value_json['DailyForecasts'][1]['Day']['RainProbability'] }}"
        unit_of_measurement: "%"

      - name: "AccuWRainValueDay1"
        value_template: "{{ value_json['DailyForecasts'][1]['Day']['Rain'].Value }}"
        unit_of_measurement: "mm"
        
      - name: "AccuWRainProbabilityNight1"
        value_template: "{{ value_json['DailyForecasts'][1]['Night']['RainProbability'] }}"
        unit_of_measurement: "%"

      - name: "AccuWRainValueNight1"
        value_template: "{{ value_json['DailyForecasts'][1]['Night']['Rain'].Value }}"
        unit_of_measurement: "mm"  
        
      - name: "AccuWRainProbabilityDay2"
        value_template: "{{ value_json['DailyForecasts'][2]['Day']['RainProbability'] }}"
        unit_of_measurement: "%"

      - name: "AccuWRainValueDay2"
        value_template: "{{ value_json['DailyForecasts'][2]['Day']['Rain'].Value }}"
        unit_of_measurement: "mm"
        
      - name: "AccuWRainProbabilityNight2"
        value_template: "{{ value_json['DailyForecasts'][2]['Night']['RainProbability'] }}"
        unit_of_measurement: "%"

      - name: "AccuWRainValueNight2"
        value_template: "{{ value_json['DailyForecasts'][2]['Night']['Rain'].Value }}"
        unit_of_measurement: "mm"  

      - name: "AccuWRainProbabilityDay3"
        value_template: "{{ value_json['DailyForecasts'][3]['Day']['RainProbability'] }}"
        unit_of_measurement: "%"

      - name: "AccuWRainValueDay3"
        value_template: "{{ value_json['DailyForecasts'][3]['Day']['Rain'].Value }}"
        unit_of_measurement: "mm"
        
      - name: "AccuWRainProbabilityNight3"
        value_template: "{{ value_json['DailyForecasts'][3]['Night']['RainProbability'] }}"
        unit_of_measurement: "%"

      - name: "AccuWRainValueNight3"
        value_template: "{{ value_json['DailyForecasts'][3]['Night']['Rain'].Value }}"
        unit_of_measurement: "mm"
        
      - name: "AccuWRainProbabilityDay4"
        value_template: "{{ value_json['DailyForecasts'][4]['Day']['RainProbability'] }}"
        unit_of_measurement: "%"

      - name: "AccuWRainValueDay4"
        value_template: "{{ value_json['DailyForecasts'][4]['Day']['Rain'].Value }}"
        unit_of_measurement: "mm"

      - name: "AccuWRainProbabilityNight4"
        value_template: "{{ value_json['DailyForecasts'][4]['Night']['RainProbability'] }}"
        unit_of_measurement: "%"

      - name: "AccuWRainValueNight4"
        value_template: "{{ value_json['DailyForecasts'][4]['Night']['Rain'].Value }}"
        unit_of_measurement: "mm"

Then in template.yml I have:

# AccuWeather integration calculations
- trigger:
  - platform: time_pattern
    hours: '/12'
  sensor:
    - name: "RainProbabilityDay1"
      unit_of_measurement: "%"
      state: >
        {% set Day = states('sensor.AccuWRainProbabilityDay1')|float %}
        {% set Night = states('sensor.AccuWRainProbabilityNight1')|float %}
        
        {{ ((Day + Night) / 2) | round(1, default=0) }}

      availability: "{{ is_number(states('sensor.AccuWRainProbabilityDay1')) }}"

- trigger:
  - platform: time_pattern
    hours: '/12'
  sensor:
    - name: "RainProbabilityDay2"
      unit_of_measurement: "%"
      state: >
        {% set Day = states('sensor.AccuWRainProbabilityDay2')|float %}
        {% set Night = states('sensor.AccuWRainProbabilityNight2')|float %}
        
        {{ ((Day + Night) / 2) | round(1, default=0) }}
      
      availability: "{{ is_number(states('sensor.AccuWRainProbabilityDay2')) }}"
    
- trigger:
  - platform: time_pattern
    hours: '/12'
  sensor:
    - name: "RainProbabilityDay3"
      unit_of_measurement: "%"
      state: >
        {% set Day = states('sensor.AccuWRainProbabilityDay3')|float %}
        {% set Night = states('sensor.AccuWRainProbabilityNight3')|float %}
        
        {{ ((Day + Night) / 2) | round(1, default=0) }}
      
      availability: "{{ is_number(states('sensor.AccuWRainProbabilityDay3')) }}"
        
- trigger:
  - platform: time_pattern
    hours: '/12'
  sensor:
    - name: "RainProbabilityDay4"
      unit_of_measurement: "%"
      state: >
        {% set Day = states('sensor.AccuWRainProbabilityDay4')|float %}
        {% set Night = states('sensor.AccuWRainProbabilityNight4')|float %}
        
        {{ ((Day + Night) / 2) | round(1, default=0) }}
      
      availability: "{{ is_number(states('sensor.AccuWRainProbabilityDay4')) }}"
      
- trigger:
  - platform: time_pattern
    hours: '/12'
  sensor:
    - name: "RainVolumeDay1"
      unit_of_measurement: "mm"
      state: >
        {% set Day = states('sensor.AccuWRainValueDay1')|float %}
        {% set Night = states('sensor.AccuWRainValueNight1')|float %}
        
        {{ (Day + Night) | round(1, default=0) }}
      
      availability: "{{ is_number(states('sensor.AccuWRainValueDay1')) }}"
        
- trigger:
  - platform: time_pattern
    hours: '/12'
  sensor:
    - name: "RainVolumeDay2"
      unit_of_measurement: "mm"
      state: >
        {% set Day = states('sensor.AccuWRainValueDay2')|float %}
        {% set Night = states('sensor.AccuWRainValueNight2')|float %}
        
        {{ (Day + Night) | round(1, default=0) }}
      
      availability: "{{ is_number(states('sensor.AccuWRainValueDay2')) }}"
    
- trigger:
  - platform: time_pattern
    hours: '/12'
  sensor:
    - name: "RainVolumeDay3"
      unit_of_measurement: "mm"
      state: >
        {% set Day = states('sensor.AccuWRainValueDay3')|float %}
        {% set Night = states('sensor.AccuWRainValueNight3')|float %}
        
        {{ (Day + Night) | round(1, default=0) }}
      
      availability: "{{ is_number(states('sensor.AccuWRainValueDay3')) }}"
        
- trigger:
  - platform: time_pattern
    hours: '/12'
  sensor:
    - name: "RainVolumeDay4"
      unit_of_measurement: "mm"
      state: >
        {% set Day = states('sensor.AccuWRainValueDay4')|float %}
        {% set Night = states('sensor.AccuWRainValueNight4')|float %}
        
        {{ (Day + Night) | round(1, default=0) }}
      
      availability: "{{ is_number(states('sensor.AccuWRainValueDay4')) }}"

According to AccuWeather counters I am consistently hitting 10 messages per hour, and I don’t know why that is? According to me I make one call, and from that one call all is being calculated. Or am I missing something important?

Any help will be greatly appreciated!

1 Like

refresh (scan_interval) is at 360 seconds… = 6 minutes = 10x hour

I am SO stupid. Thank you for showing me that. It should have been 3600. Thank you. Question: is the trigger: hours /12 for the template values strictly necessary? Or will it only update if the sensor is updated once every 3600 (:slight_smile: ) seconds? I am struggling a bit to understand that part properly.

The trigger interval on your template isn’t needed for this, you could probably get away with setting up the sensors as just a template helper in the UI as. Then whenever your REST sensors update, that templated sensor will also update (otherwise it will happen every 12 hours from the time HA starts up, if the API itself is slow to respond etc, it might also “update” your templated sensors to stale values (if they have changed)

Hint, to avoid hitting the limit you could set the scan_interval to something very high and trigger the sensor via an automation (update entity), this way you can execute an update at the moments you want, e.g. not sure if you require updates at every hour of the day

Thank you! That is what I had initially had. Was only executing it once a day. But then, as with all things HA I wanted “to make it better” so I decided to change it. Will try the 3600 interval and then see if it goes better once my API quota is reset.

It is not needed. The template will update when the source sensors update.

1 Like

Thank you. After all the replies it indeed seems that I am down to 1 API call per hour now, and all the template values are indeed correct. Thanks to this awesome community.