Enable/disable RESTful blocks of code in a configuration.yaml

In my configuration.yaml fill I have two blocks of RESTful code that I use to access cloud based information about my PV system. The cloud seems to get overwhelmed and when additional RESTful sensors it tends to become unresponsive. Is there a way that I could selectively enable/disable the individual blocks of RESTful code so that their execution could be staggered? Maybe even set a flag to indicate when one block of code has successfully received a response back so that the next block can be executed?

#CONFIGURATION.YAML yaml file

# Loads default set of integrations. Do not remove.
default_config:

# Line to enable HA Configuration Editor
config_editor:

# Text to speech
tts:
  - platform: google_translate
  
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml


# RESTful INTEGRATIONS
rest:

# Studer Synoptic Data Request
  - authentication: basic
    method: GET
    scan_interval: 10
    timeout: 120
    resource: https://api.studer-innotec.com/api/v1/installation/synoptic/NNNN #replace NNNN with your Studer system ID
    headers:
      Accept: application/json
      UHASH: !secret UHASH  # Points to SHA256 encoded username in secrecs.yaml file, or you can replace !secret UHASH with your hard coded SHA256 encoded username right here in the configuration.yaml file
      PHASH: !secret PHASH  # Points to #MD5 encoded password in secrecs.yaml file, or you can replace !secret UHASH with your hard coded #MD5 encoded password right here in the configuration.yaml file
    sensor:
      - name: "Studer"
        unique_id: "studer"
        value_template: "42"
        json_attributes:
          - energy
          - battery
          - power


# Studer Aux contact status read
  - authentication: basic
    method: GET
    scan_interval: 30
    timeout: 120
#Put you Studer cloud credentials and system ID in the line below.    
    resource: "https://portal.studer-innotec.com/scomwebservice.asmx/ReadUserInfo?email=<[email protected]>>&pwd=<your_studer_password>&installationNumber=<your_studer_system_ID>&device=XT1&infoId=3031"
    sensor:
      - name: "Studer Xtender AUX1"
        unique_id: "studer_xtender_aux1"
        value_template: "{{ 'unavailable' if (value_json.UserInfoResult.ErrorCode!='1') else 'opened' if (value_json.UserInfoResult.UIntValue=='0') else 'closed' }}"
        json_attributes_path: "$['UserInfoResult']"
        json_attributes:
          - UIntValue
          - FloatValue
          - ErrorCode
          - ErrorMessage
          - ScomFormatNo

# TEMPLATE INTEGRATION    
template:

#sensor to identify if daylight savings for selecting summer or winter tariff structure  
  - binary_sensor:
      - name: "Is DST"
        state: "{{ now().timetuple().tm_isdst == true }}"

        
#INCLUDE for tariffs sensor calculations
  - sensor: !include tariffs.yaml

#INCLUDE for Studer sensors that copy data from RESTful API call attributes 
  - sensor: !include studer.yaml  

#INCLUDE for Shelly sensor calculations 
  - sensor: !include shelly.yaml  

#INCLUDE for Tuya sensor yaml blocks 
  - sensor: !include tuya.yaml

I was using a very long update time and calling update entity service for respective rest sensor through automations whenever i want, so i can control the order and load on server end

1 Like

As fuatakgun said, extend the scan interval out to several years or more, and then use an automation to trigger updates with the update_entity service.

1 Like

Then in an automation have an action something like:

service: homeassistant.update_entity
data: {}
target:
  entity_id: sensor.studer
1 Like

Yes this is right :slight_smile:

1 Like

I put together an automation that alternately calls 2 RESTful blocks. The first block is called once, then the second is called 5 times in a row based on the value of a counter. The blocks are called every 5 seconds unless the cloud responded with an error on the last read, then the call rate is reduced to one a minute.

I imagine there is a ore elegant way of doing this, but it hasn’t occurred to me.

What happens if the RESTful block is called before it has received data from the previous request?

alias: Studer RESTful API call
description: ""
trigger:
  - platform: time_pattern
    id: scan
    seconds: /5
  - platform: time_pattern
    id: retry_after_pause
    minutes: /1
condition: []
action:
  - if:
      - condition: state
        entity_id: sensor.studer_cloud_status
        state: Studer Cloud Online
      - condition: state
        entity_id: sensor.studer_aux1_api_read_status
        state: AUX Read OK
        enabled: true
    then:
      - if:
          - condition: trigger
            id:
              - scan
        then:
          - choose:
              - conditions:
                  - condition: numeric_state
                    entity_id: counter.studer_restful_scanner
                    below: 1
                sequence:
                  - service: homeassistant.update_entity
                    data: {}
                    target:
                      entity_id: sensor.studer_xtender_aux1
              - conditions:
                  - condition: numeric_state
                    entity_id: counter.studer_restful_scanner
                    above: 0
                sequence:
                  - service: homeassistant.update_entity
                    data: {}
                    target:
                      entity_id: sensor.studer
          - if:
              - condition: numeric_state
                entity_id: counter.studer_restful_scanner
                below: 5
            then:
              - service: counter.increment
                data: {}
                target:
                  entity_id: counter.studer_restful_scanner
            else:
              - service: counter.reset
                data: {}
                target:
                  entity_id: counter.studer_restful_scanner
    else:
      - if:
          - condition: trigger
            id:
              - retry_after_pause
        then:
          - choose:
              - conditions:
                  - condition: numeric_state
                    entity_id: counter.studer_restful_scanner
                    below: 1
                sequence:
                  - service: homeassistant.update_entity
                    data: {}
                    target:
                      entity_id: sensor.studer_xtender_aux1
              - conditions:
                  - condition: numeric_state
                    entity_id: counter.studer_restful_scanner
                    above: 0
                sequence:
                  - service: homeassistant.update_entity
                    data: {}
                    target:
                      entity_id: sensor.studer
          - if:
              - condition: numeric_state
                entity_id: counter.studer_restful_scanner
                below: 5
            then:
              - service: counter.increment
                data: {}
                target:
                  entity_id: counter.studer_restful_scanner
            else:
              - service: counter.reset
                data: {}
                target:
                  entity_id: counter.studer_restful_scanner
mode: single

I have been unable to get update_entity to refresh a rest sensor more than once every 10 seconds. It seems to be throttled.

Does anyone know if this is intended - AFAICS it is not documented anywhere.

What if you want to refresh up to 1/sec? update_entity does not seem to allow this…